python - 如何仅在gensim中访问主题词

标签 python nlp gensim lda topic-modeling

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

我在 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,我如何访问它的词和其他信息

提前致谢

最佳答案

我认为下面的代码片段应该给你一个元组列表,其中包含每个主题 (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))

关于python - 如何仅在gensim中访问主题词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46536132/

相关文章:

python - Word2Vec 和 Gensim 参数等价

python - 在 Python 中将进程作为线程与网络一起使用

python - 为什么 python SSL 模块无法验证 graph.facebook.com 证书?

python - 如何使用经过训练的 skipgram 模型预测单词?

nlp - 什么是共现矩阵以及它们如何在 NLP 中使用?

python - NLP - 如何添加更多功能?

python-2.7 - 如何将 gensim LDA 主题输出与分数一起保存到 csv?

python - 将日志级别设置为 logging.DEBUG 或 logging.INFO 无效

python - Django 正则表达式不匹配