- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想计算文本语料库中单词的词频。我一直在使用 NLTK 的 word_tokenize 后跟 probability.FreqDist 一段时间来完成这项工作。 word_tokenize 返回一个列表,该列表由 FreqDist 转换为频率分布。然而,我最近遇到了集合中的计数器函数 (collections.Counter),它似乎在做完全相同的事情。 FreqDist 和 Counter 都有一个 most_common(n) 函数,它返回 n 个最常见的单词。有谁知道这两者之间是否有区别?一个比另一个快吗?是否存在其中一个行得通而另一个行不通的情况?
最佳答案
nltk.probability.FreqDist
是 collections.Counter
的子类。
来自docs :
A frequency distribution for the outcomes of an experiment. A frequency distribution records the number of times each outcome of an experiment has occurred. For example, a frequency distribution could be used to record the frequency of each word type in a document. Formally, a frequency distribution can be defined as a function mapping from each sample to the number of times that sample occurred as an outcome.
The inheritance is explicitly shown from the code从本质上讲,Counter
和 FreqDist
的初始化方式没有区别,请参阅 https://github.com/nltk/nltk/blob/develop/nltk/probability.py#L106
所以在速度方面,创建一个 Counter
和 FreqDist
应该是一样的。速度上的差异应该是微不足道的,但值得注意的是,开销可能是:
.__init__()
主要区别在于 FreqDist
为统计/概率自然语言处理 (NLP) 提供的各种函数,例如finding hapaxes . FreqDist
扩展 Counter
的完整函数列表如下:
>>> from collections import Counter
>>> from nltk import FreqDist
>>> x = FreqDist()
>>> y = Counter()
>>> set(dir(x)).difference(set(dir(y)))
set(['plot', 'hapaxes', '_cumulative_frequencies', 'r_Nr', 'pprint', 'N', 'unicode_repr', 'B', 'tabulate', 'pformat', 'max', 'Nr', 'freq', '__unicode__'])
当谈到使用FreqDist.most_common()
时,它实际上是在使用Counter
的父函数,所以检索排序的most_common
的速度> 两种类型的列表相同。
就个人而言,当我只想检索计数时,我使用 collections.Counter
。但是,当我需要进行一些统计操作时,我要么使用 nltk.FreqDist
,要么将 Counter
转储到 pandas.DataFrame
中(请参阅Transform a Counter object into a Pandas DataFrame)。
关于python - Python 的 collections.Counter 和 nltk.probability.FreqDist 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34603922/
哪个更快? Counter()+=Counter 或 Counter.update(Counter)? 为什么一个比另一个更快? 我尝试了一些简单的分析,但我认为这不足以最终证明 Counter+=C
这个问题在这里已经有了答案: ++someVariable vs. someVariable++ in JavaScript (7 个答案) 关闭 7 年前。 var counter = 0; va
下面是我正在使用的代码。如果我按 addQuanity m_label 设置显示一个而不是两个。如果我再次按 addWuantity,m_label 显示 2。按 minusQuantity 将 m_
这个问题已经有答案了: Does Java evaluate remaining conditions after boolean result is known? (7 个回答) 已关闭 6 年前。
因此,当我将计数器(from collections import Counter)打印到一个文件时,我总是得到它的文字 Counter ({'Foo': 12}) 有没有办法让计数器不那么字面地写出
我正在使用 CSS2.1 计数器将数字应用于棋盘上的人,以实现棋盘游戏,其棋盘图使用 HTML 和 CSS,方法如下: .ply {counter-increment:main;} .move:be
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Is there a performance difference between i++ and ++i
我在尝试编译 Arduino 草图时遇到此错误。我看不出它认为我试图在没有参数的情况下调用 Counter::Counter 的地方。这是怎么回事? sketch/periodic_effect.cp
调用Get-Counter时使用-ComputerName参数和使用-Counter参数中的路径有区别吗? Get-Counter -Counter "\Memory\Available MB
姓名 Counter在 collections 中都定义了(作为一个类(class))和在 typing (作为通用类型名称)。不幸的是,它们略有不同。处理这个问题的推荐方法是什么? 相同点和不同点:
此代码不会给出任何失败,但如果您使用 counter++,则第一次迭代会失败。 parameters="one two three" counter=0 for option in $param
powershell 中的 get-counter/export-counter cmdlet 似乎以美国格式返回日期,这在这种情况下是相当不受欢迎的。我浏览了两个 get-help -full 页面
我有 2 个计数器(来自集合的计数器),我想将一个附加到另一个,而第一个计数器的重叠键将被忽略。喜欢 dic.update (python 词典更新) 例如: from collections imp
我想在我的项目中为 Provider ( ChangeNotifierProvider ) 创建一个单元测试,我的单元测试、小部件测试和集成测试成功通过 ✔️,所以现在我尝试(努力尝试🥵...)创建
我知道以下代码的复杂度为 O(log(n)): while (n>1) { counter++; n/=2; } 我知道在这里,n 在每次迭代中被分成两半,这意味着如果 n 是 100
Counter.getName() 方法与 Counter.getDisplayName() 方法有什么区别。我没有从文档中看到太多信息 http://hadoop.apache.org/docs/r
我有一个 python 文件,用于在 Hadoop(版本 2.6.0)上使用 mrjob 来计算二元语法,但我没有得到我希望的输出,而且我在破译终端中的输出时遇到了问题我哪里出错了。 我的代码: re
我看到带有错误消息的事件 ID 2001: It has taken too long to refresh the W3SVC counters , the stale counters are b
我对 React 完全陌生,我正在 YouTube 上学习教程(使用 MOSH 编程),但我遇到了这个错误,在找到类似问题后无法解决。 index.js import React from 'reac
我正在运行一个 hadoop 作业(来自 oozie),它有几个计数器和多输出。 我收到如下错误:org.apache.hadoop.mapreduce.counters.LimitExceededE
我是一名优秀的程序员,十分优秀!