如何构造大型语料库中的一元、二元和三元词,并计算它们的出现频率。按照最频繁的到最不频繁的gram来排列结果。
from nltk import word_tokenize
from nltk.util import ngrams
from collections import Counter
text = "I need to write a program in NLTK that breaks a corpus (a large collection of \
txt files) into unigrams, bigrams, trigrams, fourgrams and fivegrams.\
I need to write a program in NLTK that breaks a corpus"
token = nltk.word_tokenize(text)
bigrams = ngrams(token,2)
trigrams = ngrams(token,3)```发布于 2020-05-08 23:03:10
试试这个:
import nltk
from nltk import word_tokenize
from nltk.util import ngrams
from collections import Counter
text = '''I need to write a program in NLTK that breaks a corpus (a large
collection of txt files) into unigrams, bigrams, trigrams, fourgrams and
fivegrams. I need to write a program in NLTK that breaks a corpus'''
token = nltk.word_tokenize(text)
most_frequent_bigrams = Counter(list(ngrams(token,2))).most_common()
most_frequent_trigrams = Counter(list(ngrams(token,3))).most_common()
for k, v in most_frequent_bigrams:
print (k,v)
for k, v in most_frequent_trigrams:
print (k,v)https://stackoverflow.com/questions/61681613
复制相似问题