nltk.sem.util module

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.

nltk.sem.util.demo()[source]
nltk.sem.util.demo_legacy_grammar()[source]

Check that interpret_sents() is compatible with legacy grammars that use a lowercase ‘sem’ feature.

Define ‘test.fcfg’ to be the following

nltk.sem.util.demo_model0()[source]
nltk.sem.util.evaluate_sents(inputs, grammar, model, assignment, trace=0)[source]

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

Parameters
  • inputs (list(str)) – a list of sentences

  • grammar (nltk.grammar.FeatureGrammar) – FeatureGrammar or name of feature-based grammar

Returns

a mapping from sentences to lists of triples (parse-tree, semantic-representations, evaluation-in-model)

Return type

list(list(tuple(nltk.tree.Tree, nltk.sem.logic.ConstantExpression, bool or dict(str): bool)))

nltk.sem.util.interpret_sents(inputs, grammar, semkey='SEM', trace=0)[source]

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

Parameters
  • inputs (list(str)) – a list of sentences

  • grammar (nltk.grammar.FeatureGrammar) – FeatureGrammar or name of feature-based grammar

Returns

a mapping from sentences to lists of pairs (parse-tree, semantic-representations)

Return type

list(list(tuple(nltk.tree.Tree, nltk.sem.logic.ConstantExpression)))

nltk.sem.util.parse_sents(inputs, grammar, trace=0)[source]

Convert input sentences into syntactic trees.

Parameters
  • inputs (list(str)) – sentences to be parsed

  • grammar (nltk.grammar.FeatureGrammar) – FeatureGrammar or name of feature-based grammar

Return type

list(nltk.tree.Tree) or dict(list(str)): list(Tree)

Returns

a mapping from input sentences to a list of Tree instances.

nltk.sem.util.read_sents(filename, encoding='utf8')[source]
nltk.sem.util.root_semrep(syntree, semkey='SEM')[source]

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

the semantic representation at the root of a Tree

Return type

sem.Expression