请参阅首先从评论中提取副词和形容词,例如:
import nltk
from nltk.tokenize import sent_tokenize, word_tokenize
import csv
para = "What can I say about this place. The staff of the restaurant is nice and the eggplant is not bad. Apart from that, very uninspired food, lack of atmosphere and too expensive. I am a staunch vegetarian and was sorely dissapointed with the veggie options on the menu. Will be the last time I visit, I recommend others to avoid"
sentense = word_tokenize(para)
word_features = []
for i,j in nltk.pos_tag(sentense):
if j in ['JJ', 'JJR', 'JJS', 'RB', 'RBR', 'RBS']:
word_features.append(i)
rating = 0
for i in word_features:
with open('words.txt', 'rt') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
if i == row[0]:
print i, row[1]
if row[1] == 'pos':
rating = rating + 1
elif row[1] == 'neg':
rating = rating - 1
print rating
现在您必须有一个外部 csv 文件,其中应该有正面和负面的词
喜欢 : 皱纹,neg wrinkled,neg 皱纹,neg masterly,pos 杰作,pos 杰作,pos
上述脚本的工作如下:
1. 阅读第 2 句。提取副词和形容词 3.比较 CVS 的正面和负面词 4。然后给句子打分
上述脚本的结果是:
nice pos
bad neg
expensive neg
sorely neg
-2
根据您的需要更改结果。对不起我的英语:P