2017-09-22 3 views
1
Typeerror

Typeerror: ‚TfidfModel‘ Objekt ist nicht aufrufbarTFIDIF Modellherstellung in GENSIM

Warum kann ich die TFIDF Matrix nicht berechnet für jeden Doc nach der Initialisierung?

Ich begann mit 999 Dokumente: 999 Absätze mit jeweils etwa 5-15 Sätze. Nach Spacy alles Zeichenüber, habe ich das Wörterbuch (~ 16k einzigartige Token) und corpus (eine Liste der Listen von Tupeln)

nun die TFIDF Matrix (und später LDA und W2V Ich bin bereit, zu schaffen Matrices) für einige ML; Doch nach dem TFIDF Modell mit meinem Korpus Initialisierung (für die Berechnung der "IDF) tfidf = models.TfidfModel(corpus) ich die folgende Fehlermeldung erhalten, wenn die TFIDF jedes doc tfidf(corpus[5]) Typeerror zu sehen versuchen:‚TfidfModel‘Objekt ist nicht aufrufbar

Ich bin in der Lage, dieses Modell mit einem anderen Korpus zu erstellen, wo ich vier Dokumente habe jeweils nur aus einem Satz. Dort kann ich bestätigen, dass das erwartete Korpusformat eine Liste von Listen von Tupeln ist: [doc1 [(word1, count), (word2, count), ...], doc2 [(word3, count), (word4, zählen), ...] ...]

from gensim import corpora, models, similarities 

texts = [['teenager', 'martha', 'moxley'...], ['ok','like','kris','usual',...]...] 
dictionary = corpora.Dictionary(texts) 
>>> Dictionary(15937 unique tokens: ['teenager', 'martha', 'moxley']...) 

corpus = [dictionary.doc2bow(text) for text in texts] 
>>> [[(0, 2),(1, 2),(2, 1)...],[(3, 1),(4, 1)...]...] 

tfidf = models.TfidfModel(corpus) 
>>> TfidfModel(num_docs=999, num_nnz=86642) 

tfidf(corpus[0]) 
>>> TypeError: 'TfidfModel' object is not callable 

corpus[0] 
>>> [(0, 2),(1, 2),(2, 1)...] 

print(type(corpus),type(corpus[1]),type(corpus[1][3])) 
>>> <class 'list'> <class 'list'> <class 'tuple'> 

Antwort

0

Erweiterung auf @ whs2k Antwort wird die eckige Klammer-Syntax verwendet, um eine Transformation Wrapper zu bilden um den Korpus herum, bildet eine Art faule Verarbeitungspipeline.

ich nicht bekommen, bis ich die Notiz in diesem Tutorial lesen: https://radimrehurek.com/gensim/tut2.html

Calling model[corpus] only creates a wrapper around the old corpus document stream – actual conversions are done on-the-fly, during document iteration. We cannot convert the entire corpus at the time of calling corpus_transformed = model[corpus], because that would mean storing the result in main memory, and that contradicts gensim’s objective of memory-indepedence. If you will be iterating over the transformed corpus_transformed multiple times, and the transformation is costly, serialize the resulting corpus to disk first and continue using that.

Aber ich fühle mich immer noch nicht, dass ich voll und ganz die zugrunde liegende Python-Liste Magie verstehen.

1

Statt: tfidf(corpus[0])

Versuche: tfidf[corpus[0]]