git @ Cat's Eye Technologies relwrite / cbecdae
If `--output-file` not given, dump result to stdout. Chris Pressey 8 months ago
3 changed file(s) with 24 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
4545 of memory and only taking a few hours of processor time:
4646
4747 ```
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
4951 ```
5052
5153 Parse a really long string from a non-terminal in a grammar, without running out
7577
7678 Analyze the input grammar and classify it in the Chomsky hierarchy.
7779
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.
8082
8183 Allow strategies to be defined richly, perhaps in JSON files, and let
8284 them configure parameters like beam width, max rewrites per utterance, etc.
8888 working_utterances = sorted(working_utterances, key=scoring_functions[strategy])[:beam_width]
8989
9090 for utterance in final_utterances:
91 print(' '.join(utterance))
9291 collected_utterances.append(utterance)
9392 num_derivations += 1
9493 if max_derivations and num_derivations >= max_derivations:
00 from argparse import ArgumentParser
11 import json
2 import sys
23
34 from .engine import derive
45
1516 help='name of JSON file containing the grammar to use'
1617 )
1718 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"
2022 )
2123
2224 # Options that affect the process
3537 "--start-set-file", metavar='FILENAME', type=str, default=None,
3638 help="Use the set of utterances in this JSON file as "
3739 "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"
3846 )
3947
4048 argparser.add_argument(
114122 beam_width=options.beam_width,
115123 )
116124
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")
119134
120135
121136 if __name__ == '__main__':