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

필터링 할 문서가 하나 밖에 없기 때문에 12 행으로 변경하십시오. this