2017-10-09 4 views
1

현재 Excel에서 텍스트를 읽고 bigram을 적용하고 있습니다. finalList 다음 샘플 코드에서 사용 된 목록은 입력 단어 입력에서 읽기 Excel 파일의 목록이 있습니다. 단어의 입력 텍스트ngram을 적용하기 전에 입력 텍스트를 이해하는 가장 좋은 방법

bigram=ngrams(finalList ,2) 

입력 텍스트 목록에 적용

from nltk.corpus import stopwords 

음절의 논리 :

는 다음 라이브러리의 도움으로 입력에서 중지 단어를 제거 내 엔드 - 투 - 엔드를 완료 방법.

전류 출력 : 완료, 종료, 종료 프로세스가 완료되었습니다.

원하는 출력 : 종단 간 종단 처리가 완료되었습니다.

즉, (end-to-end)와 같은 단어 그룹은 1 단어로 간주되어야 함을 의미합니다.

+2

토큰 화를 확인 하시겠습니까? – alexis

+1

적절한 토크 나이저 사용 : http://nlp.cogcomp.org/ – Daniel

답변

1

문제를 해결하려면 regex를 사용하여 정지 단어를 정리해야합니다. 다음 예를 참조하십시오.

import re 
text = 'I completed my end-to-end process..:?' 
pattern = re.compile(r"\.*:\?*") # to remove zero or more instances of such stop words, the hyphen is not included in the stop words. 
new_text = re.sub(pattern, '', text) 
print(new_text) 
'I completed my end-to-end process' 


# Now you can generate bigrams manually. 
# 1. Tokanize the new text 
tok = new_text.split() 
print(tok) # If the size of token is huge, just print the first five ones, like this print(tok[:5]) 
['I', 'completed', 'my', 'end-to-end', 'process'] 

# 2. Loop over the list and generate bigrams, store them in a var called bigrams 
bigrams = [] 
for i in range(len(tok) - 1): # -1 to avoid index error 
    bigram = tok[i] + ' ' + tok[i + 1] 
    bigrams.append(bigram) 


# 3. Print your bigrams 
for bi in bigrams: 
    print(bi, end = ', ') 

I completed, completed my, my end-to-end, end-to-end process, 

이 정보가 도움이되기를 바랍니다.