机器学习之情感分析

本文探讨了机器学习中的情感分析技术,通过代码实现展示了如何进行情感判断,从而帮助理解文本情绪。
摘要由CSDN通过智能技术生成

情感分析

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
# sent.py
import nltk.corpus as nc
import nltk.classify as cf
import nltk.classify.util as cu
pdata = []
fileids = nc.movie_reviews.fileids('pos')
# print(fileids)
for fileid in fileids:
    feature = {}
    words = nc.movie_reviews.words(fileid)
    for word in words:
        feature[word] = True
    pdata.append((feature, 'POSITIVE'))
ndata = []
fileids = nc.movie_reviews.fileids('neg')
# print(fileids)
for fileid in fileids:
    feature = {}
    words = nc.movie_reviews.words(fileid)
    for word in words:
        feature[word] = True
    ndata.append((feature, 'NEGATIVE'))
pnumb, nnumb = int(len(pdata) * 0.8), int(len(ndata) * 0.8)
train_data = pdata[:pnumb] + ndata[:nnumb]
test_data = pdata[pnumb:] + ndata[nnumb:]
model = cf.NaiveBayesClassifier.train(train_data)
model = cf.NaiveBayesClassifier.train(train_data)
ac = cu.accuracy(model, test_data)
print(ac)
tops = model.most_informative_features()
for top in tops[:10]:
    print(top[0])
reviews = [
    'It is an amazing movie.',
    'This is a dull movie. I would never recommend it to anyone.',
    'The cinematography is pretty great in this move.',
    'The direction was terrible and the story was all over the place.']
sents, probs = [], []
for review in reviews:
    feature = {}
    words = review.split()
    for word in words:
        feature[word] = True
    pcls = model.prob_classify(feature)
    sent = pcls.max()
    prob = pcls.prob(sent)
    sents.append(sent)
    probs.append(prob)
for review, sent, prob in zip(reviews, sents, probs):
    print(review, '->', sent, '%.2f%%' % round(prob * 100, 2))

code result

0.735
outstanding
insulting
vulnerable
ludicrous
uninvolving
astounding
avoids
fascination
anna
animators
It is an amazing movie. -> POSITIVE 63.16%
This is a dull movie. I would never recommend it to anyone. -> NEGATIVE 76.52%
The cinematography is pretty great in this move. -> POSITIVE 68.67%
The direction was terrible and the story was all over the place. -> NEGATIVE 67.03%
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值