Coherence Score验证LDA主题分类水平

from gensim.models import CoherenceModel
from gensim.corpora.dictionary import Dictionary
from gensim.models.ldamodel import LdaModel
from sklearn.feature_extraction.text import CountVectorizer
import pandas as pd
import nltk
from nltk.tokenize import word_tokenize
from multiprocessing import freeze_support

# 下载停用词和分词器所需的数据
#nltk.download('punkt')
nltk.download('stopwords')

def main():
    # 读取Excel文件
    data = pd.read_excel("Laos news overall.xlsx")

    # 假设您有一个名为 "cleaned_title" 的列,包含了新闻标题
    titles = data['内容'].tolist()

    # 初始化分词器和停用词
    stop_words = set(nltk.corpus.stopwords.words('english'))
    tokenizer = nltk.RegexpTokenizer(r'\w+')

    # 分词并去除停用词
    tokenized_titles = []
    for title in titles:
        words = tokenizer.tokenize(title)
        words = [word for word in words if word.lower() not in stop_words]
        tokenized_titles.append(words)

    # 创建字典
    dictionary = Dictionary(tokenized_titles)
    corpus = [dictionary.doc2bow(title) for title in tokenized_titles]

    # 训练 LDA 模型
    num_topics = 10
    lda = LdaModel(corpus, num_topics=num_topics, id2word=dictionary)

    # 计算 coherence score
    coherence_model = CoherenceModel(model=lda, texts=tokenized_titles, dictionary=dictionary, coherence='c_v')
    coherence_score = coherence_model.get_coherence()

    print(f"Coherence Score: {coherence_score}")

if __name__ == '__main__':
    freeze_support()
    main()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值