Package nltk_lite :: Package semantics :: Module utilities
[hide private]
[frames] | no frames]

Source Code for Module nltk_lite.semantics.utilities

  1  # Natural Language Toolkit: Semantic Interpretation 
  2  # 
  3  # Copyright (C) 2001-2007 University of Pennsylvania 
  4  # Author: Ewan Klein <ewan@inf.ed.ac.uk> 
  5  # URL: <http://nltk.sf.net> 
  6  # For license information, see LICENSE.TXT 
  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  ## Utility functions for connecting parse output to semantics 
 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
39 -def root_node(syntree, start='S'):
40 """ 41 Find the root node in a syntactic tree. 42 """ 43 # check that we have a tree 44 assert isinstance(syntree, Tree) 45 # in the featurechart parser, the root node is '[INIT]' 46 # so go down to the first child if necessary 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):
55 """ 56 Find the semantic representation at a given tree node. 57 """ 58 # check that we have a GrammarCategory 59 assert isinstance(node, GrammarCategory) 60 try: 61 semrep = node.get_feature('sem') 62 if beta_reduce: 63 semrep = semrep.simplify() 64 return semrep 65 except KeyError: 66 print "Node has no 'sem' feature specification" 67 raise
68
69 -def root_semrep(syntree, beta_reduce=True, start='S'):
70 """ 71 Find the semantic representation at the root of a tree. 72 """ 73 node = root_node(syntree, start=start) 74 return semrep(node, beta_reduce=beta_reduce)
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