码迷,mamicode.com
首页 > 其他好文 > 详细

粗糙的情感分析

时间:2017-12-05 21:15:55      阅读:188      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!