Python做文本情感分析之情感极性分析_python情感极性分析有哪些常用方法

1.2.2 去除停用词

遍历所有语料中的所有词语,删除其中的停用词
e.g. 这样/的/酒店/配/这样/的/价格/还算/不错
–> 酒店/配/价格/还算/不错

1.3 构建模型
1.3.1 将词语分类并记录其位置

将句子中各类词分别存储并标注位置。

`"""
2. 情感定位
"""
def classifyWords(wordDict):
 # (1) 情感词
 senList = readLines('BosonNLP\_sentiment\_score.txt')
 senDict = defaultdict()
 for s in senList:
 senDict[s.split(' ')[0]] = s.split(' ')[1]
 # (2) 否定词
 notList = readLines('notDict.txt')
 # (3) 程度副词
 degreeList = readLines('degreeDict.txt')
 degreeDict = defaultdict()
 for d in degreeList:
 degreeDict[d.split(',')[0]] = d.split(',')[1]
 
 senWord = defaultdict()
 notWord = defaultdict()
 degreeWord = defaultdict()
 
 for word in wordDict.keys():
 if word in senDict.keys() and word not in notList and word not in degreeDict.keys():
 senWord[wordDict[word]] = senDict[word]
 elif word in notList and word not in degreeDict.keys():
 notWord[wordDict[word]] = -1
 elif word in degreeDict.keys():
 degreeWord[wordDict[word]] = degreeDict[word]
 return senWord, notWord, degreeWord`
1.3.2 计算句子得分

在此,简化的情感分数计算逻辑:所有情感词语组的分数之和

定义一个情感词语组:两情感词之间的所有否定词和程度副词与这两情感词中的后一情感词构成一个情感词组,即notWords + degreeWords + sentiWords,例如不是很交好,其中不是为否定词,为程度副词,交好为情感词,那么这个情感词语组的分数为:
finalSentiScore = (-1) ^ 1 * 1.25 * 0.747127733968
其中1指的是一个否定词,1.25是程度副词的数值,0.747127733968交好的情感分数。

伪代码如下:
finalSentiScore = (-1) ^ (num of notWords) * degreeNum * sentiScore
finalScore = sum(finalSentiScore)

`"""
3. 情感聚合
"""
def scoreSent(senWord, notWord, degreeWord, segResult):
 W = 1
 score = 0
 # 存所有情感词的位置的列表
 senLoc = senWord.keys()
 notLoc = notWord.keys()
 degreeLoc = degreeWord.keys()
 senloc = -1
 # notloc = -1
 # degreeloc = -1
 
 # 遍历句中所有单词segResult&#x
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值