-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcompute_section_stats.py
49 lines (40 loc) · 1.8 KB
/
compute_section_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
import os
import json
import numpy as np
from tqdm import tqdm
from collections import Counter
if __name__ == "__main__":
fp = "./tos-data-og"
total_num_paras = []
section_lengths = []
number_of_times_section_topic_appeared = []
paragraphs_of_section_topic = []
for fn in tqdm(sorted(os.listdir(fp))):
full_path = os.path.join(fp, fn)
with open(full_path) as f:
data = json.load(f)
curr_num_paras = 1
curr_section_length = 1
prev_section_heading = data["level1_headings"][0]["section"]
for section in data["level1_headings"][1:]:
curr_num_paras += 1
paragraphs_of_section_topic.append(section["section"])
if section["section"] == prev_section_heading:
curr_section_length += 1
else:
section_lengths.append(curr_section_length)
curr_section_length = 1
prev_section_heading = section["section"]
number_of_times_section_topic_appeared.append(section["section"])
total_num_paras.append(curr_num_paras)
print(f"Median #Paras per Doc: {np.median(total_num_paras):.2f}")
print(f"Mean #Paras per Doc: {np.mean(total_num_paras):.2f}")
print(f"---------------------------------------")
print(f"Median #Paras per Section: {np.median(section_lengths):.2f}")
print(f"Mean #Paras per Section: {np.mean(section_lengths):.2f}")
print("----------------------------------------")
print("Top occurring sections by the number of times they appear:")
print(Counter(number_of_times_section_topic_appeared).most_common(10))
print("----------------------------------------")
print("Top sections by the number of paragraphs:")
print(Counter(paragraphs_of_section_topic).most_common(10))