1. 程式人生 > >粗糙的情感分析

粗糙的情感分析

strip urn motion nbsp nco else pri return 情感

import jieba
def judodd(num):
if (num % 2) == 0:
return "enev"
else:
return "odd"
def open_dict(Dict = "hhh",path = "D:\\"):
path = path + "%s.txt" % Dict
dictionary = open(path,"r",encoding = "utf8")
dict = []
for word in dictionary:
word = word.strip("\n")
dict.append(word)
return dict
deny_word = open_dict(Dict = "否定詞",path = "D:\\")
posword = open_dict(Dict = "positive", path = "D:\\")
negword = open_dict(Dict = "negative",path = "D:\\")
def emotion(file):
seg_sentence = file.split("。")
for sen in seg_sentence:
segtmp = jieba.lcut(sen,cut_all = False)
print(segtmp)
i = 0
a = 0
for word in segtmp:
if word in posword:
count = 1
c = 0
for w in segtmp[a:i]:
if w in deny_word:
c += 1
if judodd(c) == "odd":
count *= -1
else:
count = 1
a = i + 1
elif word in negword:
count = -1
c = 0
for w in segtmp[a:i]:
if w in deny_word:
c += 1
if judodd(c) == "odd":
count *= -1
else:
count = -1
a = i + 1
else:
count = 0
i += 1
print(sen,count)
file = "我好開心啊,非常非常非常高興!今天我得了一百分,我很興奮開心,愉快,開心"
emotion(file)

粗糙的情感分析