2012-04-05 21 views
2

Ich versuche, eine Liste von Wörtern (eine Token-Zeichenfolge) in jede mögliche Teilzeichenfolge zu brechen. Ich würde dann gerne eine FreqDist auf jeder Teilkette ausführen, um die häufigste Teilkette zu finden. Der erste Teil funktioniert gut. Allerdings, wenn ich den FreqDist laufen lasse, erhalte ich die Fehlermeldung:Python-Häufigkeitsverteilung (FreqDist/NLTK) Problem

TypeError: unhashable type: 'list' 

Hier ist mein Code:

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 

Die Ausgabe von substrings ist in Ordnung. Hier ist es:

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

Allerdings kann ich nicht einfach die FreqDist darauf laufen lassen. Jede Einsicht würde sehr geschätzt werden. In diesem Fall hätte jeder Teilstring nur eine FreqDist von 1, aber dieses Programm soll auf einer viel größeren Textprobe ausgeführt werden.

Antwort

6

Ich bin mir nicht ganz sicher, was Sie wollen, aber die Fehlermeldung sagt, dass sie die Liste hashen möchte, was normalerweise ein Zeichen ist, dass sie in einen Satz gesetzt oder als Wörterbuchschlüssel verwendet wird. Wir können das umgehen, indem wir stattdessen Tupel geben.

>>> 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']] 

aber wir haben noch

>>> 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' 

Wenn wir die Subsequenzen in Tupeln machen, aber:

>>> 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> 

Ist das, was Sie suchen?