2
我試圖將每個可能的子字符串中的單詞列表(一個標記字符串)分解。然後,我想在每個子字符串上運行FreqDist,以查找最常見的子字符串。第一部分工作正常。然而,當我運行FreqDist,我得到的錯誤:Python頻率分佈(FreqDist/NLTK)問題
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,但該程序旨在運行在更大的文本樣本上。