This repository has been archived by the owner on Jan 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathanalysis.py
149 lines (124 loc) · 4.25 KB
/
analysis.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
'''
A module for analyzing the syntax of the model output.
'''
import os
import argparse
from collections import Counter
from contextlib import ExitStack
from itertools import chain
from data import preprocess
from utils.tree import ParseTree
def parse_args(argv=None):
''' Parse the arguments '''
parser = argparse.ArgumentParser(
description='Analyze parse outputs'
)
parser.add_argument(
'-b',
'--bpe-path',
type=str,
required=True,
help='The path to the learned bpe'
)
parser.add_argument(
'-s',
'--span',
type=int,
default=6,
help='What span to use for parse segmentation'
)
parser.add_argument(
'--preprocess-buffer-size',
type=int,
default=1000,
help='Number of lines to preprocess at once'
)
parser.add_argument(
'reference',
type=str,
help='The reference text'
)
parser.add_argument(
'prediction',
type=str,
help='The predicted text'
)
parser.add_argument(
'-v',
'--verbose',
default=0,
action='count',
help='Increase the verbosity level'
)
return parser.parse_args(args=argv)
def compute_f1(reference, prediction):
''' Compute an F1 score using a bag of words approach to the sequence '''
matches = Counter(prediction) & Counter(reference)
match_count = sum(matches.values())
if match_count:
recall = match_count / len(reference)
precision = match_count / len(prediction)
return (2 * recall * precision) / (recall + precision)
else:
return 0
def expand_constituents(line, segment=None):
'''
Expand the constituents in the parse, i.e convert constituent chunks
from <NP3> to NP NP NP.
'''
if segment:
constituents, _ = segment(line)
else:
constituents = line.strip().split()
matches = [ParseTree.CONSTITUENT_REGEX.match(c) for c in constituents]
return list(chain.from_iterable([[m[1]] * int(m[2]) for m in matches if m]))
def process_file(path, buffer=1000):
''' Process the given path return the path for the resultant file '''
parse_ext = '.parse'
if parse_ext not in path and not os.path.exists(f'{path}{parse_ext}'):
preprocess.parse(path, f'{path}{parse_ext}', buffer)
if parse_ext not in path:
path = f'{path}{parse_ext}'
return path
def main(argv=None):
''' Main entry point for analyzing the parse '''
args = parse_args(argv)
reference_path = process_file(args.reference, args.preprocess_buffer_size)
prediction_path = process_file(args.prediction, args.preprocess_buffer_size)
segmenters = [
preprocess.ParseSegmenter(args.bpe_path, span, 0)
for span in range(1, args.span + 1)
]
segment_reference = f'span{args.span}' not in args.reference
segment_prediction = f'span{args.span}' not in args.prediction
with ExitStack() as stack:
count = 0
matches = 0
f1_score = 0
reference_file = stack.enter_context(open(reference_path, 'rt'))
prediction_file = stack.enter_context(open(prediction_path, 'rt'))
for reference_line, prediction_line in zip(reference_file, prediction_file):
best_match = 0
best_f1_score = 0
if not segment_reference:
reference = expand_constituents(reference_line)
for segmenter in segmenters:
if segment_reference:
reference = expand_constituents(reference_line, segmenter)
prediction = expand_constituents(
prediction_line,
segmenter if segment_prediction else None
)
this_f1_score = compute_f1(reference, prediction)
if this_f1_score > best_f1_score:
best_match = reference == prediction
best_f1_score = this_f1_score
count += 1
matches += best_match
f1_score += best_f1_score
if not count:
print('No lines of input! Unable to analyze {} and {}.')
exit(1)
print(f'F1={f1_score / count:.2%}, Accuracy={matches / count:.2%}')
if __name__ == '__main__':
main()