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を実行できません。どんな洞察力も大変高く評価されます。この場合、各部分文字列は1のFreqDistしか持たないが、このプログラムははるかに大きなテキストのサンプルで実行されることを意図している。