If `--output-file` not given, dump result to stdout.
Chris Pressey
8 months ago
45 | 45 | of memory and only taking a few hours of processor time: |
46 | 46 | |
47 | 47 | ``` |
48 | ./bin/relwrite eg/recursive-grammar.json --start "<Sentence>" --max-derivations=1 --strategy=expand --expand-until=3000 | |
48 | ./bin/relwrite eg/recursive-grammar.json --start "<Sentence>" \ | |
49 | --max-derivations=1 --strategy=expand --expand-until=3000 \ | |
50 | --output-file=out.json | |
49 | 51 | ``` |
50 | 52 | |
51 | 53 | Parse a really long string from a non-terminal in a grammar, without running out |
75 | 77 | |
76 | 78 | Analyze the input grammar and classify it in the Chomsky hierarchy. |
77 | 79 | |
78 | If the input grammar is context-free, use an algorithm such as Earley or CYK to | |
79 | efficiently parse or generate it. | |
80 | If the input grammar is context-free, use a chart parsing algorithm to | |
81 | efficiently parse it, or an incremental algorithm to generate from it. | |
80 | 82 | |
81 | 83 | Allow strategies to be defined richly, perhaps in JSON files, and let |
82 | 84 | them configure parameters like beam width, max rewrites per utterance, etc. |
88 | 88 | working_utterances = sorted(working_utterances, key=scoring_functions[strategy])[:beam_width] |
89 | 89 | |
90 | 90 | for utterance in final_utterances: |
91 | print(' '.join(utterance)) | |
92 | 91 | collected_utterances.append(utterance) |
93 | 92 | num_derivations += 1 |
94 | 93 | if max_derivations and num_derivations >= max_derivations: |
0 | 0 | from argparse import ArgumentParser |
1 | 1 | import json |
2 | import sys | |
2 | 3 | |
3 | 4 | from .engine import derive |
4 | 5 | |
15 | 16 | help='name of JSON file containing the grammar to use' |
16 | 17 | ) |
17 | 18 | argparser.add_argument( |
18 | '--output-filename', '-o', metavar='FILENAME', type=str, default='out.json', | |
19 | help='name of file to write JSON-formatted output to' | |
19 | '--output-file', '-o', metavar='FILENAME', type=str, default=None, | |
20 | help="name of file to write JSON-formatted output to; " | |
21 | "if not given, it will be written to standard output" | |
20 | 22 | ) |
21 | 23 | |
22 | 24 | # Options that affect the process |
35 | 37 | "--start-set-file", metavar='FILENAME', type=str, default=None, |
36 | 38 | help="Use the set of utterances in this JSON file as " |
37 | 39 | "the starting point of the derivation" |
40 | ) | |
41 | argparser.add_argument( | |
42 | "--goal", metavar='UTTERANCE', type=str, default=None, | |
43 | help="A single utterance; if given, the processor expects it " | |
44 | "to be the final result of the derivation; if it is not, " | |
45 | "exits with a non-zero error code" | |
38 | 46 | ) |
39 | 47 | |
40 | 48 | argparser.add_argument( |
114 | 122 | beam_width=options.beam_width, |
115 | 123 | ) |
116 | 124 | |
117 | with open(options.output_filename, 'w') as f: | |
118 | f.write(json.dumps(result, indent=4)) | |
125 | if options.goal: | |
126 | raise NotImplementedError(str(result)) | |
127 | ||
128 | if options.output_file: | |
129 | with open(options.output_file, 'w') as f: | |
130 | f.write(json.dumps(result, indent=4)) | |
131 | else: | |
132 | sys.stdout.write(json.dumps(result, indent=4)) | |
133 | sys.stdout.write("\n") | |
119 | 134 | |
120 | 135 | |
121 | 136 | if __name__ == '__main__': |