# -*- coding: utf-8 -*-
"""
author = 'suxuer'
date =
"""
import jieba
import jieba.posseg as pseg
import sys
import importlib
print("加载用户词典...")
importlib.reload(sys)
#sys.setdefaultencoding('utf8') #py没有这个
#下载需要分词的文件
jieba.load_userdict('G:/project/python_pachong/src/emotion_dict/pos_all_dict.txt')
jieba.load_userdict('G:/project/python_pachong/src/emotion_dict/neg_all_dict.txt')
#分词,返回List
def segmentation(sentence):
seg_list = jieba.cut(sentence)
seg_result = []
for word in seg_list:
seg_result.append(word)
return seg_result
# 分词,词性标注,词和词性构成一个元组
def postagger(sentence):
pos_data = pseg.cut(sentence)
pos_list = []
for w in pos_data:
pos_list.append((w.word, w.flag))
print (pos_list[:])
return pos_list
# 句子切分
def cut_sentence(words):
#words = words.decode('utf8')
start = 0
i = 0
token = 'meaningless'
sents = []
#根据标点符号将句子切分
punt_list = ',.!?;~,。!?;~… ' #.decode('utf8')
#print "punc_list", punt_list
for word in words:
#print "word", word
if word not in punt_list: # 如果不是标点符号
#print "word1", word
i += 1
token = list(words[start:i+2]).pop()
#print "token:", token
elif word in punt_list and token in punt_list: # 处理省略号
#print "word2", word
i += 1
token = list(words[start:i+2]).pop()
#print "token:", token
else:
#print "word3", word
sents.append(words[start:i+1]) # 断句
start = i + 1
i += 1
if start < len(words): # 处理最后的部分
sents.append(words[start:])
return sents
def read_lines(filename):
fp = open(filename, 'r',encoding=
基于词典的情感分析
最新推荐文章于 2021-08-23 00:57:32 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)