Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
Directly capture output in EVALB metric (#2755)
Browse files Browse the repository at this point in the history
  • Loading branch information
nelson-liu committed Apr 25, 2019
1 parent 2077215 commit b6453dc
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions allennlp/training/metrics/evalb_bracketing_scorer.py
Expand Up @@ -84,7 +84,6 @@ def __call__(self, predicted_trees: List[Tree], gold_trees: List[Tree]) -> None:
tempdir = tempfile.mkdtemp()
gold_path = os.path.join(tempdir, "gold.txt")
predicted_path = os.path.join(tempdir, "predicted.txt")
output_path = os.path.join(tempdir, "output.txt")
with open(gold_path, "w") as gold_file:
for tree in gold_trees:
gold_file.write(f"{tree.pformat(margin=1000000)}\n")
Expand All @@ -93,19 +92,19 @@ def __call__(self, predicted_trees: List[Tree], gold_trees: List[Tree]) -> None:
for tree in predicted_trees:
predicted_file.write(f"{tree.pformat(margin=1000000)}\n")

command = f"{self._evalb_program_path} -p {self._evalb_param_path} " \
f"{gold_path} {predicted_path} > {output_path}"
subprocess.run(command, shell=True, check=True)

with open(output_path) as infile:
for line in infile:
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]
self._correct_predicted_brackets += numeric_line[5]
self._gold_brackets += numeric_line[6]
self._predicted_brackets += numeric_line[7]
command = [self._evalb_program_path, "-p", self._evalb_param_path,
gold_path, predicted_path]
completed_process = subprocess.run(command, stdout=subprocess.PIPE,
universal_newlines=True, check=True)

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]
self._correct_predicted_brackets += numeric_line[5]
self._gold_brackets += numeric_line[6]
self._predicted_brackets += numeric_line[7]

shutil.rmtree(tempdir)

Expand Down

0 comments on commit b6453dc

Please sign in to comment.