-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats.py
77 lines (64 loc) · 2.57 KB
/
stats.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
from core.reducers import Summarizers
from core import tokenize_sentences
from prettytable import PrettyTable, ALL
def read(filepath: str) -> str:
with open(filepath, 'r', encoding='utf-8') as fixture:
# compute total selection
return fixture.read()
def make_stats_by_sentences(text, sentences, options):
result = [[] for i in range(len(sentences))]
for index, s in enumerate(Summarizers):
summary = s.summarize(text, options)
selection = list(map(lambda sentence: sentence in summary, sentences))
for i_sentence, is_in_summary in enumerate(selection):
result[i_sentence].append(is_in_summary)
# if is_in_summary is True:
# stats[i_sentence].append(s.name)
return result
def make_stats_by_summarizers(text, sentences, options):
result = [[] for i in range(len(Summarizers))]
for index, s in enumerate(Summarizers):
summary = s.summarize(text, options)
selection = list(map(lambda sentence: sentence in summary, sentences))
result[index] = selection
return result
def create_html(table):
with open('stats.html', 'w') as html:
html.write('<html><head><style>')
html.write("""
body { font-family: sans-serif; }
table {
border-collapse: collapse;
}
th {
background: white;
position: sticky;
top: 0;
box-shadow: 0 2px 2px -1px rgba(0, 0, 0, 0.4);
}
table, th, td {
border: 1px solid black;
}
th, td { padding: 5px; }
tr:nth-child(even) {background-color: #f2f2f2;}
tr>td:not(:nth-child(2)) { text-align: center; }
tr>td:not(:nth-child(-n + 2)):not(:empty) { background-color: #ccc; }
""")
html.write('</style></head><body>')
html.write(table.get_html_string())
html.write('</body></html>')
RATIO = 0.5
if __name__ == '__main__':
fixture = read('fixtures/it.web.rest-api.txt')
sentences = tokenize_sentences(fixture)
stats = make_stats_by_sentences(fixture, sentences, {"ratio": RATIO})
headers = ['#', 'SENTENCE'] + [s.name for s in Summarizers]
table = PrettyTable(field_names=headers)
table.align['SENTENCE'] = 'l'
table.hrules = ALL
for index, sentence in enumerate(sentences):
table.add_row([index + 1, sentence] + ['+' if x is True else '' for x in stats[index]])
# Генерирует HTML страницу с таблицей
create_html(table)
# Выводит ASCII таблицу в консоль
print(table)