Skip to content
Permalink
Browse files

Directly capture output in EVALB metric (#2755)

  • Loading branch information...
nelson-liu committed Apr 25, 2019
1 parent 2077215 commit b6453dc1427fcad71616d10e9394c7d6ccb91566
Showing with 13 additions and 14 deletions.
  1. +13 −14 allennlp/training/metrics/evalb_bracketing_scorer.py
@@ -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")
@@ -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)

0 comments on commit b6453dc

Please sign in to comment.
You can’t perform that action at this time.