|
8 | 8 |
|
9 | 9 | """
|
10 | 10 |
|
11 |
| -from __future__ import print_function |
12 |
| -from __future__ import division |
| 11 | +import random |
13 | 12 |
|
14 | 13 | import amr
|
15 |
| -import os |
16 |
| -import random |
17 | 14 | import sys
|
18 | 15 |
|
19 | 16 | # total number of iteration in smatch computation
|
|
44 | 41 | match_triple_dict = {}
|
45 | 42 |
|
46 | 43 |
|
47 |
| -def build_arg_parser(): |
48 |
| - """ |
49 |
| - Build an argument parser using argparse. Use it when python version is 2.7 or later. |
50 |
| -
|
51 |
| - """ |
52 |
| - parser = argparse.ArgumentParser(description="Smatch calculator -- arguments") |
53 |
| - parser.add_argument('-f', nargs=2, required=True, type=argparse.FileType('r'), |
54 |
| - help='Two files containing AMR pairs. AMRs in each file are separated by a single blank line') |
55 |
| - parser.add_argument('-r', type=int, default=4, help='Restart number (Default:4)') |
56 |
| - parser.add_argument('--significant', type=int, default=2, help='significant digits to output (default: 2)') |
57 |
| - parser.add_argument('-v', action='store_true', help='Verbose output (Default:false)') |
58 |
| - parser.add_argument('--vv', action='store_true', help='Very Verbose output (Default:false)') |
59 |
| - parser.add_argument('--ms', action='store_true', default=False, |
60 |
| - help='Output multiple scores (one AMR pair a score)' |
61 |
| - 'instead of a single document-level smatch score (Default: false)') |
62 |
| - parser.add_argument('--pr', action='store_true', default=False, |
63 |
| - help="Output precision and recall as well as the f-score. Default: false") |
64 |
| - parser.add_argument('--justinstance', action='store_true', default=False, |
65 |
| - help="just pay attention to matching instances") |
66 |
| - parser.add_argument('--justattribute', action='store_true', default=False, |
67 |
| - help="just pay attention to matching attributes") |
68 |
| - parser.add_argument('--justrelation', action='store_true', default=False, |
69 |
| - help="just pay attention to matching relations") |
70 |
| - |
71 |
| - return parser |
72 |
| - |
73 |
| - |
74 |
| -def build_arg_parser2(): |
75 |
| - """ |
76 |
| - Build an argument parser using optparse. Use it when python version is 2.5 or 2.6. |
77 |
| -
|
78 |
| - """ |
79 |
| - usage_str = "Smatch calculator -- arguments" |
80 |
| - parser = optparse.OptionParser(usage=usage_str) |
81 |
| - parser.add_option("-f", "--files", nargs=2, dest="f", type="string", |
82 |
| - help='Two files containing AMR pairs. AMRs in each file are ' \ |
83 |
| - 'separated by a single blank line. This option is required.') |
84 |
| - parser.add_option("-r", "--restart", dest="r", type="int", help='Restart number (Default: 4)') |
85 |
| - parser.add_option('--significant', dest="significant", type="int", default=2, |
86 |
| - help='significant digits to output (default: 2)') |
87 |
| - parser.add_option("-v", "--verbose", action='store_true', dest="v", help='Verbose output (Default:False)') |
88 |
| - parser.add_option("--vv", "--veryverbose", action='store_true', dest="vv", |
89 |
| - help='Very Verbose output (Default:False)') |
90 |
| - parser.add_option("--ms", "--multiple_score", action='store_true', dest="ms", |
91 |
| - help='Output multiple scores (one AMR pair a score) instead of ' \ |
92 |
| - 'a single document-level smatch score (Default: False)') |
93 |
| - parser.add_option('--pr', "--precision_recall", action='store_true', dest="pr", |
94 |
| - help="Output precision and recall as well as the f-score. Default: false") |
95 |
| - parser.add_option('--justinstance', action='store_true', default=False, |
96 |
| - help="just pay attention to matching instances") |
97 |
| - parser.add_option('--justattribute', action='store_true', default=False, |
98 |
| - help="just pay attention to matching attributes") |
99 |
| - parser.add_option('--justrelation', action='store_true', default=False, |
100 |
| - help="just pay attention to matching relations") |
101 |
| - parser.set_defaults(r=4, v=False, ms=False, pr=False) |
102 |
| - return parser |
103 |
| - |
104 |
| - |
105 | 44 | def get_best_match(instance1, attribute1, relation1,
|
106 | 45 | instance2, attribute2, relation2,
|
107 | 46 | prefix1, prefix2, doinstance=True, doattribute=True, dorelation=True):
|
@@ -854,36 +793,62 @@ def main(arguments):
|
854 | 793 |
|
855 | 794 |
|
856 | 795 | if __name__ == "__main__":
|
857 |
| - parser = None |
858 |
| - args = None |
859 |
| - # use optparse if python version is 2.5 or 2.6 |
860 |
| - if sys.version_info[0] == 2 and sys.version_info[1] < 7: |
861 |
| - import optparse |
862 |
| - |
863 |
| - if len(sys.argv) == 1: |
864 |
| - print("No argument given. Please run smatch.py -h to see the argument description.", file=ERROR_LOG) |
865 |
| - exit(1) |
866 |
| - parser = build_arg_parser2() |
867 |
| - (args, opts) = parser.parse_args() |
868 |
| - file_handle = [] |
869 |
| - if args.f is None: |
870 |
| - print("smatch.py requires -f option to indicate two files \ |
871 |
| - containing AMR as input. Please run smatch.py -h to \ |
872 |
| - see the argument description.", file=ERROR_LOG) |
873 |
| - exit(1) |
874 |
| - # assert there are 2 file names following -f. |
875 |
| - assert (len(args.f) == 2) |
876 |
| - for file_path in args.f: |
877 |
| - if not os.path.exists(file_path): |
878 |
| - print("Given file", args.f[0], "does not exist", file=ERROR_LOG) |
879 |
| - exit(1) |
880 |
| - file_handle.append(open(file_path, encoding='utf8')) |
881 |
| - # use opened files |
882 |
| - args.f = tuple(file_handle) |
883 |
| - # use argparse if python version is 2.7 or later |
884 |
| - else: |
885 |
| - import argparse |
886 |
| - |
887 |
| - parser = build_arg_parser() |
888 |
| - args = parser.parse_args() |
| 796 | + import argparse |
| 797 | + |
| 798 | + parser = argparse.ArgumentParser(description="Smatch calculator") |
| 799 | + parser.add_argument( |
| 800 | + '-f', |
| 801 | + nargs=2, |
| 802 | + required=True, |
| 803 | + type=argparse.FileType('r'), |
| 804 | + help=('Two files containing AMR pairs. ' |
| 805 | + 'AMRs in each file are separated by a single blank line')) |
| 806 | + parser.add_argument( |
| 807 | + '-r', |
| 808 | + type=int, |
| 809 | + default=4, |
| 810 | + help='Restart number (Default:4)') |
| 811 | + parser.add_argument( |
| 812 | + '--significant', |
| 813 | + type=int, |
| 814 | + default=2, |
| 815 | + help='significant digits to output (default: 2)') |
| 816 | + parser.add_argument( |
| 817 | + '-v', |
| 818 | + action='store_true', |
| 819 | + help='Verbose output (Default:false)') |
| 820 | + parser.add_argument( |
| 821 | + '--vv', |
| 822 | + action='store_true', |
| 823 | + help='Very Verbose output (Default:false)') |
| 824 | + parser.add_argument( |
| 825 | + '--ms', |
| 826 | + action='store_true', |
| 827 | + default=False, |
| 828 | + help=('Output multiple scores (one AMR pair a score) ' |
| 829 | + 'instead of a single document-level smatch score ' |
| 830 | + '(Default: false)')) |
| 831 | + parser.add_argument( |
| 832 | + '--pr', |
| 833 | + action='store_true', |
| 834 | + default=False, |
| 835 | + help=('Output precision and recall as well as the f-score. ' |
| 836 | + 'Default: false')) |
| 837 | + parser.add_argument( |
| 838 | + '--justinstance', |
| 839 | + action='store_true', |
| 840 | + default=False, |
| 841 | + help="just pay attention to matching instances") |
| 842 | + parser.add_argument( |
| 843 | + '--justattribute', |
| 844 | + action='store_true', |
| 845 | + default=False, |
| 846 | + help="just pay attention to matching attributes") |
| 847 | + parser.add_argument( |
| 848 | + '--justrelation', |
| 849 | + action='store_true', |
| 850 | + default=False, |
| 851 | + help="just pay attention to matching relations") |
| 852 | + |
| 853 | + args = parser.parse_args() |
889 | 854 | main(args)
|
0 commit comments