2017-09-22 18 views
1

형식 오류 : 'TfidfModel'객체는TFIDIF 모델 창조 형식 오류 Gensim에서

가없는 이유는 초기화 후 각 문서의 TFIDF 매트릭스를 계산할 수 호출하지? : 999 개의 문단으로 각각 약 5-15 문장으로 시작했습니다. 적응이 모든 것을 토큰 화 한 후, 나는 사전을 만들어 (~ 16K 고유 한 토큰) 및 코퍼스

지금 나는 TFIDF 행렬을 만들 준비가 (나중에 LDA와 w2V 해요 (튜플의리스트 목록) matricies); 그러나합니다 ('IDF'의 계산) 내 코퍼스으로 TFIDF 모델을 초기화 한 후 각 문서의 TFIDF를 참조 할 때 나는 다음과 같은 오류 메시지가 tfidf = models.TfidfModel(corpus)tfidf(corpus[5]) 형식 오류 : 'TfidfModel'객체가 호출되지 않습니다

나는 단지 각각 문장으로 구성된 4 개의 문서가있는 differnt corpus를 사용하여이 모델을 만들 수 있습니다. 여기에 예상 코퍼스 폼이 터플 목록 목록임을 확인할 수 있습니다 : [doc1 [(word1, count), (word2, count), ...], doc2 [(word3, count), (word4, 계산), ...] ...]

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

답변

0

, 대괄호 구문은 변환 래퍼를 형성하는 데 사용됩니다 코퍼스 주변에서 일종의 게으른 처리 파이프 라인을 형성합니다.

나는이 튜토리얼에서 메모를 읽을 때까지 그것을하지 않았다 : 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.

하지만 난 아직도 내가 완전히 기본 파이썬 목록 마법을 이해 생각하지 않습니다.

1

대신에 : tfidf(corpus[0])

시도 : @ whs2k의 대답에 확장 tfidf[corpus[0]]