【问题标题】:LDA in Python, I get characters not topicsPython中的LDA,我得到的是字符而不是主题
【发布时间】:2023-03-10 20:09:01
【问题描述】:

我对在 Python 中执行 LDA 感到有些困惑。 我有一个文档文件,我想运行 LDA 并获取主题。

import docx
import nltk
import gensim
from gensim.models import hdpmodel, ldamodel
from gensim import corpora
def getText(filename):
    doc = docx.Document(filename)
    fullText = []
    for para in doc.paragraphs:
        fullText.append(para.text)
    return '\n'.join(fullText)
fullText=getText('ElizabethII.docx')

#create lda object
stoplist = set('for a of the and to in'.split())
texts = [[word for word in document.lower().split() if word not in stoplist]
         for document in fullText]

all_tokens = sum(texts, [])
tokens_once = set(word for word in set(all_tokens) if all_tokens.count(word) 
== 1)
texts = [[word for word in text if word not in tokens_once]
         for text in texts]

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

lda = ldamodel.LdaModel(corpus, id2word=dictionary, num_topics=5, passes=15)
topics = lda.show_topics(num_words=4)
for topic in topics:
    print(topic)
corpus_lda = lda[corpus]

print(lda.show_topics())

结果我得到了这个:

(0, '0.723*"r" + 0.211*"f" + 0.025*"5" + 0.013*"-"')
(1, '0.410*"e" + 0.258*"t" + 0.206*"h" + 0.068*"m"')
(2, '0.319*"n" + 0.162*"l" + 0.113*"c" + 0.101*"u"')
(3, '0.503*"i" + 0.324*"d" + 0.113*"b" + 0.041*"9"')
(4, '0.355*"o" + 0.307*"s" + 0.106*"w" + 0.052*"v"')

这让我很困惑。为什么我得到字符而不是主题?是因为我的 docx 文件(包含 1900 个单词?)还是代码错误?或者我应该为句子(段落)提供主题? (怎么做?)

【问题讨论】:

    标签: python nltk lda topic-modeling


    【解决方案1】:

    如下所示使用word_tokenize来准备由单词组成的token:

    from nltk.stem import WordNetLemmatizer
    from nltk.corpus import stopwords
    from nltk import word_tokenize 
    from gensim.models import ldamodel
    from gensim import corpora
    
    stop_words = set(stopwords.words('english'))
    Doc1 = 'Data science is an interdisciplinary field that uses scientific methods, processes, algorithms and systems to extract knowledge and insights from data in various forms, both structured and unstructured,[1][2] similar to data mining. '
    Doc2 = 'Biology is the natural science that studies life and living organisms, including their physical structure, chemical processes, molecular interactions, physiological mechanisms, development and evolution.'
    docs = [Doc1, Doc2]
    
    tokens = [word_tokenize(doc.lower()) for doc in docs]
    tokens = [[t for t in token_list if t not in stop_words] for token_list in tokens]
    word_l = WordNetLemmatizer()
    tokens = [[word_l.lemmatize(t) for t in token_list if t.isalpha()] for token_list in tokens]
    
    dictionary = corpora.Dictionary(tokens)
    corpus = [dictionary.doc2bow(t) for t in tokens]
    
    lda = ldamodel.LdaModel(corpus, id2word=dictionary, num_topics=5, passes=15)
    topics = lda.show_topics(num_words=4)
    for topic in topics:
        print(topic)
    
    (0, '0.029*"science" + 0.029*"process" + 0.029*"living" + 0.029*"organism"')
    (1, '0.114*"data" + 0.043*"scientific" + 0.043*"similar" + 0.043*"mining"')
    (2, '0.029*"science" + 0.029*"process" + 0.029*"living" + 0.029*"biology"')
    (3, '0.029*"process" + 0.029*"science" + 0.029*"living" + 0.029*"biology"')
    (4, '0.048*"process" + 0.048*"science" + 0.048*"evolution" + 0.048*"physical"')
    

    【讨论】:

    • 谢谢。但是这里的文档已经区分句子而不是全文(如维基页面)。所以我要在分词之前把文本分解成句子?
    • 并非如此。相同的代码也可以在单个文档上工作。如果是这种情况,请告诉我,我将再举一个例子
    • 我运行代码,但在结果中也发现了一些标点符号。我如何将它们作为停用词删除。我得到类似的东西:“=="
    • 我已经在一个文档中测试了我的答案中的代码,其中包含以下 = == - # } { [ } ] ; " & ! 符号。在处理文档后,它们都没有出现在 tokens 变量中。你真的应该将你的问题限制在你在 SO 上发布的问题的直接范围内。如果你仍然有标点符号问题,请发布一个新问题详细概述它们。
    猜你喜欢
    • 2015-06-27
    • 1970-01-01
    • 2020-03-26
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    相关资源
    最近更新 更多