1
2
3
4
5
6
7
8 """
9 Utility functions for batch-processing sentences: parsing and
10 extraction of the semantic representation of the root node of the the
11 syntax tree, followed by evaluation of the semantic representation in
12 a first-order model.
13 """
14
15
16 from nltk_lite import tokenize
17 from nltk_lite.parse.category import *
18 from nltk_lite.parse.grammarfile import *
19 from nltk_lite.parse.tree import Tree
20 from evaluate import *
21 from logic import *
22
23
24
25
26
27 -def text_parse(inputs, grammar, trace=0):
28 """
29 Convert input sentences into syntactic trees.
30 """
31 parses = {}
32 for sent in inputs:
33 tokens = list(tokenize.whitespace(sent))
34 parser = grammar.earley_parser(trace=trace)
35 syntrees = parser.get_parse_list(tokens)
36 parses[sent] = syntrees
37 return parses
38
40 """
41 Find the root node in a syntactic tree.
42 """
43
44 assert isinstance(syntree, Tree)
45
46
47 if syntree.node.head() == start:
48 return syntree.node
49 elif syntree[0].node.head() == start:
50 return syntree[0].node
51 else:
52 raise ValueError("Tree not rooted in %s node" % start)
53
54 -def semrep(node, beta_reduce=True):
68
75
76 -def text_interpret(inputs, grammar, beta_reduce=True, start='S', syntrace=0):
77 """
78 Add the semantic representation to each syntactic parse tree
79 of each input sentence.
80 """
81 parses = text_parse(inputs, grammar, trace=syntrace)
82 semreps = {}
83 for sent in inputs:
84 syntrees = parses[sent]
85 syn_sem = \
86 [(syn, root_semrep(syn, beta_reduce=beta_reduce, start=start)) for syn in syntrees]
87 semreps[sent] = syn_sem
88 return semreps
89
90 -def text_evaluate(inputs, grammar, model, assignment, semtrace=0):
91 """
92 Add the truth-in-a-model value to each semantic representation
93 for each syntactic parse of each input sentences.
94 """
95 g = assignment
96 m = model
97 semreps = text_interpret(inputs, grammar)
98 evaluations = {}
99 for sent in inputs:
100 syn_sem_val = \
101 [(syn, sem, m.evaluate(str(sem), g, trace=semtrace)) for (syn, sem) in semreps[sent]]
102 evaluations[sent] = syn_sem_val
103 return evaluations
104