validate.py 931 B

12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (c) Facebook, Inc. and its affiliates.
  4. #
  5. # This source code is licensed under the MIT license found in the
  6. # LICENSE file in the root directory of this source tree.
  7. import sys
  8. """Reads in a fairseq output file, and verifies that the constraints
  9. (C- lines) are present in the output (the first H- line). Assumes that
  10. constraints are listed prior to the first hypothesis.
  11. """
  12. constraints = []
  13. found = 0
  14. total = 0
  15. for line in sys.stdin:
  16. if line.startswith("C-"):
  17. constraints.append(line.rstrip().split("\t")[1])
  18. elif line.startswith("H-"):
  19. text = line.split("\t")[2]
  20. for constraint in constraints:
  21. total += 1
  22. if constraint in text:
  23. found += 1
  24. else:
  25. print(f"No {constraint} in {text}", file=sys.stderr)
  26. constraints = []
  27. print(f"Found {found} / {total} = {100 * found / total:.1f}%")