- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我几乎已经对所有内容进行了排序,但是因为我想要前 2k 个唯一单词,所以我得到了一个 super 困惑的分布。我最终将用它来构建一本字典,但我想看看哪些是最常见的 2k 单词,这样我就可以为字典选择相关的单词。无论如何,请参阅下面的代码。如何修改以获得我看到(字)(计数)的列表?不必限制在 2k,如果能看到所有的数据会很高兴吗?谢谢!
>>> fileObj = codecs.open( "/Users/shannonmcgregor/Desktop/ALLstories.txt", "r", "Latin-1" )
chattanooga_stories = fileObj.read()
>>> import nltk
from nltk.corpus import stopwords
>>> lowered_stories = chattanooga_stories.lower()
>>> word_list = lowered_stories.split()
>>> filtered_stories = [w for w in word_list if not w in stopwords.words('english')]
>>> fdist = nltk.FreqDist(w.lower() for w in filtered_stories)
>>> print(fdist)
<FreqDist with 7031 samples and 19893 outcomes>
>>> top_2k = [ ]
>>> top_2k = fdist.most_common(2000)
>>> fdist.plot(2000, cumulative=True)
最佳答案
当您使用most_common()时,您确实可以获得各种单词的计数。使用 items 方法获取按排序顺序排列的项目列表(最常见的第一个)。
fdist = nltk.FreqDist(filtered_stories) #filtered_stories is already lowercase
print(fdist)
top_2k = [ ]
top_2k = fdist.most_common(2000)
tok_2k.items() #should give you a sorted list
关于python - 打印词频列表(有分布),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37934476/
我在 Eclipse 中创建了一个 Java 程序。该程序计算每个单词的频率。例如,如果用户输入“I went to the shop”,程序将产生输出“1 1 1 2”,即 1 个字长 1 ('I'
我在工作中只有 R 可用,而且我以前用 Python 做过。我需要获取 CSV 文件中每组事件的计数。我在 Python 中进行了情绪分析,我在提供的表格中搜索了一本 Python 字典,其中包含每个
我想一个字一个字地读,然后将哪个字与我的结构数组中的字进行比较。如果我没有,我想在第一个空位添加。 #include #include #include #include using names
我想计算已转换为标记的文本文件中特定单词前后三个单词的频率。 from nltk.tokenize import sent_tokenize from nltk.tokenize import wor
我需要编写一个程序来计算文本中每个单词的频率,此外我需要能够返回 n 个最常用单词的列表(如果更多单词具有相同的频率(它们按字母顺序排序)。还有一个未计算在内的单词列表(停用词)。 停用词使用什么结构
我对 sklearn 的 TfidfVectorizer 在计算每个文档中单词的频率时有一个疑问。 我看到的示例代码是: >>> from sklearn.feature_extraction.tex
我是一名优秀的程序员,十分优秀!