首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在python中构造单字、双字和三字

在python中构造单字、双字和三字
EN

Stack Overflow用户
提问于 2020-05-08 22:38:25
回答 1查看 52关注 0票数 0

如何构造大型语料库中的一元、二元和三元词,并计算它们的出现频率。按照最频繁的到最不频繁的gram来排列结果。

代码语言:javascript
复制
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)```
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-08 23:03:10

试试这个:

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61681613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档