Package nltk :: Package sem :: Module util
[hide private]
[frames] | no frames]

Module util

source code

Utility functions for batch-processing sentences: parsing and extraction of the semantic representation of the root node of the the syntax tree, followed by evaluation of the semantic representation in a first-order model.

Functions [hide private]
dict
batch_parse(inputs, grammar, trace=0)
Convert input sentences into syntactic trees.
source code
logic.Expression
root_semrep(syntree, semkey='SEM')
Find the semantic representation at the root of a tree.
source code
dict
batch_interpret(inputs, grammar, semkey='SEM', trace=0)
Add the semantic representation to each syntactic parse tree of each input sentence.
source code
dict
batch_evaluate(inputs, grammar, model, assignment, trace=0)
Add the truth-in-a-model value to each semantic representation for each syntactic parse of each input sentences.
source code
tuple
parse_valuation_line(s)
Parse a line in a valuation file.
source code
Valuation
parse_valuation(s)
Convert a valuation file into a valuation.
source code
list of Expression
parse_logic(s, logic_parser=None)
Convert a file of First Order Formulas into a list of {Expression}s.
source code
 
skolemize(expression, univ_scope=None, used_variables=None)
Skolemize the expression and convert to conjunctive normal form (CNF)
source code
 
to_cnf(first, second)
Convert this split disjunction to conjunctive normal form (CNF)
source code
 
demo_model0() source code
 
read_sents(file) source code
 
demo_legacy_grammar()
Check that batch_interpret() is compatible with legacy grammars that use a lowercase 'sem' feature.
source code
 
demo() source code
Variables [hide private]
  _VAL_SPLIT_RE = re.compile(r'\s*=+>\s*')
  _ELEMENT_SPLIT_RE = re.compile(r'\s*,\s*')
  _TUPLES_RE = re.compile(r'(?x)\s*(\([^\)]+\))\s*')
  APP = 'APP'
Function Details [hide private]

batch_parse(inputs, grammar, trace=0)

source code 

Convert input sentences into syntactic trees.

Parameters:
  • inputs (list of str) - sentences to be parsed
  • grammar - FeatureGrammar or name of feature-based grammar
Returns: dict
a mapping from input sentences to a list of Trees

root_semrep(syntree, semkey='SEM')

source code 

Find the semantic representation at the root of a tree.

Parameters:
  • syntree - a parse Tree
  • semkey - the feature label to use for the root semantics in the tree
Returns: logic.Expression
the semantic representation at the root of a Tree

batch_interpret(inputs, grammar, semkey='SEM', trace=0)

source code 

Add the semantic representation to each syntactic parse tree of each input sentence.

Parameters:
  • inputs - a list of sentences
  • grammar - FeatureGrammar or name of feature-based grammar
Returns: dict
a mapping from sentences to lists of pairs (parse-tree, semantic-representations)

batch_evaluate(inputs, grammar, model, assignment, trace=0)

source code 

Add the truth-in-a-model value to each semantic representation for each syntactic parse of each input sentences.

Parameters:
  • inputs - a list of sentences
  • grammar - FeatureGrammar or name of feature-based grammar
Returns: dict
a mapping from sentences to lists of triples (parse-tree, semantic-representations, evaluation-in-model)

parse_valuation_line(s)

source code 

Parse a line in a valuation file.

Lines are expected to be of the form:

 noosa => n
 girl => {g1, g2}
 chase => {(b1, g1), (b2, g1), (g1, d1), (g2, d2)}
Parameters:
  • s (str) - input line
Returns: tuple
a pair (symbol, value)

parse_valuation(s)

source code 

Convert a valuation file into a valuation.

Parameters:
  • s (str) - the contents of a valuation file
Returns: Valuation
a nltk.sem valuation

parse_logic(s, logic_parser=None)

source code 

Convert a file of First Order Formulas into a list of {Expression}s.

Parameters:
  • s (str) - the contents of the file
  • logic_parser (LogicParser) - The parser to be used to parse the logical expression
Returns: list of Expression
a list of parsed formulas.

demo_legacy_grammar()

source code 

Check that batch_interpret() is compatible with legacy grammars that use a lowercase 'sem' feature.

Define 'test.fcfg' to be the following