forked from puria-radmard/RFL-SBDALNER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.py
143 lines (129 loc) · 4.66 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
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
import os
import pickle
import json
def triplet_stats(fin):
valid = 0
total = 0
for line in fin:
line = line.strip()
if not line:
continue
sentence = json.loads(line)
total += len(sentence["relationMentions"])
for relationMention in sentence["relationMentions"]:
if relationMention["label"] != "None":
valid += 1
return valid, total
def sentence_length_stats():
length_dict = {}
with open("data/NYT_CoType/corpus.txt", "rt", encoding="utf-8") as fin:
for line in fin:
sentence = line.strip().split()
length = len(sentence)
if length not in length_dict:
length_dict[length] = 1
else:
length_dict[length] = length_dict[length] + 1
with open("data/NYT_CoType/sentence_length_stats.pk", "wb") as f:
pickle.dump(length_dict, f)
def token_length_stats():
length_dict = {}
with open("data/NYT_CoType/corpus.txt", "rt", encoding="utf-8") as fin:
for line in fin:
sentence = line.strip().split()
for word in sentence:
length = len(word)
if length not in length_dict:
length_dict[length] = 1
else:
length_dict[length] = length_dict[length] + 1
with open("data/NYT_CoType/token_length_stats.pk", "wb") as f:
pickle.dump(length_dict, f)
def show_length(length_dict, groups):
max_length = max(length_dict.keys())
total = sum(length_dict.values())
length_list = list(length_dict.items())
length_list.sort(key=lambda tup: tup[0])
length_groups = [0] * len(groups)
for length_tuple in length_list:
for index, group in enumerate(groups):
if length_tuple[0] <= group:
length_groups[index] = length_groups[index] + length_tuple[1]
break
print("-" * 36)
print(
"| ( 0, {:3d}] | {:8d} | {:7.3f}% |".format(
groups[0], length_groups[0], length_groups[0] / total * 100
)
)
for i in range(1, len(length_groups)):
print(
"| ({:3d}, {:3d}] | {:8d} | {:7.3f}% |".format(
groups[i - 1],
groups[i],
length_groups[i],
length_groups[i] / total * 100,
)
)
print("-" * 36)
print(
"| Total | {:8d} | {:7.3f}% |".format(
sum(length_groups), sum(length_groups) / total * 100
)
)
print("-" * 36)
print("Max Length: {:d}".format(max_length))
if __name__ == "__main__":
if not os.path.exists("data/NYT_CoType/sentence_length_stats.pk"):
sentence_length_stats()
with open("data/NYT_CoType/sentence_length_stats.pk", "rb") as f:
length_dict = pickle.load(f)
groups = list(range(10, 110, 10))
show_length(length_dict, groups)
if not os.path.exists("data/NYT_CoType/token_length_stats.pk"):
token_length_stats()
with open("data/NYT_CoType/token_length_stats.pk", "rb") as f:
length_dict = pickle.load(f)
groups = list(range(5, 25, 3))
show_length(length_dict, groups)
print()
with open("data/NYT_CoType/train.json", "rt", encoding="utf-8") as fin:
valid, total = triplet_stats(fin)
print("Train\n\tValid Triplets: {}\n\tTotal Triplets: {}".format(valid, total))
with open("data/NYT_CoType/test.json", "rt", encoding="utf-8") as fin:
valid, total = triplet_stats(fin)
print("Test\n\tValid Triplets: {}\n\tTotal Triplets: {}".format(valid, total))
# ------------------------------------
# | ( 0, 10] | 2251 | 0.952% |
# | ( 10, 20] | 22997 | 9.729% |
# | ( 20, 30] | 55208 | 23.356% |
# | ( 30, 40] | 65138 | 27.557% |
# | ( 40, 50] | 48907 | 20.690% |
# | ( 50, 60] | 24943 | 10.552% |
# | ( 60, 70] | 10123 | 4.283% |
# | ( 70, 80] | 3848 | 1.628% |
# | ( 80, 90] | 1619 | 0.685% |
# | ( 90, 100] | 664 | 0.281% |
# ------------------------------------
# | Total | 235698 | 99.713% |
# ------------------------------------
# Max Length: 9621
# ------------------------------------
# | ( 0, 5] | 6119176 | 68.385% |
# | ( 5, 8] | 2008222 | 22.443% |
# | ( 8, 11] | 682932 | 7.632% |
# | ( 11, 14] | 120471 | 1.346% |
# | ( 14, 17] | 12470 | 0.139% |
# | ( 17, 20] | 3492 | 0.039% |
# | ( 20, 23] | 807 | 0.009% |
# ------------------------------------
# | Total | 8947570 | 99.994% |
# ------------------------------------
# Max Length: 107
#
# Train
# Valid Triplets: 111610
# Total Triplets: 372853
# Test
# Valid Triplets: 410
# Total Triplets: 3880