Python 基于Snownlp的情感分析(简单)

from snownlp import SnowNLP
# 需要操作的句子
file=open('text2.txt',mode='r')
str=file.readline()
str=str.replace('。','。\n')
ls=str.split('\n')
print(ls)
def word_seg(str):
    ls1 = SnowNLP(str).words
    res = []
    ls2 = [x.strip() for x in open('stopword.txt', encoding='utf-8', mode='r').readlines()]
    for i in ls1:
        if i not in ls2:
            res.append(i)
    return res

def Sentiments_anly(ls):
    res = []
    for i in ls:
        if len(i) > 1:
            value = SnowNLP(i).sentiments
            if value > 0.6:
                s = "pos"
            else:
                s = 'nav'
            res.append((i, s))
    return res

ls=word_seg(str)
print(Sentiments_anly(ls))

 

你可能感兴趣的:(python)