如果用jieba,做情感分析,用python示例,3.6版本
时间: 2024-04-29 10:24:36 浏览: 100
jieba分词-情感分析-词云例子.ipynb.zip_jieba_meantu2t_snownlp_情感分析
可以参考以下代码示例:
```python
import jieba
import jieba.analyse
import jieba.posseg as pseg
# 设置停用词
stop_words = set(['的', '了', '在', '是', '我', '你', '他', '她'])
# 分词和提取关键词
def preprocess(text):
# 去除标点符号和空格
text = text.replace('\n', '').replace('\r', '').replace('\t', '').replace(' ', '')
# 分词
words = pseg.cut(text)
# 提取名词和动词作为关键词
keywords = []
for word, flag in words:
if flag.startswith('n') or flag.startswith('v'):
if word not in stop_words:
keywords.append(word)
return keywords
# 加载情感词典
def load_dict(filename):
sents = {}
with open(filename, 'r', encoding='utf-8') as f:
for line in f.readlines():
s = line.strip().split('\t')
if len(s) == 1:
continue
word = s[0]
score = s[1]
if '-' in score:
score = score.split('-')[0]
else:
score = score.split('+')[0]
sents[word] = float(score)
return sents
# 计算情感得分
def sentiment_score(text, pos_dict, neg_dict):
words = preprocess(text)
pos_score, neg_score = 0, 0
for word in words:
if word in pos_dict:
pos_score += pos_dict[word]
if word in neg_dict:
neg_score += neg_dict[word]
return pos_score - neg_score
# 主函数
if __name__ == '__main__':
# 加载情感词典
pos_dict = load_dict('pos.txt')
neg_dict = load_dict('neg.txt')
# 示例
text = '这个电影真的很好看,演员演技都很棒,剧情也很精彩!'
score = sentiment_score(text, pos_dict, neg_dict)
print('情感得分:', score)
```
其中,pos.txt和neg.txt是情感词典文件,里面存储了正向和负向情感的词语及其得分。在本例中,我们使用jiba进行中文分词和提取关键词,然后根据情感词典计算情感得分。
阅读全文