-
Notifications
You must be signed in to change notification settings - Fork 0
/
display_review_data.py
71 lines (57 loc) · 2.1 KB
/
display_review_data.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
from download_review_data import load_reviews
from filter_review_data import cluster_reviews_by_language
def print_global_summary(review_dict):
query_summary = review_dict['query_summary']
positive = query_summary['total_positive']
negative = query_summary['total_negative']
print(
'Review score = {} ({})'.format(
query_summary['review_score'],
query_summary['review_score_desc'],
),
)
print(f'#positive reviews = {positive}')
print(f'#negative reviews = {negative}')
print('#reviews = {}'.format(query_summary['total_reviews']))
if query_summary['total_reviews'] != (positive + negative):
raise AssertionError()
return
def print_regional_summary(review_dict):
languages = cluster_reviews_by_language(review_dict, verbose=True)
for language in sorted(
languages.keys(),
key=lambda x: sum(len(languages[x][is_upvote]) for is_upvote in [True, False]),
):
language_stats = languages[language]
positive = language_stats[True]
negative = language_stats[False]
num_reviews = len(positive) + len(negative)
ratio_num_reviews = len(positive) / num_reviews
positive_length = sum(
map(
len,
[review_dict['reviews'][review_id]['review'] for review_id in positive],
),
)
negative_length = sum(
map(
len,
[review_dict['reviews'][review_id]['review'] for review_id in negative],
),
)
total_length = positive_length + negative_length
ratio_length = positive_length / total_length
print(
'Language = {:10} #reviews = {:5} ({:.0%} positive) ; #characters = {:10} ({:.0%} positive)'.format(
language.title(),
num_reviews,
ratio_num_reviews,
total_length,
ratio_length,
),
)
return
if __name__ == '__main__':
review_dict = load_reviews()
print_global_summary(review_dict)
print_regional_summary(review_dict)