简体   繁体   中英

gensim Generating LSI model causes “Python has stopped working”

So I am trying to use gensim to generate an LSI model along with corpus_lsi following this tutorial.

I start with a corpus and a dictionary that I generated myself. The list of documents are too small (9 lines = 9 documents), which is the sample list provided in gensim tutorials

However, pythos just crashes when it reaches the line for generating LSI_model. You can see below my code along with the generated output

Code

#!/usr/bin/env python
import os
from gensim import corpora, models, similarities
import logging

#logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)

if __name__ == '__main__':
    if (os.path.exists("tmp\dictionary.dict")):
        dictionary = corpora.Dictionary.load('tmp\dictionary.dict')
        corpus = corpora.MmCorpus('tmp\corpus.mm')
        print("Used files generated Dataset Generator")
    else:
        print("Please run dataset generator")

print ("generating tf-idf model ...")
tfidf = models.TfidfModel(corpus)   # Generate tfidf matrix (tf-idf model)
print ("generating corpus_tf-idf model ...")
corpus_tfidf = tfidf[corpus]    #use the model to transform vectors

print ("generating LSI model ...")
lsi = models.LsiModel(corpus_tfidf, id2word=dictionary, num_topics=2) # initialize an LSI transformation
print ("generating corpus_lsi model ...")
corpus_lsi = lsi[corpus_tfidf] # create a double wrapper over the original corpus: bow->tfidf->fold-in-lsi

lsi.print_topics(2)

Output

Used files generated Dataset Generator
generating tf-idf model ...
generating corpus_tf-idf model ...
generating LSI model ...

After printing "generating LSI model" it crashes

Any suggestions ?

Other things I tried

  • Changing python version to python 2.6
  • Removing gensim and installing it again from github (instead of conda)

It seems that the issue was the function used in the tutorial (maybe downgraded or something)

so I changed the line

lsi = models.LsiModel(corpus_tfidf, id2word=dictionary, num_topics=2) # initialize an LSI transformation

To

lsi = LsiModel(corpus_tfidf,num_topics=2)

And it actually worked fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM