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