粗糙的情感分析2
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("\n")
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 = open("E:\\python练习文件\\2.txt","r",encoding = "utf8").readlines()
for n in range(0,100):
emotion(file[n])
浙公网安备 33010602011771号