2017-09-16 1 views
0

Ich versuche, das Q6-Rezept gezeigt here, aber mein Korpus immer wieder als [] zurückgegeben, obwohl ich überprüft habe und es scheint das Dokument richtig zu lesen.Gensim - Iterieren über mehrere Dokumente

So ist mein Code:

def iter_documents(top_directory): 
    """Iterate over all documents, yielding a document (=list of utf8 tokens) at a time.""" 
    for root, dirs, files in os.walk(top_directory): 
     for file in filter(lambda file: file.endswith('.txt'), files): 
      document = open(os.path.join(root, file)).read() # read the entire document, as one big string 
      yield utils.tokenize(document, lower=True) # or whatever tokenization suits you 
class MyCorpus(object): 
    # Used to create the object 
    def __init__(self, top_dir): 
     self.top_dir = top_dir 
     self.dictionary = corpora.Dictionary(iter_documents(top_dir)) 
     self.dictionary.filter_extremes(no_below=1, keep_n=30000) # check API docs for pruning params 
# Used if you ever need to iterate through the values 
def __iter__(self): 
    for tokens in iter_documents(self.top_dir): 
     yield self.dictionary.doc2bow(tokens) 

und die Textdatei I-Test bin ist this.

Antwort

0

Ok, ich habe es herausgefunden. Ändern Sie Zeile 12 zu diesem: self.dictionary.filter_extremes(no_below=0, no_above=1,keep_n=30000)

Da ich nur 1 Dokument zu Beginn mit es ist herausgefiltert werden. Siehe this