-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.py
110 lines (93 loc) · 3.35 KB
/
reader.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
import comment
import commentdb
from typing import *
from collections import Counter
class Reader:
def __init__(self, db: commentdb.DBConnection):
self._db = db
self._comments = db.get_all_comments()
def with_tag(self, tag: str, allow_anon: bool = True) -> List[comment.Comment]:
comments = []
for com in self._comments:
if tag in com.title.lower() and (allow_anon or not com.is_anon()):
comments.append(com)
return comments
def filter_repeats(self):
new_comments = []
body_set = set()
for com in self._comments:
if com.comment not in body_set:
new_comments.append(com)
body_set.add(com.comment)
self._comments = new_comments
def amount_anon(self) -> int:
anon = 0
for com in self._comments:
if com.is_anon():
anon += 1
return anon
def __str__(self, verbose: bool = False) -> str:
support = 0
oppose = 0
unknown = 0
total = support + oppose + unknown
msg = "Support: {} ({:.2f}%) Oppose: {} ({:.2f}%) Unknown: {} ({:.2f}%) Total: {}".format(
support, support / total * 100, oppose, oppose / total * 100, unknown, unknown / total * 100, total
)
return msg
def __len__(self) -> int:
return len(self._comments)
def count_titles(self) -> Counter:
count = Counter()
for comm in self._comments:
count.update({comm.title.lower(): 1})
return count
def count_authors(self) -> Counter:
count = Counter()
for comm in self._comments:
count.update({comm.author.lower(): 1})
return count
def count_comments(self) -> Counter:
count = Counter()
for comm in self._comments:
count.update({comm.comment.lower(): 1})
return count
def print_stats(stats: dict):
msg = ""
for key in stats.keys():
msg += "[{key}: {val} ({percent:.2f}%)] ".format(key=key, val=stats[key], percent=stats[key]/stats["total"]*100)
print("="*len(msg))
print(msg)
print("="*len(msg))
if __name__ == "__main__":
db = commentdb.DBConnection("test.db")
reader = Reader(db)
reader.filter_repeats()
# ===================================================================
support_anon = len(reader.with_tag("support"))
oppose_anon = len(reader.with_tag("oppose"))
unknown_anon = len(reader) - support_anon - oppose_anon
stats = {
"support": support_anon,
"oppose": oppose_anon,
"unknown": unknown_anon,
"total": support_anon + oppose_anon + unknown_anon
}
print_stats(stats)
# ===================================================================
support = len(reader.with_tag("support", False))
oppose = len(reader.with_tag("support", False))
unknown = len(reader) - reader.amount_anon() - support - oppose
stats = {
"support": support,
"oppose": oppose,
"unknown": unknown,
"total": support + oppose + unknown
}
print_stats(stats)
print("Titles:")
for title in reader.count_titles().most_common(10):
print("\t{}: {}".format(title[0], title[1]))
print("Authors:")
for author in reader.count_authors().most_common(10):
print("\t{}: {}".format(author[0], author[1]))