2013-03-07 46 views
0

全部,Gensim主题打印错误/问题

这是我在this thread中回复的内容。我试图在gensim中打印LSI主题时遇到了一些棘手的结果。这是我的代码:

try: 
    from gensim import corpora, models 
except ImportError as err: 
    print err 

class LSI: 
    def topics(self, corpus): 
     tfidf = models.TfidfModel(corpus) 
     corpus_tfidf = tfidf[corpus] 
     dictionary = corpora.Dictionary(corpus) 
     lsi = models.LsiModel(corpus_tfidf, id2word=dictionary, num_topics=5) 
     print lsi.show_topics() 

if __name__ == '__main__': 
    data = '../data/data.txt' 
    corpus = corpora.textcorpus.TextCorpus(data) 
    LSI().topics(corpus) 

这将以下内容输出到控制台。

-0.804*"(5, 1)" + -0.246*"(856, 1)" + -0.227*"(145, 1)" + ...... 

我希望能够打印出像@ 2er0的主题做了over here但我得到这样的结果。请参阅下面的内容并注意打印的第二项是一个元组,我不知道它来自哪里。 data.txt是一个包含几个段落的文本文件。就这些。

对此的任何想法都是太棒了!亚当

回答

4

要回答为什么你的LSI主题是元组代替文字,请检查您输入的语料库。

它是从通过corpus = [dictionary.doc2bow(text) for text in texts]转换成语料库的文档列表创建的吗?

因为如果不是,你只是从序列化的语料库中读取它而不读取字典,那么你不会得到主题输出中的单词。

下面我的代码工作,并打印出的主题加权话:

import gensim as gs 

documents = ["Human machine interface for lab abc computer applications", 
      "A survey of user opinion of computer system response time", 
      "The EPS user interface management system", 
      "System and human system engineering testing of EPS", 
      "Relation of user perceived response time to error measurement", 
      "The generation of random binary unordered trees", 
      "The intersection graph of paths in trees", 
      "Graph minors IV Widths of trees and well quasi ordering", 
      "Graph minors A survey"] 

texts = [[word for word in document.lower().split()] for document in documents] 
dictionary = gs.corpora.Dictionary(texts) 
corpus = [dictionary.doc2bow(text) for text in texts] 

tfidf = gs.models.TfidfModel(corpus) 
corpus_tfidf = tfidf[corpus] 

lsi = gs.models.LsiModel(corpus_tfidf, id2word=dictionary, num_topics=5) 
lsi.print_topics() 

for i in lsi.print_topics(): 
    print i 

以上输出:

-0.331*"system" + -0.329*"a" + -0.329*"survey" + -0.241*"user" + -0.234*"minors" + -0.217*"opinion" + -0.215*"eps" + -0.212*"graph" + -0.205*"response" + -0.205*"time" 
-0.330*"minors" + 0.313*"eps" + 0.301*"system" + -0.288*"graph" + -0.274*"a" + -0.274*"survey" + 0.268*"management" + 0.262*"interface" + 0.208*"human" + 0.189*"engineering" 
0.282*"trees" + 0.267*"the" + 0.236*"in" + 0.236*"paths" + 0.236*"intersection" + -0.233*"time" + -0.233*"response" + 0.202*"generation" + 0.202*"unordered" + 0.202*"binary" 
-0.247*"generation" + -0.247*"unordered" + -0.247*"random" + -0.247*"binary" + 0.219*"minors" + -0.214*"the" + -0.214*"to" + -0.214*"error" + -0.214*"perceived" + -0.214*"relation" 
0.333*"machine" + 0.333*"for" + 0.333*"lab" + 0.333*"abc" + 0.333*"applications" + 0.258*"computer" + -0.214*"system" + -0.194*"eps" + -0.191*"and" + -0.188*"testing" 
+0

非常感谢您抽出时间来解决这个问题!我们现在很好... – aeupinhere 2013-03-12 02:58:46

0

它看起来丑陋,但这样做的工作(只是一个纯粹的基于字符串的方法):

#x = lsi.show_topics() 
x = '-0.804*"(5, 1)" + -0.246*"(856, 1)" + -0.227*"(145, 1)"' 
y = [(j.split("*")[0], (j.split("*")[1].split(",")[0].lstrip('"('), j.split("*")[1].split(",")[1].strip().rstrip(')"'))) for j in [i for i in x.strip().split(" + ")]] 

for i in y: 
    print y 

以上输出:

('-0.804', ('5', '1')) 
('-0.246', ('856', '1')) 
('-0.227', ('145', '1')) 

如果没有,你可以尝试lsi.print_topic(我),而不是lsi.show_topics()

for i in range(len(lsi.show_topics())): 
    print lsi.print_topic(i) 
+0

嘿@ 2er0。非常感谢你回答这个问题。在你的上面的答案中,当我得到实际的主题词时,我得到的数字是例如“(5,1)”。任何想法,为什么这是? – aeupinhere 2013-03-08 13:52:03

+0

您可以打印完整的代码,并显示您加载为“语料库”的文档。它认为这是因为你只是把一个矢量当作一个语料库而不是单词。 – alvas 2013-03-08 16:21:53

+0

我有一个直觉,你的语料库看起来像这样: '[(0,1),(2,2),(3,1),(4,1)]'没有一个字典,看起来像[(0 ,'狗'),(2,'the'),(3,'ate'),(4,'cat')] – alvas 2013-03-12 02:31:17