清洗文本高频词、情感分析、情感分类、主题建模挖掘主题

import pandas as pd

import re

import nltk

from nltk import FreqDist

from nltk.sentiment.vader import SentimentIntensityAnalyzer

from nltk.tokenize import word_tokenize

import spacy

from spacy.lang.en.stop_words import STOP_WORDS

from gensim.corpora import Dictionary

from gensim.models import LdaModel

下载NLTK的停用词、情感分析和词性标注所需的资源

nltk.download('stopwords')

nltk.download('punkt')

nltk.download('vader_lexicon')

加载SpaCy的英文NLP模型

nlp = spacy.load("en_core_web_sm")

读取Excel文件

df = pd.read_excel('nltk分词处理结果第二次.xlsx')

定义文本清洗函数

def clean_text(text):

去除HTML标签

cleaned_text = re.sub(r'<.*?>', '', text)

去除多余空格和换行符

cleaned_text = re.sub(r'\s+', ' ', cleaned_text)

转换为小写

cleaned_text = cleaned_text.lower()

return cleaned_text

清洗文本数据

df['cleaned_content'] = df['content'].apply(clean_text)

词频分析

words = []

for text in df['cleaned_content']:

words += word_tokenize(text)

freq_dist = FreqDist(words)

print("词频分析结果:", freq_dist.most_common(10))

情感分析

sia = SentimentIntensityAnalyzer()

df['sentiment_score'] = df['cleaned_content'].apply(lambda x: sia.polarity_scores(x)['compound'])

print("情感分析结果:", df['sentiment_score'])

定义阈值

positive_threshold = 0.5

negative_threshold = -0.5

根据情感分数进行分类

def classify_sentiment(score):

if score > positive_threshold:

return '积极'

elif score < negative_threshold:

return '消极'

else:

return '中性'

应用分类函数,创建新的列 'sentiment_category'

df['sentiment_category'] = df['sentiment_score'].apply(classify_sentiment)

输出带有情感分类的数据

print(df[['cleaned_content', 'sentiment_score', 'sentiment_category']])

主题建模

tokens = [[token.text.lower() for token in nlp(text) if token.is_alpha and token.text.lower() not in STOP_WORDS] for text in df['cleaned_content']]

dictionary = Dictionary(tokens)

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

lda_model = LdaModel(corpus, num_topics=5, id2word=dictionary, passes=15)

topics = lda_model.print_topics(num_words=5)

print("主题建模结果:")

for topic in topics:

print(topic)

相关推荐
腾视科技1 小时前
腾视科技TS-SG-SM7系列AI算力模组:32TOPS算力引擎,开启边缘智能新纪元
人工智能·科技
极新1 小时前
深势科技生命科学高级业务架构师孟月:AI4S 赋能生命科学研发,数智化平台的实践与落地 | 2025极新AIGC峰会演讲实录
人工智能
Light606 小时前
破局而立:制造业软件企业的模式重构与AI赋能新路径
人工智能·云原生·工业软件·商业模式创新·ai赋能·人机协同·制造业软件
Quintus五等升6 小时前
深度学习①|线性回归的实现
人工智能·python·深度学习·学习·机器学习·回归·线性回归
natide6 小时前
text-generateion-webui模型加载器(Model Loaders)选项
人工智能·llama
野生的码农6 小时前
码农的妇产科实习记录
android·java·人工智能
TechubNews6 小时前
2026 年观察名单:基于 a16z「重大构想」,详解稳定币、RWA 及 AI Agent 等 8 大流行趋势
大数据·人工智能·区块链
脑极体7 小时前
机器人的罪与罚
人工智能·机器人
三不原则7 小时前
故障案例:容器启动失败排查(AI运维场景)——从日志分析到根因定位
运维·人工智能·kubernetes
点云SLAM7 小时前
凸优化(Convex Optimization)理论(1)
人工智能·算法·slam·数学原理·凸优化·数值优化理论·机器人应用