-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsenti_ana_news.py
58 lines (48 loc) · 1.52 KB
/
senti_ana_news.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import json
from snownlp import SnowNLP
import time
def union_by_date(news):
"""
把列表news中日期相同的字典合并
:param news:
:return:
"""
for i in range(len(news)-1):
if news[i]['时间'] == news[i+1]['时间']:
news[i+1]['地址'] = news[i+1]['地址'] + ' && ' + news[i]['地址']
news[i+1]['标题'] = news[i+1]['标题'] + ' ' + news[i]['标题']
news[i] = {}
else:
continue
for j in range(len(news)-1,-1,-1):
if not news[j]:
news.pop(j)
# 删除键-'地址'
for dict in news:
del dict['地址']
# 倒置列表
res = []
for i in range(len(news)-1,-1,-1):
res.append(news[i])
return res
def senti_ana(news):
""""
利用SnowNLP分析每天新闻的情绪值,并增加到字典中
"""
for dict in news:
s = SnowNLP(dict['标题'])
dict['情绪值'] = s.sentiments
return news
if __name__ == '__main__':
start_time = time.time()
# 导入数据
with open('news_result.json', 'r', encoding='utf-8') as f:
news = json.load(f)
result = senti_ana(union_by_date(news))
# 将添加了情绪值的新闻数据保存到comments_new.json中
json_ = json.dumps(result, ensure_ascii=False)
with open('/Users/apple/PycharmProjects/untitled4/stock_analysis/news_add_sentiments.json', 'w') as f:
f.write(json_)
end_time = time.time()
times = end_time - start_time
print("运行时间:",times,"秒")