Python 频率分布 (FreqDist/NLTK) 问题

标签 python nltk frequency-distribution

我正在尝试将单词列表(标记化字符串)分解为每个可能的子字符串。然后我想在每个子字符串上运行 FreqDist,以找到最常见的子字符串。第一部分工作正常。但是,当我运行 FreqDist 时,出现错误:

TypeError: unhashable type: 'list'

这是我的代码:

import nltk

string = ['This','is','a','sample']
substrings = []

count1 = 0
count2 = 0

for word in string:
    while count2 <= len(string):
        if count1 != count2:
            temp = string[count1:count2]
            substrings.append(temp)
        count2 += 1
    count1 +=1
    count2 = count1

print substrings

fd = nltk.FreqDist(substrings)

print fd

substrings 的输出没问题。在这里:

[['This'], ['This', 'is'], ['This', 'is', 'a'], ['This', 'is', 'a', 'sample'], ['is'], ['is', 'a'], ['is', 'a', 'sample'], ['a'], ['a', 'sample'], ['sample']]

但是,我无法让 FreqDist 在其上运行。任何见解将不胜感激。在这种情况下,每个子字符串的 FreqDist 仅为 1,但该程序旨在运行更大的文本样本。

最佳答案

我不完全确定你想要什么,但错误消息是说它想要对列表进行哈希处理,这通常是将其放入集合中或将其用作字典键的标志。我们可以通过给它元组来解决这个问题。

>>> import nltk
>>> import itertools
>>> 
>>> sentence = ['This','is','a','sample']
>>> contiguous_subs = [sentence[i:j] for i,j in itertools.combinations(xrange(len(sentence)+1), 2)]
>>> contiguous_subs
[['This'], ['This', 'is'], ['This', 'is', 'a'], ['This', 'is', 'a', 'sample'],
 ['is'], ['is', 'a'], ['is', 'a', 'sample'], ['a'], ['a', 'sample'],
 ['sample']]

但我们还有

>>> fd = nltk.FreqDist(contiguous_subs)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/nltk/probability.py", line 107, in __init__
    self.update(samples)
  File "/usr/local/lib/python2.7/dist-packages/nltk/probability.py", line 437, in update
    self.inc(sample, count=count)
  File "/usr/local/lib/python2.7/dist-packages/nltk/probability.py", line 122, in inc
    self[sample] = self.get(sample,0) + count
TypeError: unhashable type: 'list'

但是,如果我们将子序列变成元组:

>>> contiguous_subs = [tuple(sentence[i:j]) for i,j in itertools.combinations(xrange(len(sentence)+1), 2)]
>>> contiguous_subs
[('This',), ('This', 'is'), ('This', 'is', 'a'), ('This', 'is', 'a', 'sample'), ('is',), ('is', 'a'), ('is', 'a', 'sample'), ('a',), ('a', 'sample'), ('sample',)]
>>> fd = nltk.FreqDist(contiguous_subs)
>>> print fd
<FreqDist: ('This',): 1, ('This', 'is'): 1, ('This', 'is', 'a'): 1, ('This', 'is', 'a', 'sample'): 1, ('a',): 1, ('a', 'sample'): 1, ('is',): 1, ('is', 'a'): 1, ('is', 'a', 'sample'): 1, ('sample',): 1>

这就是你要找的吗?

关于Python 频率分布 (FreqDist/NLTK) 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10031470/

相关文章:

python - 改变分解分布以匹配更多的聚合水平分布

r - R中Zipf(频率与排名)图的命令是什么

R:如何获取每个日期和小时的频率计数

python - matplotlib中FuncAnimation的手动时间循环控制

Python递增

python - 如何将字符串变量插入到这个长Python SQL字符串中?

python - Nltk安装

python - 从 synset 获取同义词返回错误 - Python

python - 如何在基于 Django 类的 View 中显示使用另一个模型的外键的注释?

python - 在 Python 中使用 selenium 从 <li> 项中提取文本