【问题标题】:How to access topic words only in gensim如何仅在 gensim 中访问主题词
【发布时间】:2018-03-14 03:38:01
【问题描述】:

我使用 Gensim 构建了 LDA 模型,我只想获取主题词我怎样才能只获取主题词,没有概率,也没有 IDs.words

我在 gensim 中尝试了 print_topics() 和 show_topics() 函数,但我无法获得干净的单词!

这是我使用的代码

dictionary = corpora.Dictionary(doc_clean)
doc_term_matrix = [dictionary.doc2bow(doc) for doc in doc_clean]
Lda = gensim.models.ldamodel.LdaModel
ldamodel = Lda(doc_term_matrix, num_topics=12, id2word = dictionary, passes = 100, alpha='auto', update_every=5)
x = ldamodel.print_topics(num_topics=12, num_words=5)
for i in x:
    print(i[1])
    #print('\n' + str(i))

0.045*تعرض + 0.045*الماضية + 0.045*السنوات + 0.045*وءسرته + 0.045*لءحمد
0.021*مصر + 0.021*الديمقراطية + 0.021*حرية + 0.021*باسم + 0.021*الحكومة
0.068*المواطنة + 0.068*الطاءفية + 0.068*وانهيارات + 0.068*رابطة + 0.005*طبول
0.033*عربية + 0.033*انكسارات + 0.033*رهابيين + 0.033*بحقوق + 0.033*ل
0.007*وحريات + 0.007*ممنهج + 0.007*قواءم + 0.007*الناس + 0.007*دراج
0.116*طبول + 0.116*الوطنية + 0.060*يكتب + 0.060*مصر + 0.005*عربية
0.064*قيم + 0.064*وهن + 0.064*عربيا + 0.064*والتعددية + 0.064*الديمقراطية
0.036*تضامنا + 0.036*الشخصية + 0.036*مع + 0.036*التفتيش + 0.036*الءخلاق
0.052*تضامنا + 0.052*كل + 0.052*محمد + 0.052*الخلوق + 0.052*مظلوم
0.034*بمواطنين + 0.034*رهابية + 0.034*لم + 0.034*عليهم + 0.034*يثبت
0.035*مع + 0.035*ومستشار + 0.035*يستعيدا + 0.035*ءرهقهما + 0.035*حريتهما
0.064*للقمع + 0.064*قريبة + 0.064*لا + 0.064*نهاية + 0.064*مصر

我尝试了 show_topics,它给出了相同的输出

y = np.array(ldamodel.show_topics(num_topics=12, num_words=5))
for i in y[:,1]:
    #if i != '%d':
    #print([str(word) for word in i])
    print(i)

如果我有主题 ID,我如何访问它的单词和其他信息

提前致谢

【问题讨论】:

    标签: python nlp gensim lda topic-modeling


    【解决方案1】:

    我认为下面的代码 sn-p 应该为您提供一个元组列表,其中包含该主题中的每个主题(tp)和相应的单词列表(wd)

    x=ldamodel.show_topics(num_topics=12, num_words=5,formatted=False)
    topics_words = [(tp[0], [wd[0] for wd in tp[1]]) for tp in x]
    
    #Below Code Prints Topics and Words
    for topic,words in topics_words:
        print(str(topic)+ "::"+ str(words))
    print()
    
    #Below Code Prints Only Words 
    for topic,words in topics_words:
        print(" ".join(words))
    

    【讨论】:

      【解决方案2】:

      另一个答案是给出一个与每个单词相关的权重字符串。但是,如果您想分别获取主题中的每个单词以进行进一步的工作。然后你可以试试这个。这里的主题号是字典的键,值是一个字符串,包含该主题中的所有单词,用空格分隔

      x=ldamodel.show_topics()
      
      twords={}
      for topic,word in x:
          twords[topic]=re.sub('[^A-Za-z ]+', '', word)
      print(twords)
      

      【讨论】:

        【解决方案3】:

        假设你的模型叫做ldamodel

        my_dict = {'Topic_' + str(i): [token for token, score in ldamodel.show_topic(i, topn=10)] for i in range(0, ldamodel.num_topics)}
        

        我们得到(2 个主题):

        print(my_dict)
        
        {'Topic_0': ['excel',
          'data',
          'learn',
          'feedback',
          'coaching',
          'tips',
          'digital',
          'use',
          'team',
          'people'],
         'Topic_1': ['leadership',
          'decisions',
          'business',
          'agile',
          'people',
          'change',
          'global',
          'data',
          'team',
          'leaders']}
        

        或者my_dict['Topic_0'],我们得到:

        ['excel',
         'data',
         'learn',
         'feedback',
         'coaching',
         'tips',
         'digital',
         'use',
         'team',
         'people']
        

        【讨论】:

          【解决方案4】:

          您可以在 gensim 中使用 get_topic_terms() 代替 print_topics() 和 show_topics() 函数。

          假设您有以下 2 个变量:id2wordlda_model,它们的定义如下:

          corpus_words = [['term1', 'term_2'], ['term3', 'term4']]
          id2word = gensim.corpora.Dictionary(corpus_words)
          corpus = [id2word.doc2bow(text) for text in corpus_words]
          lda_model = gensim.models.LdaMulticore(corpus=corpus, id2word=id2word, num_topics=2)
          

          通过调用 get_topic_terms()

          [ lda_model.get_topic_terms(tid, topn=3)] for tid in range(2) ]
          

          你得到了 3 个单词的 id 和它们在 2 个主题中的每一个的分数。

          那么,需要以下内容:

          [ [(id2word[wid], s) for (wid, s) in lda_model.get_topic_terms(tid, topn=3)] for tid in range(2)]
          
          [[('term1', 0.32463402), ('term_2', 0.3211307), ('term4', 0.18077125)], 
           [('term3', 0.3250474), ('term4', 0.31788236), ('term_2', 0.18025273)]]
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-08-08
            • 1970-01-01
            • 2017-07-06
            • 1970-01-01
            • 1970-01-01
            • 2013-02-22
            • 2013-07-13
            • 1970-01-01
            相关资源
            最近更新 更多