Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

score.py 1.9 KB

You have to be logged in to leave a comment. Sign In
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
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2017-present, Facebook, Inc.
  3. # All rights reserved.
  4. #
  5. # This source code is licensed under the license found in the LICENSE file in
  6. # the root directory of this source tree. An additional grant of patent rights
  7. # can be found in the PATENTS file in the same directory.
  8. #
  9. import argparse
  10. import os
  11. import sys
  12. from fairseq import bleu, dictionary, tokenizer
  13. def main():
  14. parser = argparse.ArgumentParser(description='Command-line script for BLEU scoring.')
  15. parser.add_argument('-s', '--sys', default='-', help='system output')
  16. parser.add_argument('-r', '--ref', required=True, help='references')
  17. parser.add_argument('-o', '--order', default=4, metavar='N',
  18. type=int, help='consider ngrams up to this order')
  19. parser.add_argument('--ignore-case', action='store_true',
  20. help='case-insensitive scoring')
  21. args = parser.parse_args()
  22. print(args)
  23. assert args.sys == '-' or os.path.exists(args.sys), \
  24. "System output file {} does not exist".format(args.sys)
  25. assert os.path.exists(args.ref), \
  26. "Reference file {} does not exist".format(args.ref)
  27. dict = dictionary.Dictionary()
  28. def readlines(fd):
  29. for line in fd.readlines():
  30. if args.ignore_case:
  31. yield line.lower()
  32. yield line
  33. def score(fdsys):
  34. with open(args.ref) as fdref:
  35. scorer = bleu.Scorer(dict.pad(), dict.eos(), dict.unk())
  36. for sys_tok, ref_tok in zip(readlines(fdsys), readlines(fdref)):
  37. sys_tok = tokenizer.Tokenizer.tokenize(sys_tok, dict)
  38. ref_tok = tokenizer.Tokenizer.tokenize(ref_tok, dict)
  39. scorer.add(ref_tok, sys_tok)
  40. print(scorer.result_string(args.order))
  41. if args.sys == '-':
  42. score(sys.stdin)
  43. else:
  44. with open(args.sys, 'r') as f:
  45. score(f)
  46. if __name__ == '__main__':
  47. main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...