This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
evalb_bracketing_scorer.py
197 lines (166 loc) · 7.13 KB
/
evalb_bracketing_scorer.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
from typing import List
import logging
import os
import tempfile
import subprocess
import shutil
from nltk import Tree
from allennlp.common.checks import ConfigurationError
from allennlp.training.metrics.metric import Metric
from allennlp.nn.util import dist_reduce_sum
logger = logging.getLogger(__name__)
DEFAULT_EVALB_DIR = os.path.abspath(
os.path.join(
os.path.dirname(os.path.realpath(__file__)), os.pardir, os.pardir, "tools", "EVALB"
)
)
@Metric.register("evalb")
class EvalbBracketingScorer(Metric):
"""
This class uses the external EVALB software for computing a broad range of metrics
on parse trees. Here, we use it to compute the Precision, Recall and F1 metrics.
You can download the source for EVALB from here: <https://nlp.cs.nyu.edu/evalb/>.
Note that this software is 20 years old. In order to compile it on modern hardware,
you may need to remove an `include <malloc.h>` statement in `evalb.c` before it
will compile.
AllenNLP contains the EVALB software, but you will need to compile it yourself
before using it because the binary it generates is system dependent. To build it,
run `make` inside the `allennlp/tools/EVALB` directory.
Note that this metric reads and writes from disk quite a bit. You probably don't
want to include it in your training loop; instead, you should calculate this on
a validation set only.
# Parameters
evalb_directory_path : `str`, required.
The directory containing the EVALB executable.
evalb_param_filename : `str`, optional (default = `"COLLINS.prm"`)
The relative name of the EVALB configuration file used when scoring the trees.
By default, this uses the COLLINS.prm configuration file which comes with EVALB.
This configuration ignores POS tags and some punctuation labels.
evalb_num_errors_to_kill : `int`, optional (default = `"10"`)
The number of errors to tolerate from EVALB before terminating evaluation.
"""
def __init__(
self,
evalb_directory_path: str = DEFAULT_EVALB_DIR,
evalb_param_filename: str = "COLLINS.prm",
evalb_num_errors_to_kill: int = 10,
) -> None:
self._evalb_directory_path = evalb_directory_path
self._evalb_program_path = os.path.join(evalb_directory_path, "evalb")
self._evalb_param_path = os.path.join(evalb_directory_path, evalb_param_filename)
self._evalb_num_errors_to_kill = evalb_num_errors_to_kill
self._header_line = [
"ID",
"Len.",
"Stat.",
"Recal",
"Prec.",
"Bracket",
"gold",
"test",
"Bracket",
"Words",
"Tags",
"Accracy",
]
self._correct_predicted_brackets = 0.0
self._gold_brackets = 0.0
self._predicted_brackets = 0.0
def __call__(self, predicted_trees: List[Tree], gold_trees: List[Tree]) -> None: # type: ignore
"""
# Parameters
predicted_trees : `List[Tree]`
A list of predicted NLTK Trees to compute score for.
gold_trees : `List[Tree]`
A list of gold NLTK Trees to use as a reference.
"""
if not os.path.exists(self._evalb_program_path):
logger.warning(
f"EVALB not found at {self._evalb_program_path}. Attempting to compile it."
)
EvalbBracketingScorer.compile_evalb(self._evalb_directory_path)
# If EVALB executable still doesn't exist, raise an error.
if not os.path.exists(self._evalb_program_path):
compile_command = (
f"python -c 'from allennlp.training.metrics import EvalbBracketingScorer; "
f'EvalbBracketingScorer.compile_evalb("{self._evalb_directory_path}")\''
)
raise ConfigurationError(
f"EVALB still not found at {self._evalb_program_path}. "
"You must compile the EVALB scorer before using it."
" Run 'make' in the '{}' directory or run: {}".format(
self._evalb_program_path, compile_command
)
)
tempdir = tempfile.mkdtemp()
gold_path = os.path.join(tempdir, "gold.txt")
predicted_path = os.path.join(tempdir, "predicted.txt")
with open(gold_path, "w") as gold_file:
for tree in gold_trees:
gold_file.write(f"{tree.pformat(margin=1000000)}\n")
with open(predicted_path, "w") as predicted_file:
for tree in predicted_trees:
predicted_file.write(f"{tree.pformat(margin=1000000)}\n")
command = [
self._evalb_program_path,
"-p",
self._evalb_param_path,
"-e",
str(self._evalb_num_errors_to_kill),
gold_path,
predicted_path,
]
completed_process = subprocess.run(
command, stdout=subprocess.PIPE, universal_newlines=True, check=True
)
_correct_predicted_brackets = 0.0
_gold_brackets = 0.0
_predicted_brackets = 0.0
for line in completed_process.stdout.split("\n"):
stripped = line.strip().split()
if len(stripped) == 12 and stripped != self._header_line:
# This line contains results for a single tree.
numeric_line = [float(x) for x in stripped]
_correct_predicted_brackets += numeric_line[5]
_gold_brackets += numeric_line[6]
_predicted_brackets += numeric_line[7]
shutil.rmtree(tempdir)
self._correct_predicted_brackets += dist_reduce_sum(_correct_predicted_brackets)
self._gold_brackets += dist_reduce_sum(_gold_brackets)
self._predicted_brackets += dist_reduce_sum(_predicted_brackets)
def get_metric(self, reset: bool = False):
"""
# Returns
The average precision, recall and f1.
"""
recall = (
self._correct_predicted_brackets / self._gold_brackets
if self._gold_brackets > 0
else 0.0
)
precision = (
self._correct_predicted_brackets / self._predicted_brackets
if self._gold_brackets > 0
else 0.0
)
f1_measure = (
2 * (precision * recall) / (precision + recall) if precision + recall > 0 else 0
)
if reset:
self.reset()
return {
"evalb_recall": recall,
"evalb_precision": precision,
"evalb_f1_measure": f1_measure,
}
def reset(self):
self._correct_predicted_brackets = 0.0
self._gold_brackets = 0.0
self._predicted_brackets = 0.0
@staticmethod
def compile_evalb(evalb_directory_path: str = DEFAULT_EVALB_DIR):
logger.info(f"Compiling EVALB by running make in {evalb_directory_path}.")
os.system("cd {} && make && cd ../../../".format(evalb_directory_path))
@staticmethod
def clean_evalb(evalb_directory_path: str = DEFAULT_EVALB_DIR):
os.system("rm {}".format(os.path.join(evalb_directory_path, "evalb")))