有大神会用python做网络评论文本的情感分析么?有偿
答案:1 悬赏:70 手机版
解决时间 2021-03-23 03:26
- 提问者网友:且恨且铭记
- 2021-03-22 17:39
有大神会用python做网络评论文本的情感分析么?有偿
最佳答案
- 五星知识达人网友:逃夭
- 2021-03-22 19:09
这个自学一会就会了,给你一个模型,自己研究一下,没那么难。
import jieba
import nltk.classify.util
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import names
def word_feats(words):
return dict([(word, True) for word in words])
text1 = open(r"积极.txt", "r").read()
seg_list = jieba.cut(text1)
result1 = " ".join(seg_list)
text2 = open(r"消极.txt", "r").read()
seg_list = jieba.cut(text2)
result2 = " ".join(seg_list)
# 数据准备
positive_vocab =result1
negative_vocab =result2
# 特征提取
positive_features = [(word_feats(pos), 'pos') for pos in positive_vocab]
negative_features = [(word_feats(neg), 'neg') for neg in negative_vocab]
train_set = negative_features + positive_features
# 训练模型
classifier = NaiveBayesClassifier.train(train_set)
# 实战测试
neg = 0
pos = 0
sentence = input("请输入一句你喜欢的话:")
sentence = sentence.lower()
seg_list = jieba.cut(sentence)
result1 = " ".join(seg_list)
words = result1.split(" ")
for word in words:
classResult = classifier.classify(word_feats(word))
if classResult == 'neg':
neg = neg + 1
if classResult == 'pos':
pos = pos + 1
print('积极:' + str(float(pos) / len(words)))
print('消极: ' + str(float(neg) / len(words)))
import jieba
import nltk.classify.util
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import names
def word_feats(words):
return dict([(word, True) for word in words])
text1 = open(r"积极.txt", "r").read()
seg_list = jieba.cut(text1)
result1 = " ".join(seg_list)
text2 = open(r"消极.txt", "r").read()
seg_list = jieba.cut(text2)
result2 = " ".join(seg_list)
# 数据准备
positive_vocab =result1
negative_vocab =result2
# 特征提取
positive_features = [(word_feats(pos), 'pos') for pos in positive_vocab]
negative_features = [(word_feats(neg), 'neg') for neg in negative_vocab]
train_set = negative_features + positive_features
# 训练模型
classifier = NaiveBayesClassifier.train(train_set)
# 实战测试
neg = 0
pos = 0
sentence = input("请输入一句你喜欢的话:")
sentence = sentence.lower()
seg_list = jieba.cut(sentence)
result1 = " ".join(seg_list)
words = result1.split(" ")
for word in words:
classResult = classifier.classify(word_feats(word))
if classResult == 'neg':
neg = neg + 1
if classResult == 'pos':
pos = pos + 1
print('积极:' + str(float(pos) / len(words)))
print('消极: ' + str(float(neg) / len(words)))
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯