2014-06-12 4 views
2

나는 을 사용하여 문서 모음에서 사전을 빌드합니다. 각 문서는 토큰 목록입니다. 이 내 코드gensim 사전에 토큰을 추가하는 방법

def constructModel(self, docTokens): 
    """ Given document tokens, constructs the tf-idf and similarity models""" 

    #construct dictionary for the BOW (vector-space) model : Dictionary = a mapping between words and their integer ids = collection of (word_index,word_string) pairs 
    #print "dictionary" 
    self.dictionary = corpora.Dictionary(docTokens) 

    # prune dictionary: remove words that appear too infrequently or too frequently 
    print "dictionary size before filter_extremes:",self.dictionary#len(self.dictionary.values()) 
    #self.dictionary.filter_extremes(no_below=1, no_above=0.9, keep_n=100000) 
    #self.dictionary.compactify() 

    print "dictionary size after filter_extremes:",self.dictionary 

    #construct the corpus bow vectors; bow vector = collection of (word_id,word_frequency) pairs 
    corpus_bow = [self.dictionary.doc2bow(doc) for doc in docTokens] 


    #construct the tf-idf model 
    self.model = models.TfidfModel(corpus_bow,normalize=True) 
    corpus_tfidf = self.model[corpus_bow] # first transform each raw bow vector in the corpus to the tfidf model's vector space 
    self.similarityModel = similarities.MatrixSimilarity(corpus_tfidf) # construct the term-document index 

제 질문은이 사전에 새 문서 (토큰)를 추가하고 업데이트하는 방법입니다. 나는 gensim 문서에서 검색 그러나 나는 해결책을 찾을 수 없습니다

답변

6

gensim 웹 페이지 here

에 병합 한 다음 새 문서를 다른 사전을 만들고 있습니다 할 수있는 방법을이 작업을 수행하는 방법에 대한 문서가있다 그들.

from gensim import corpora 

dict1 = corpora.Dictionary(firstDocs) 
dict2 = corpora.Dictionary(moreDocs) 
dict1.merge_with(dict2) 

문서에 따르면, 동일한 토큰과 동일한 토큰을 새로운 ID에 매핑합니다.

0

당신은 add_documents 방법을 사용할 수 있습니다 위의 코드를 실행

from gensim import corpora 
text = [["aaa", "aaa"]] 
dictionary = corpora.Dictionary(text) 
dictionary.add_documents([['bbb','bbb']]) 
print(dictionary) 

후를, 당신이 얻을 것이다 :

Dictionary(2 unique tokens: ['aaa', 'bbb']) 

은 자세한 내용은 document을 읽어보십시오.

관련 문제