【问题标题】:Print only topic name using LDA with python使用带有 python 的 LDA 仅打印主题名称
【发布时间】:2018-06-23 13:02:36
【问题描述】:

我只需要打印主题词(只有一个词)。但它包含一些数字,但我不能只得到像“快乐”这样的主题名称。我的字符串词是“Happy”,为什么它显示“Happi”

    import warnings
warnings.filterwarnings(action='ignore', category=UserWarning, module='gensim')
from nltk.tokenize import RegexpTokenizer
from stop_words import get_stop_words
from nltk.stem.porter import PorterStemmer
from gensim import corpora, models

import gensim
import string
tokenizer = RegexpTokenizer(r'\w+')
en_stop = get_stop_words('en')
p_stemmer = PorterStemmer()
fr = open('Happy DespicableMe.txt','r')
doc_a = fr.read()
fr.close()
doc_set = [doc_a]
texts = []
for i in doc_set:


    raw = i.lower()
    tokens = tokenizer.tokenize(raw)


    stopped_tokens = [i for i in tokens if not i in en_stop]


    stemmed_tokens = [p_stemmer.stem(i) for i in stopped_tokens]


    texts.append(stemmed_tokens)


dictionary = corpora.Dictionary(texts)


corpus = [dictionary.doc2bow(text) for text in texts]


ldamodel = gensim.models.ldamodel.LdaModel(corpus, num_topics=1, id2word = dictionary, passes=20)



rafa = ldamodel.show_topics(num_topics=1, num_words=1, log=False , formatted=False)


print(rafa)

它只显示 [(0, '0.142*"happi"')]。但我只想打印单词。

【问题讨论】:

  • 粘贴.txt文件的内容。
  • 它包含一首歌的歌词。
  • @S.Kablar 更新
  • 这个答案对你有帮助吗?

标签: python-3.x nltk gensim lda stemming


【解决方案1】:

你被误会困扰:

词干提取通过一系列转换规则去除常见的后缀和前缀来提取词干。实际上,生成的词干不一定是实际的英语单词。词干提取的目的是标准化单词以进行比较。例如

stem_word('happy') == stem_word('happier')

您需要一个 Lemmatizer(例如 nltk.stem.wordnet)来查找引理。引理与词干的不同之处在于,引理是单词的规范形式,而词干可能不是真正的单词。

拥有install the corpus/wordnet之后,你可以这样使用它:

from nltk.corpus import wordnet
syns = wordnet.synsets("happier")
print(syns[0].lemmas()[0].name())

输出:

happy

【讨论】:

  • 有什么办法可以得到实际的单词。
  • @Rafi 是的,补充说。
猜你喜欢
  • 2023-04-06
  • 2013-06-04
  • 1970-01-01
  • 2022-11-18
  • 2019-03-15
  • 2017-12-12
  • 2017-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多