2017-09-16 52 views
0

我想要顯示的Q6食譜here但我的語料庫不斷返回爲[],儘管我已經檢查過它似乎正在正確讀取文檔。Gensim - 迭代多個文檔

所以我的代碼是:

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) 

和我使用的文本文件來測試this

回答

0

好吧我想通了。將第12行更改爲:self.dictionary.filter_extremes(no_below=0, no_above=1,keep_n=30000)

因爲我只有一個文檔開始時將其過濾掉。 見this