-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyze.py
197 lines (164 loc) · 6.13 KB
/
analyze.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# 对posts.josn文件进行分析
import asyncio
import json
import jieba
import numpy as np
from wordcloud import WordCloud, ImageColorGenerator
import matplotlib.pyplot as plt
from PIL import Image # 处理图片
import datetime
from concurrent.futures import ProcessPoolExecutor
# 根据data生成词云
def generate_word_cloud_by_data(data):
# 将文本数据拼接成一个长字符串
text_data = " ".join(post["text"] for post in data)
# 使用jieba分词
text_data = " ".join(jieba.cut(text_data))
# 读取停用词
# 停用词基于[stopwords/cn_stopwords.txt at master · goto456/stopwords](https://github.com/goto456/stopwords/blob/master/cn_stopwords.txt)
# 添加了贴吧常用的表情符号
stopwords = set()
content = [line.strip() for line in open('resources/cn_stopwords.txt', 'r', encoding="utf8").readlines()]
stopwords.update(content)
# image_mask = Image.open("resources/huaji.jpg")
# print(f"蒙版图片mode: {image_mask.mode}")
# mask = np.array(image_mask)
# 创建 WordCloud 对象
wordcloud = WordCloud(width=480,
height=480,
# mask=mask,
background_color="white",
# mode="RGB",
font_path='C:\Windows\Fonts\STZHONGS.ttf',
stopwords=stopwords,
max_words=100,
relative_scaling=0.8,
# contour_width=1,
).generate(text_data)
# image_color = ImageColorGenerator(mask)
# wordcloud.recolor(color_func=image_color)
# 显示词云图
plt.figure(figsize=(10, 5))
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
return plt
# 从post文件生成词云
def generate_word_cloud_from_file(file_path):
# 读取json文件
data = json.load(open(file_path, encoding="utf-8"))
result_plt = generate_word_cloud_by_data(data)
# 标题
result_plt.title(f"{file_path}词云图")
result_plt.show()
# wordcloud.to_file(f"output/wordcloud_{file_path}.png")
# 为每年的数据生成词云
def generate_word_cloud_every_year():
# 读取json文件
with open("output/post_all.json", encoding='utf-8') as f:
data = json.load(f)
for k, v in data.items():
result_plt = generate_word_cloud_by_data(v)
# 设置标题
result_plt.title(f"{k}年词云图")
# 保存图片
result_plt.savefig(f"output/wordcloud_{k}.png")
result_plt.show()
# 根据年份拆分数据,输出到不同的json文件中
def split_data_by_year():
# 读取json文件
with open('output/posts.json', encoding='utf-8') as f:
data = json.load(f)
files_data = {}
def process(item):
# 由时间戳转换为datetime对象
create_date = datetime.datetime.fromtimestamp(item["create_time"])
year = create_date.year
if year not in files_data.keys():
files_data[year] = []
files_data[year].append(item)
for item in data:
process(item)
# 输出到对应的json文件中
for k, v in files_data.items():
with open(f"output/post_{k}.json", "w", encoding="utf-8") as f:
json.dump(v, f, ensure_ascii=False, indent=2)
# 输出到总的json文件中
with open(f"output/post_all.json", "w", encoding="utf-8") as f:
json.dump(files_data, f, ensure_ascii=False, indent=2)
# 统计在每个贴吧的回复数
def count_post_per_forum(data):
stats = {}
for item in data:
name = item['forum_name']
if name not in stats:
stats[name] = 0
stats[name] += 1
return stats
# 根据data生成饼状图
def generate_pie_chart_by_data(data):
# 统计在每个贴吧的回复数
stats = count_post_per_forum(data)
# 出于显示美观考虑,将占比小的贴吧合并为其他
num_labels = 7 # 需要显示的标签数
# 当贴吧数大于num_labels时
if len(stats) > num_labels:
sorted_counts = sorted(stats.values())
min_count = sorted_counts[-num_labels]
other_count = 0
new_stats = {
k: v for k, v in stats.items() if v >= min_count
}
for v in stats.values():
if v < min_count:
other_count += v
new_stats['其他'] = other_count
stats = new_stats
# 绘制饼状图
labels = list(stats.keys())
sizes = list(stats.values())
fig1, ax1 = plt.subplots()
ax1.pie(sizes,
labels=labels,
# autopct='%1.1f%%',
pctdistance=0.1,
labeldistance=1.1,
shadow=False,
startangle=90)
ax1.axis('equal')
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
# 创建图例
# plt.legend(labels, loc="best", bbox_to_anchor=(0.5, 0, 0.5, 1))
return plt
# 由文件生成饼状图
def generate_pie_chart_from_file(file_path):
# 读取json文件
with open(file_path, encoding='utf-8') as f:
data = json.load(f)
result_plt = generate_pie_chart_by_data(data)
result_plt.show()
# 为每年的数据生成饼状图
def generate_pie_chart_every_year():
# 读取json文件
with open("output/post_all.json", encoding='utf-8') as f:
data = json.load(f)
for k, v in data.items():
result_plt = generate_pie_chart_by_data(v)
# 设置标题
result_plt.title(f"{k}年回复贴吧分布")
# 保存图片
result_plt.savefig(f"output/pie_{k}.png")
result_plt.show()
# 为每年的数据生成记录文件
def generate_post_all_data():
# 读取json文件
with open("output/post_all.json", encoding='utf-8') as f:
data = json.load(f)
result = {}
for k, v in data.items():
year_data = count_post_per_forum(v)
# 添加总数
year_data["总计回复"] = len(v)
result[k] = year_data
with open(f"output/post_all_data.json", "w", encoding="utf-8") as f:
json.dump(result, f, ensure_ascii=False, indent=2)