2017-09-16 5 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)

私は1つの文書しかフィルタリングされていないため、除外されています。 this

関連する問題