用Python编程一个用户评价系统,要求使用列表、字典、jieba库对自定义文本进行分词、使用add_word()、del_word()函数修改分词字典词语、将最终的评价结果以可视化图表形式呈现
时间: 2023-06-17 22:03:08 浏览: 42
好的,我可以给您提供一个简单的用户评价系统的代码框架,您可以根据自己的需求进行修改和完善。
首先,我们需要导入需要用到的库:
```python
import jieba
import matplotlib.pyplot as plt
```
然后,我们定义一个评价系统的类:
```python
class EvaluationSystem:
def __init__(self):
self.positive_words = [] # 存储积极词语
self.negative_words = [] # 存储消极词语
# 加载自定义的积极和消极词语列表
def load_words(self, positive_words_file, negative_words_file):
# 读取积极词语文件
with open(positive_words_file, 'r', encoding='utf-8') as f:
for line in f:
self.positive_words.append(line.strip())
# 读取消极词语文件
with open(negative_words_file, 'r', encoding='utf-8') as f:
for line in f:
self.negative_words.append(line.strip())
# 分词
def cut_words(self, text):
words = jieba.lcut(text)
return words
# 分析评价结果
def analyze_evaluation(self, text):
words = self.cut_words(text)
positive_count = 0 # 记录积极词语出现次数
negative_count = 0 # 记录消极词语出现次数
for word in words:
if word in self.positive_words:
positive_count += 1
elif word in self.negative_words:
negative_count += 1
return positive_count, negative_count
# 可视化评价结果
def visualize_evaluation(self, positive_count, negative_count):
labels = ['Positive', 'Negative']
values = [positive_count, negative_count]
plt.bar(labels, values)
plt.show()
# 添加词语到分词字典中
def add_word(self, word):
jieba.add_word(word)
# 从分词字典中删除指定的词语
def del_word(self, word):
jieba.del_word(word)
```
接下来,我们可以使用这个评价系统进行评价分析:
```python
# 创建评价系统实例
es = EvaluationSystem()
# 加载自定义积极和消极词语列表
es.load_words('positive_words.txt', 'negative_words.txt')
# 添加自定义词语到分词字典中
es.add_word('好评')
# 从分词字典中删除指定的词语
es.del_word('差评')
# 分析评价结果
text = '这家店的服务态度非常好,菜品也很美味,下次还会再来!'
positive_count, negative_count = es.analyze_evaluation(text)
print('Positive count:', positive_count)
print('Negative count:', negative_count)
# 可视化评价结果
es.visualize_evaluation(positive_count, negative_count)
```
这个代码框架可以帮助您完成用户评价系统的基本功能,您可以根据需要进行修改和完善。