本文整理汇总了Python中gensim.models.ldamodel.LdaModel.get_topic_terms方法的典型用法代码示例。如果您正苦于以下问题:Python LdaModel.get_topic_terms方法的具体用法?Python LdaModel.get_topic_terms怎么用?Python LdaModel.get_topic_terms使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gensim.models.ldamodel.LdaModel
的用法示例。
在下文中一共展示了LdaModel.get_topic_terms方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: plottopicpop
# 需要导入模块: from gensim.models.ldamodel import LdaModel [as 别名]
# 或者: from gensim.models.ldamodel.LdaModel import get_topic_terms [as 别名]
def plottopicpop():
internet = [0 for i in range(10)]
developing = [0 for i in range(10)]
habr = [0 for i in range(10)]
n = 0
for year in range(2006, 2016):
articles, numberofarticles = getarticlesbyyear(year)
print("Got articles for", str(year))
# Normalaize texts
i = 0
for article in articles:
article = replacesymbols(article)
articles[i] = normalaisestr(article.lower())
i += 1
print('Normalaised')
# Remove unnecessary words
texts = [[word for word in article if word not in stoplist]
for article in articles]
print('Deleted stopwords')
dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
print('Starting training')
# Щадящий режим для ОЗУ
for i in range(numberofarticles // 100):
begin = 100 * i
end = 100 * (i + 1)
if end > numberofarticles:
end = numberofarticles
lda = LdaModel(corpus[begin:end:], id2word=dictionary, num_topics=end - begin)
for j in range(lda.num_topics):
topics = lda.get_topic_terms(j, 15)
# print(topics)
for topic in topics[0]:
top = dictionary.get(topic)
# print(top)
if "интернет" == top:
internet[n] += 1
if "разработка" == top:
developing[n] += 1
if "хабра" == top:
habr[n] += 1
del lda
n += 1
print(internet,'\n', developing, '\n', habr)
plt.title('Population of 3 topics.')
plt.xlabel('Year 2006 - 2015')
plt.ylabel('Number of articles')
plt.plot(internet, label="Интернет")
plt.plot(developing, label="Разработка")
plt.plot(habr, label="Хабра")
plt.legend()
plt.show()
示例2: ldaforhabr
# 需要导入模块: from gensim.models.ldamodel import LdaModel [as 别名]
# 或者: from gensim.models.ldamodel.LdaModel import get_topic_terms [as 别名]
def ldaforhabr():
numberofarticles = 0
articles, numberofarticles = getarticles()
print("Got articles")
# Normalaize texts
i = 0
for article in articles:
article = replacesymbols(article)
articles[i] = normalaisestr(article.lower())
i += 1
print('Normalaised')
# Remove unnecessary words
texts = [[word for word in article if word not in stoplist]
for article in articles]
print('Deleted stopwords')
dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
print('Starting training')
f = open('lda.log', 'w')
for i in range(i // numberofarticles):
begin = 100 * i
end = 100 * (i + 1)
if end > numberofarticles:
end = numberofarticles
lda = LdaModel(corpus[begin:end:], id2word=dictionary, num_topics=end - begin)
for j in range(lda.num_topics):
topics = lda.get_topic_terms(j, 15)
f.write(str(begin + j) + ": ")
# print(topics)
for topic in topics[0]:
top = dictionary.get(topic)
if top is not None:
f.write(top + '\n')
f.write('-----------\n')
# i += 1
del lda
f.close()