简体   繁体   English

如何仅在gensim中访问主题词

[英]How to access topic words only in gensim

I built LDA model using Gensim and I want to get the topic words only How can I get the words of the topics only no probabilities and no IDs.words only我使用 Gensim 构建了 LDA 模型,我只想获取主题词如何仅获取主题词没有概率也没有 IDs.words

I tried print_topics() and show_topics() functions in gensim but I can't get clean words !我在 gensim 中尝试了 print_topics() 和 show_topics() 函数,但我找不到干净的词!

This is the code I used这是我使用的代码

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*مصر

I tried show_topics and it gave the same output我试过 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)

If I have the topic ID how can I access its words and other informations如果我有主题 ID,我如何访问它的单词和其他信息

Thanks in Advance提前致谢

I think the below code snippet should give you a list of tuples containing the each topic(tp) and corresponding list of words(wd) in that topic我认为下面的代码片段应该为您提供一个包含每个主题(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))

The other answer was giving a string with weights associated with each word.另一个答案是给出一个与每个单词相关的权重的字符串。 But if you want to get each word in a topic separately for further work.但是,如果您想单独获取主题中的每个单词以供进一步工作。 Then you can try this.那你可以试试这个。 Here topic no is the key to the dictionary and the value is a single string containing all words in that topic separated by space这里的主题 no 是字典的键,值是一个字符串,包含该主题中以空格分隔的所有单词

x=ldamodel.show_topics()

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

Assuming that your model is called ldamodel :假设您的模型名为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)}

And we get (for 2 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']}

Or my_dict['Topic_0'] and we get:或者my_dict['Topic_0']我们得到:

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

You could use get_topic_terms() in gensim instead of print_topics() and show_topics() functions.您可以在 gensim 中使用get_topic_terms()而不是 print_topics() 和 show_topics() 函数。

Assume you have the following 2 variables: id2word and lda_model , where they were defined as follows:假设您有以下 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)

By calling get_topic_terms() :通过调用get_topic_terms()

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

you got 3 words' ids and their scores for each of the 2 topics.你有 3 个单词的 id 和它们对 2 个主题中的每一个的分数。

Then, the following would be what are needed:然后,以下将是需要的:

[ [(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)]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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