-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistics.py
201 lines (155 loc) · 6.38 KB
/
statistics.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 09/05/2019
author: fenia
"""
import argparse
import numpy as np
from collections import OrderedDict
from recordtype import recordtype
parser = argparse.ArgumentParser()
parser.add_argument('--data', default="./dataset/cdr/cdr_train.data", type=str)
args = parser.parse_args()
EntityInfo = recordtype('EntityInfo', 'type mstart mend sentNo')
PairInfo = recordtype('PairInfo', 'type direction cross closeA closeB')
def chunks(l, n):
"""
Yield successive n-sized chunks from l.
"""
for i in range(0, len(l), n):
assert len(l[i:i + n]) == n
yield l[i:i + n]
for d, data in zip(['DATA'], [args.data]):
documents = {}
entities = {}
relations = {}
with open(data, 'r') as infile:
for line in infile:
line = line.rstrip().split('\t')
pairs = chunks(line[2:], 17)
id_ = line[0]
if id_ not in documents:
documents[id_] = []
for sent in line[1].split('|'):
documents[id_] += [sent]
if id_ not in entities:
entities[id_] = OrderedDict()
if id_ not in relations:
relations[id_] = OrderedDict()
for p in pairs:
# pairs
if (p[5], p[11]) not in relations[id_]:
relations[id_][(p[5], p[11])] = PairInfo(p[0], p[1], p[2], p[3], p[4])
else:
print('duplicates!')
# entities
if p[5] not in entities[id_]:
entities[id_][p[5]] = EntityInfo(p[7], p[8], p[9], p[10])
if p[11] not in entities[id_]:
entities[id_][p[11]] = EntityInfo(p[13], p[14], p[15], p[16])
docs = len(documents)
pair_types = {}
inter_types = {}
intra_types = {}
ent_types = {}
men_types = {}
dist = {}
for id_ in relations.keys():
for k, p in relations[id_].items():
if p.type not in pair_types:
pair_types[p.type] = 0
pair_types[p.type] += 1
if p.type not in dist:
dist[p.type] = []
if p.type not in inter_types:
inter_types[p.type] = 0
if p.type not in intra_types:
intra_types[p.type] = 0
if p.type not in dist:
dist[p.type] = []
if p.cross == 'CROSS':
inter_types[p.type] += 1
else:
intra_types[p.type] += 1
if p.cross == 'CROSS':
dist_ = 10000
for m1 in entities[id_][k[0]].sentNo.split(':'):
for m2 in entities[id_][k[1]].sentNo.split(':'):
if abs(int(m1) - int(m2)) < dist_:
dist_ = abs(int(m1) - int(m2))
dist[p.type] += [dist_]
for e in entities[id_].values():
if e.type not in ent_types:
ent_types[e.type] = 0
ent_types[e.type] += 1
if e.type not in men_types:
men_types[e.type] = 0
for m in e.mstart.split(':'):
men_types[e.type] += 1
ents_per_doc = [len(entities[n]) for n in documents.keys()]
rels_per_doc = [len(relations[n]) for n in documents.keys()]
ments_per_doc = [np.sum([len(e.sentNo.split(':')) for e in entities[n].values()]) for n in documents.keys()]
ments_per_ent = [[len(e.sentNo.split(':')) for e in entities[n].values()] for n in documents.keys()]
sents_per_doc = [len(s) for s in documents.values()]
sent_len = [len(a.split()) for s in documents.values() for a in s]
# write data
# with open('/'.join(args.data.split('/')[:-1]) + '/' + args.data.split('/')[-1].split('.')[0] + '.gold',
# 'w') as outfile:
for id_ in relations.keys():
for k, p in relations[id_].items():
PairInfo = recordtype('PairInfo', 'type direction cross closeA closeB')
# outfile.write('{}|{}|{}|{}|{}\n'.format(id_, k[0], k[1], p.cross, p.type))
print('''
----------------------- {} ----------------------
Documents {}
'''.format(d, docs))
print(' Pairs')
for x in ['{:<10}\t{:<5}'.format(k, v) for k, v in sorted(pair_types.items())]:
print(' {}'.format(x))
print()
print(' Entities')
for x in ['{:<10}\t{:<5}'.format(k, v) for k, v in sorted(ent_types.items())]:
print(' {}'.format(x))
print()
print(' Mentions')
for x in ['{:<10}\t{:<5}'.format(k, v) for k, v in sorted(men_types.items())]:
print(' {}'.format(x))
print()
print(' Intra Pairs')
for x in ['{:<10}\t{:<5}'.format(k, v) for k, v in sorted(intra_types.items())]:
print(' {}'.format(x))
print()
print(' Inter Pairs')
for x in ['{:<10}\t{:<5}'.format(k, v) for k, v in sorted(inter_types.items())]:
print(' {}'.format(x))
print()
# print(' Average/Max Sentence Distance')
# for x in ['{:<10}\t{:.1f}\t{}'.format(k, np.average(v), np.max(v)) for k, v in sorted(dist.items())]:
# print(' {}'.format(x))
# print()
print('''
Average entites/doc {:.1f}
Max {}
Average relations/doc {:.1f}
Max {}
Average mentions/doc {:.1f}
Max {}
Average mentions/entity {:.1f}
Max {}
Average sents/doc {:.1f}
Max {}
Average/max sent length {:.1f}
Max {}
'''.format(np.average(ents_per_doc),
np.max(ents_per_doc),
np.average(rels_per_doc),
np.max(rels_per_doc),
np.average(ments_per_doc),
np.max(ments_per_doc),
np.average([item for sublist in ments_per_ent for item in sublist]),
np.max([item for sublist in ments_per_ent for item in sublist]),
np.average(sents_per_doc),
np.max(sents_per_doc),
np.average(sent_len),
np.max(sent_len)))