如何通过构建哈工大情感词典进行中文情感分析
时间: 2024-04-28 15:24:01 浏览: 15
哈工大情感词典是一个常用的中文情感词典,可以用于中文情感分析。下面是基于哈工大情感词典的中文情感分析的步骤:
1. 下载哈工大情感词典,可以在官网上下载或者用Python库进行下载:
```python
import urllib.request
url = 'https://github.com/ysc/graduation-project/blob/master/ChnSentiCorp_htl_ba_6000.txt?raw=true'
urllib.request.urlretrieve(url, 'Hownet.txt')
```
2. 加载情感词典,将词典中的词语和情感值存储到字典中:
```python
def load_dict():
sentiment_dict = {}
with open('Hownet.txt', 'r', encoding='utf-8') as f:
for line in f.readlines():
line = line.strip()
if line:
word, sentiment = line.split('\t')
sentiment_dict[word] = sentiment
return sentiment_dict
sentiment_dict = load_dict()
```
3. 对中文文本进行分词和情感分类,可以使用Python中的分词库jieba和Python中的字典:
```python
import jieba
def chinese_sentiment_analysis(text):
seg_list = jieba.cut(text)
words_list = list(seg_list)
positive_score = 0
negative_score = 0
for word in words_list:
if word in sentiment_dict:
sentiment = sentiment_dict[word]
if sentiment == 'positive':
positive_score += 1
elif sentiment == 'negative':
negative_score += 1
if positive_score > negative_score:
return 'positive'
elif positive_score < negative_score:
return 'negative'
else:
return 'neutral'
```
4. 测试代码:
```python
text = '这家餐厅的菜很好吃,服务也很周到。'
sentiment = chinese_sentiment_analysis(text)
print(sentiment) # positive
```
上述代码仅仅是一个简单的示例,实际应用中可能需要根据具体情况进行修改和优化。