标签:int rip 分析 for false open += coding print
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)
标签:int rip 分析 for false open += coding print
原文地址:http://www.cnblogs.com/dy2017python/p/7989270.html