# coding=utf-8 from collections import defaultdict import os import jieba import re import codecs # 定义一个空集合,用于存储停用词 stopwords = set() # 打开停用词文件,以只读方式读取 fr = open('停用词.txt', 'r', encoding='utf-8') # 遍历文件中的每一行,将每一行去除首尾空格后添加到停用词集合中 for word in fr: stopwords.add(word.strip()) # 打开否定词文件,以读写方式读取 not_word_file = open('否定词.txt', 'r+', encoding='utf-8') # 读取否定词文件中的每一行,并将每一行去除首尾空格后添加到否定词列表中 not_word_list = not_word_file.readlines() not_word_list = [word.strip() for word in not_word_list] # 打开程度副词文件,以读写方式读取 degree_file = open('程度副词.txt', 'r+', encoding='utf-8') # 读取程度副词文件中的每一行,并将每一行以逗号分隔,取第一个元素后添加到程度副词列表中 degree_list = degree_file.readlines() degree_list = [item.split(',')[0] for item in degree_list] # 打开一个新文件,以写入方式打开 with open('stopwords.txt', 'w', encoding='utf-8') as f: # 遍历停用词集合中的每一个元素 for word in stopwords: # 如果该元素不在否定词列表和程度副词列表中,则将该元素写入新文件中 if (word not in not_word_list) and (word not in degree_list): f.write(word + '\n') # 定义一个函数,用于对句子进行分词 def seg_word(sentence): # 使用jieba库对句子进行分词 seg_list = jieba.cut(sentence) # 定义一个空列表,用于存储分词结果 seg_result = [] # 遍历分词结果 for i in seg_list: # 将分词结果添加到列表中 seg_result.append(i) # 定义一个空集合,用于存储停用词 stopwords = set() # 打开停用词文件,以只读方式读取 with open('stopwords.txt', 'r', encoding='utf-8') as fr: # 遍历文件中的每一行,将每一行去除首尾空格后添加到停用词集合中 for i in fr: stopwords.add(i.strip()) # 返回一个列表,该列表中包含分词结果中不在停用词集合中的元素 return list(filter(lambda x: x not in stopwords, seg_result)) # 定义一个函数,用于对分词结果进行分类 def classify_words(word_list): # 打开情感词文件,以读写方式读取 sen_file = open('BosonNLP_sentiment_score.txt', 'r+', encoding='utf-8') # 读取情感词文件中的每一行,并将每一行以空格分隔,取第一个元素和第二个元素后添加到情感词列表中 sen_list = sen_file.readlines() # 定义一个空字典,用于存储情感词 sen_dict = defaultdict() for i in sen_list: if len(i.split(' ')) == 2: sen_dict[i.split(' ')[0]] = i.split(' ')[1] # 打开否定词文件,以读写方式读取 not_word_file = open('否定词.txt', 'r+', encoding='utf-8') # 读取否定词文件中的每一行,并将每一行去除首尾空格后添加到否定词列表中 not_word_list = not_word_file.readlines() # 打开程度副词文件,以读写方式读取 degree_file = open('程度副词.txt', 'r+', encoding='utf-8') # 读取程度副词文件中的每一行,并将每一行以逗号分隔,取第一个元素后添加到程度副词列表中 degree_list = degree_file.readlines() # 定义一个空字典,用于存储程度副词 degree_dict = defaultdict() for i in degree_list: degree_dict[i.split(',')[0]] = i.split(',')[0] # 定义三个空字典,用于存储情感词、否定词和程度副词 sen_word = dict() not_word = dict() degree_word = dict() # 遍历分词结果 for i in range(len(word_list)): # 获取分词结果中的每一个元素 word = word_list[i] # 如果该元素在情感词字典中,且不在否定词列表和程度副词字典中,则将该元素添加到情感词字典中 if word in sen_dict.keys() and word not in not_word_list and word not in degree_dict.keys(): sen_word[i] = sen_dict[word] # 如果该元素在否定词列表中,且不在程度副词字典中,则将该元素添加到否定词字典中 elif word in not_word_list and word not in degree_dict.keys(): not_word[i] = -1 # 如果该元素在程度副词字典中,则将该元素添加到程度副词字典中 elif word in degree_dict.keys(): degree_word[i] = degree_dict[word] # 关闭文件 sen_file.close() not_word_file.close() degree_file.close() # 返回情感词字典、否定词字典和程度副词字典 return sen_word, not_word, degree_word # 定义一个函数,用于计算情感得分 # 定义一个函数,用于计算句子的情感得分 def score_sentiment(sen_word, not_word, degree_word, seg_result): # 初始化权重为1 W = 1 # 初始化情感得分为0 score = 0 # 初始化情感索引为-1 sentiment_index = -1 # 将情感词的索引存入列表 sentiment_index_list = list(sen_word.keys()) # 遍历分词结果 for i in range(0, len(seg_result)): # 如果当前词是情感词 if i in sen_word.keys(): # 情感得分加上权重乘以情感词的得分 score += W * float(sen_word[i]) # 情感索引加1 sentiment_index += 1 # 如果情感索引小于情感词索引列表的长度减1 if sentiment_index < len(sentiment_index_list) - 1: # 遍历情感词索引之间的词 for j in range(sentiment_index_list[sentiment_index], sentiment_index_list[sentiment_index + 1]): # 如果当前词是否定词 if j in not_word.keys(): # 权重乘以-1 W *= -1 # 如果当前词是程度词 elif j in degree_word.keys(): # 权重乘以程度词的得分 W *= float(degree_word[j]) # 情感索引加1 sentiment_index += 1 # 如果情感索引小于情感词索引列表的长度减1 if sentiment_index < len(sentiment_index_list) - 1: # 将i设置为情感词索引列表中下一个情感词的索引 i = sentiment_index_list[sentiment_index + 1] # 返回情感得分 return score # 定义一个函数,用于计算句子的情感得分 def sentiment_score(sentence): # 对句子进行分词 seg_list = seg_word(sentence) # 对分词结果进行情感词、否定词、程度词的分类 sen_word, not_word, degree_word = classify_words(seg_list) # 计算情感得分 score = score_sentiment(sen_word, not_word, degree_word, seg_list) # 返回情感得分 return score主要实现了对文本情感倾向的分析,数值大小表示文本情感倾向的强烈程度,正负号表示文本情感倾向,如 - 代表消极情绪,在整段代码功能实现的过程中主要遇见了3个问题
- PermissionError: [Errno 13] Permission denied
这个问题的原因是文件的权限不够,需要改文件属性
当文件是只读属性时,会报错
Traceback (most recent call last):
File "d:\vscode_come\from collections import defaultdict.py", line 12, in <module>
not_word_file = open('否定词.txt', 'r+', encoding='utf-8')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [Errno 13] Permission denied: '否定词.txt
我在这篇文章中找到Permission denied是一个常见的错误提示,表示当前用户没有足够的权限来执行某个操作。
但实际上这时报错是因为文件是只读属性的,我想这背后有一定的相关性,因为文章中说这种错误常见于Linux系统。
当然这个文件的解决办法可能不止这一种,在这篇文章中还提到了另一种解决办法不过我并没有用上。
2.在过程中我还碰到了索引超出的问题但实际上,我并没有做出任何处理接解决了,不过我还是可以给大家讲一下我的尝试,主要是尝试这几篇文章中的内容
3.第三个问题是
‘‘这是我两年前的课程作业时间太长已经忘记了,两年前写的博客中间电脑坏了没发,已经找不到问题在哪里了’’