-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathreviews.py
437 lines (308 loc) · 10.6 KB
/
reviews.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Author LQ6H
from pyecharts import options as opts
from pyecharts.charts import Pie,Bar,WordCloud,Funnel,Line,Page
import pandas as pd
import json
import re
import csv
from collections import Counter
import jieba
def split_word(comment):
word_list = list(jieba.cut(comment))
with open("stop_words.txt", encoding="utf-8") as f:
meaningless_word = f.read().splitlines()
result = []
for i in word_list:
if i not in meaningless_word:
result.append(i.replace(" ", ""))
return result
def word_counter(words):
words_counter = Counter(words)
words_list = words_counter.most_common(100)
return words_list
def save_data():
datas = []
with open("review_final.json", "r", encoding="utf-8") as f:
for line in f.readlines():
data = list(json.loads(line).values())
datas.append(data)
with open('review_final.csv', 'w', encoding='utf-8', newline='') as csvFile:
csv.writer(csvFile).writerow(
['用户id', '用户名', '评论内容', '用户链接', '电影id', '电影title', '电影tag', '电影评分', '导演', '电影类型', '国家', '片长', '发布时间'])
for rows in datas:
csv.writer(csvFile).writerow(rows)
f.close()
df = pd.read_csv('review_final.csv', engine='python', encoding='utf-8')
return df
def Pie1():
x = list(df['用户id'].value_counts().index)[:10]
y = list(df['用户名'].value_counts().head(10))
data_pairs = [i for i in zip(x, y)]
p1 = (
Pie(init_opts=opts.InitOpts(width="1000px", height="600px"))
.add(
series_name="用户评论数量",
data_pair=data_pairs
)
.set_global_opts(
title_opts=opts.TitleOpts(title="用户评论次数分析"),
legend_opts=opts.LegendOpts(type_="scroll", pos_left="80%", orient="vertical")
)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
)
return p1
def Bar1():
x = list(df['用户id'].value_counts().index)
user = df[df['用户id'] == x[0]]
user_type = user.loc[:, '电影类型']
x = user_type.value_counts()
res = "".join(list(x.index))
pattern = re.compile(r'\w+')
s = pattern.findall(res)
res = Counter(s)
x = []
y = []
for k, v in res.items():
x.append(k)
y.append(v)
b1 = (
Bar(init_opts=opts.InitOpts(width="1000px", height="600px"))
.add_xaxis(x)
.add_yaxis("电影类型次数", y)
.set_global_opts(
title_opts=opts.TitleOpts(title="用户观看电影类型分析"),
datazoom_opts=opts.DataZoomOpts(),
)
)
return b1
def Wordlound1():
users = list(df['用户id'].value_counts().index)[:10]
user_1 = df[df['用户id'] == users[0]]
comment = "".join(user_1['评论内容'].to_list())
pattern = re.compile(r"[^a-zA-Z0-9\u4e00-\u9fa5]")
comment = pattern.sub('', comment)
words = split_word(comment)
data = word_counter(words)
w1 = (
WordCloud()
.add(
series_name="用户评论词云",
data_pair=data,
word_size_range=[10, 120],
shape="cursive"
)
.set_global_opts(
title_opts=opts.TitleOpts(title="用户评论", title_textstyle_opts=opts.TextStyleOpts(font_size=23)),
tooltip_opts=opts.TooltipOpts(is_show=True)
)
)
return w1
def Wordcloud2():
comment1 = df[df["电影title"] == "阴曹使者"]
comment1 = "".join(set(comment1['评论内容'].to_list()))
pattern = re.compile(r"[^a-zA-Z0-9\u4e00-\u9fa5]")
comment1 = pattern.sub('', comment1)
words = split_word(comment1)
data = word_counter(words)
w2 = (
WordCloud()
.add(
series_name="电影评论客观词云",
data_pair=data,
word_size_range=[10, 120],
shape="cursive"
)
.set_global_opts(
title_opts=opts.TitleOpts(title="电影评论客观词云", title_textstyle_opts=opts.TextStyleOpts(font_size=23)),
tooltip_opts=opts.TooltipOpts(is_show=True)
)
)
return w2
def Funnel1():
movie_type = df['国家'].value_counts()
x = list(movie_type.index)[:10]
y = movie_type.to_list()[:10]
data_pairs = [z for z in zip(x, y)]
f1 = (
Funnel()
.add(
series_name="",
data_pair=data_pairs
)
.set_global_opts(
title_opts=opts.TitleOpts(title="电影国家比重"),
legend_opts=opts.LegendOpts(type_="scroll", pos_left="0%", pos_top="20%", orient="vertical")
)
)
return f1
"""
def Line1():
data = df.dropna()
data.index = range(len(data))
def Month(s):
s = s.split('-')
if s[1] == "01":
return "一月"
elif s[1] == "02":
return "二月"
elif s[1] == "03":
return "三月"
else:
return "四月"
data["电影月份"] = data["发布时间"].apply(Month)
month = ["一月", "二月", "三月", "四月"]
def average_rate():
rate = []
for i in month:
month_res = data[data['电影月份'] == i]["电影评分"]
result = float(sum(month_res.to_list()) / len(month_res.to_list()))
result = float(str(result)[:4])
rate.append(result)
return rate
rate = average_rate()
l1 = (
Line(init_opts=opts.InitOpts(width="1000px", height="600px"))
.add_xaxis(month)
.add_yaxis("电影评分", rate)
.set_global_opts(
title_opts=opts.TitleOpts(title="评分")
)
)
return l1
"""
def Pie2():
types = df['电影tag'].value_counts()
x = list(types.index)
y = types.to_list()
p2 = (
Pie()
.add("电影Tag",
[z for z in zip(x, y)]
)
.set_global_opts(
title_opts=opts.TitleOpts(title="电影类别统计"),
legend_opts=opts.LegendOpts(type_="scroll", pos_left="80%", orient="vertical")
)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
)
return p2
def Pie3():
types = df['电影tag'].value_counts()
x = list(types.index)
def average_rate():
rate = []
for i in x:
month_res = df[df['电影tag'] == i]["电影评分"]
result = float(sum(month_res.to_list()) / len(month_res.to_list()))
result = float(str(result)[:4])
rate.append(result)
return rate
rate = average_rate()
p3 = (
Pie()
.add(
"",
[list(z) for z in zip(x, rate)],
radius=["40%", "55%"],
label_opts=opts.LabelOpts(
position="outside",
formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c} {per|{d}%} ",
background_color="#eee",
border_color="#aaa",
border_width=1,
border_radius=4,
rich={
"a": {"color": "#999", "lineHeight": 22, "align": "center"},
"abg": {
"backgroundColor": "#e3e3e3",
"width": "100%",
"align": "right",
"height": 22,
"borderRadius": [4, 4, 0, 0],
},
"hr": {
"borderColor": "#aaa",
"width": "100%",
"borderWidth": 0.5,
"height": 0,
},
"b": {"fontSize": 16, "lineHeight": 33},
"per": {
"color": "#eee",
"backgroundColor": "#334455",
"padding": [2, 4],
"borderRadius": 2,
},
},
),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="电影类别平均评分"),
legend_opts=opts.LegendOpts(type_="scroll", pos_left="0%", pos_top="20%", orient="vertical"),
toolbox_opts=opts.ToolboxOpts(is_show=True)
)
)
return p3
def Bar2():
types = df['电影tag'].value_counts()
x = list(types.index)
def average_rate():
rate = []
for i in x:
month_res = df[df['电影tag'] == i]["电影评分"]
result = float(sum(month_res.to_list()) / len(month_res.to_list()))
result = float(str(result)[:4])
rate.append(result)
return rate
rate = average_rate()
b2 = (
Bar(init_opts=opts.InitOpts(width="1000px", height="600px"))
.add_xaxis(x)
.add_yaxis("电影Tag", rate)
.set_global_opts(
title_opts=opts.TitleOpts(title="电影Tag评分"),
datazoom_opts=opts.DataZoomOpts(),
)
)
return b2
def Bar3():
x = list(df["导演"].value_counts().index)[:10]
def average_rate():
rate = []
for i in x:
month_res = df[df['导演'] == i]["电影评分"]
result = float(sum(month_res.to_list()) / len(month_res.to_list()))
result = float(str(result)[:4])
rate.append(result)
return rate
rate = average_rate()
b3 = (
Bar(init_opts=opts.InitOpts(width="1000px", height="600px"))
.add_xaxis(x)
.add_yaxis("电影Tag", rate)
.set_global_opts(
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),
title_opts=opts.TitleOpts(title="电影导演评分"),
# datazoom_opts=opts.DataZoomOpts(),
)
)
return b3
def main():
page=Page(layout=Page.DraggablePageLayout)
page.add(
Pie1(),
Bar1(),
Wordlound1(),
Wordcloud2(),
Funnel1(),
Pie2(),
Pie3(),
Bar2(),
Bar3(),
)
page.render("review_fun.html")
if __name__=="__main__":
df=save_data()
main()