我正在尝试将单词列表(标记化字符串)分解为每个可能的子字符串。然后我想对每个子字符串运行一个 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,但该程序旨在在更大的文本样本上运行。