基于LDA的评论大数据的分析及主题建模

1.微博的关键词大数据采集;

已完成,待优化

2.LDA

错误1:使用了import pyLDAvis.sklearn,提示没有模块no module named 'pyldavis.sklearn'

默认安装 pyLDAvis==3.4.1,最后降级处理,解决方式:

 pip install pyLDAvis==3.2.2

错误2: return vectorizer.get_feature_names()
AttributeError: 'CountVectorizer' object has no attribute 'get_feature_names'

解决方式:修改为:return vectorizer.get_feature_names_out()

错误3:

pyLDAvis\_prepare.py", line 247, in _topic_info
    default_term_info = default_term_info.sort_values(
TypeError: drop() takes from 1 to 2 positional arguments but 3 were given

解决方式:

修改_prepare.py文件 ,
将248行代码改为drop(‘saliency’, 1) ==> drop(‘saliency’, axis=1)

错误4:OSError: [Errno 22] Invalid argument: 'https://cdn.jsdelivr.net/gh/bmabey/py

修改报错处,即_display.py的227.py,local=True改为local=False


参考链接:LDA代码训练报错记录_typeerror: drop() takes from 1 to 2 positional arg-CSDN博客

pyLDAvis生成LDA主题并可视化_py ldavis 库-CSDN博客

pyLDAvis实现LDA结果可视化时报错OSError:invalid argument_python pyldavis.show() 报错-CSDN博客

已解决AttributeError: ‘CountVectorizer‘ object has no attribute ‘get_feature_names‘_countvectorizer' object has no attribute 'get_feat-CSDN博客

整出LDA图,流程跑出来了,待优化。(数据清洗,调参,其他方法,需要优化)

  • 10
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先需要明确的是,LDA(Latent Dirichlet Allocation)是一种主题模型,不是一种情感分析方法。但是可以在LDA模型的基础上进行情感分析。下面是一个基于LDA的中文文本情感分析代码示例: 1. 数据预处理 首先需要对中文文本进行分词、去停用词等预处理操作。这里使用jieba分词库和stopwords中文停用词库。 ```python import jieba import codecs # 加载中文停用词库 with codecs.open('stopwords.txt','r',encoding='utf8') as f: stopwords = [line.strip() for line in f] # 对文本进行分词和去停用词处理 def cut_stop_words(text): words = jieba.cut(text) return [word for word in words if word not in stopwords] ``` 2. LDA模型训练 使用gensim库进行LDA模型训练。 ```python import gensim from gensim import corpora # 加载预处理后的文本 with codecs.open('data.txt','r',encoding='utf8') as f: texts = [cut_stop_words(line.strip()) for line in f] # 构建词典和语料库 dictionary = corpora.Dictionary(texts) corpus = [dictionary.doc2bow(text) for text in texts] # 训练LDA模型 lda_model = gensim.models.ldamodel.LdaModel(corpus=corpus, id2word=dictionary, num_topics=10) ``` 3. 情感分析 基于LDA模型的主题分布,可以对文本进行情感分析。这里使用snownlp库进行情感分析。 ```python import snownlp # 对每个文本进行情感分析 def sentiment_analysis(text): topic_dist = lda_model.get_document_topics(dictionary.doc2bow(cut_stop_words(text)), minimum_probability=0.0) positive_prob = 0.0 negative_prob = 0.0 for topic_id, prob in topic_dist: topic_words = [word for word, _ in lda_model.show_topic(topic_id)] topic_text = ' '.join(topic_words) sentiment = snownlp.SnowNLP(topic_text).sentiments if sentiment > 0.5: positive_prob += prob else: negative_prob += prob if positive_prob > negative_prob: return 'positive' elif positive_prob < negative_prob: return 'negative' else: return 'neutral' ``` 以上就是一个基于LDA的中文文本情感分析代码示例。需要注意的是,LDA模型训练需要较大的文本语料库,并且情感分析的准确度也受到LDA模型的影响。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值