-
Notifications
You must be signed in to change notification settings - Fork 1
/
analyse.py
59 lines (49 loc) · 1.69 KB
/
analyse.py
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
#!/usr/bin/env python
import argparse
import sys
import nltk
from nltk import FeatureChartParser
from nltk.grammar import FeatureGrammar, FeatStructNonterminal
def load_grammars(grammar_paths, start_symbol=None):
"""Loads grammars from a list of files and combines them into a single feature grammar"""
if start_symbol is None:
start_symbol = FeatStructNonterminal('Sentence')
productions = []
for path in grammar_paths:
g = nltk.data.load(path, format='fcfg')
productions.extend(g.productions())
grammar = FeatureGrammar(start_symbol, productions)
return grammar
def main(argv):
"""
Loads grammar files from command-line args then parses lines from standard input.
"""
parser = argparse.ArgumentParser()
parser.add_argument(dest='grammars', nargs='+',
help='Grammar file path(s)')
parser.add_argument('--draw', dest='draw', action='store_true',
help='Draw trees')
parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
help='Be verbose')
args = parser.parse_args(argv)
grammar = load_grammars(args.grammars)
parser = FeatureChartParser(grammar, trace=args.verbose, trace_chart_width=80)
line = sys.stdin.readline()
while line:
if line[0] == '#': continue
tokens = line.lower().strip().split()
if len(tokens) == 0: continue
trees = list(parser.parse(tokens))
print('*** {} ***'.format(tokens))
if trees:
for tree in trees:
print(tree.pformat(margin=80))
if args.draw:
tree.draw()
# print(TreePrettyPrinter(tree).text())
else:
print('Could not parse {}'.format(tokens))
print('\n')
line = sys.stdin.readline()
if __name__ == '__main__':
main(sys.argv[1:])