2016-09-25 5 views
0

텍스트 파일에서 문구 수를 얻으려고하고 있지만 지금까지는 단어 수 (아래 참조) 만 얻을 수 있습니다. 텍스트 파일에 2 단어 구문이 나타나는 횟수를 계산하려면이 논리를 확장해야합니다.NLTK를 사용하여 파이썬에서 문구 계산하기

내 이해에서 NLTK의 논리를 사용하여 구문을 정의하거나 그룹화 할 수 있습니다. 컬렉션 함수가 원하는 결과를 얻는 데 필요한 함수라고 생각하지만 NLTK 문서를 읽지 않고 구현하는 방법을 모르겠습니다. 모든 팁/도움을 크게 주시면 감사하겠습니다.

+0

두 개의 특정 단어를 찾고 계십니까? 함께 나타나는 두 단어 어구? – accraze

+0

함께 나타나는 두 단어 – bkubs557

+1

[nltk.bigrams()] (http://www.nltk.org/api/nltk.html#nltk.util.bigrams)를 찾으십니까? – alexis

답변

0

collocations 모듈을 사용하여 두 단어 구를 모두 얻을 수 있습니다. 이 도구는 종종 코퍼스 내에서 연속적으로 나타나는 단어를 식별합니다.

두 단어 구를 찾으려면 먼저 다른 단어의 컨텍스트에서 단어의 빈도와 모양을 계산해야합니다. NLTK에는 이것을 할 수있는 BigramCollocationFinder 클래스가 있습니다. 여기에 우리가 음절 Collocations을 찾을 수있는 방법은 다음과 같습니다

import re 
import string 
import nltk 
from nltk.tokenize import word_tokenize, sent_tokenize 
from nltk.collocations import BigramCollocationFinder, BigramAssocMeasures 

frequency = {} 
document_text = open('Words.txt', 'r') 
text_string = document_text.read().lower() 
match_pattern = re.findall(r'\b[a-z]{3,15}\b', text_string) 

finder = BigramCollocationFinder.from_words(match_pattern) 
bigram_measures = nltk.collocations.BigramAssocMeasures() 
print(finder.nbest(bigram_measures.pmi, 2)) 

NLTK Collocations 문서 : http://www.nltk.org/api/nltk.html?highlight=collocation#module-nltk.collocations

+0

고마워요! Finder 기능에 txt 파일을 전달하려고하면 "[('W', 'o'), ('d', 's')]"가 출력됩니다. Finder에 전달하기 전에 txt 파일에해야 할 일이 있습니까? 그 문서에서 분명하지 않았다. – bkubs557

+0

필자는 필자의 대답에서 코드를 업데이트했다. 대신에 'match_pattern'을 파인더에 전달해야한다고 생각한다. – accraze

0

nltk.brigrams는 특정 텍스트의 단어 쌍의 빈도를 반환합니다. 이 시도 :

import nltk 
from nltk import bigrams 

document_text = open('Words.txt', 'r') 
text_string = document_text.read().lower() 
tokens = word_tokenize(text_string) 
result = bigrams(tokens) 

출력 :

[(('w1', 'w2'), 6), (('w3', 'w4'), 3), (('w5', 'w6'), 3), (('w7', 'w8'), 3)...] 
관련 문제