【发布时间】:2020-02-08 03:56:08
【问题描述】:
我想编写一个函数来返回给定文本的 n-gram 中每个元素的频率。 请帮忙。 我做了这个代码来计算 2 克的频率
代码:
from nltk import FreqDist
from nltk.util import ngrams
def compute_freq():
textfile = "please write a function"
bigramfdist = FreqDist()
threeramfdist = FreqDist()
for line in textfile:
if len(line) > 1:
tokens = line.strip().split(' ')
bigrams = ngrams(tokens, 2)
bigramfdist.update(bigrams)
return bigramfdist
bigramfdist = compute_freq()
【问题讨论】:
-
您在哪方面需要帮助?
-
欢迎来到 StackOverflow。请花时间阅读这篇关于如何提供minimal, Complete, and Verifiable example 的帖子并相应地修改您的问题
-
代码添加@yatu请看
-
@Code-Apprentice 我添加了我的代码,请看一下
-
您的具体问题是什么?发布的代码如何无法执行您想要的操作?
标签: python pandas nltk tf-idf countvectorizer