nltk.inference.discourse module¶
Module for incrementally developing simple discourses, and checking for semantic ambiguity, consistency and informativeness.
Many of the ideas are based on the CURT family of programs of Blackburn and Bos (see http://homepages.inf.ed.ac.uk/jbos/comsem/book1.html).
Consistency checking is carried out by using the mace module to call the Mace4 model builder.
Informativeness checking is carried out with a call to Prover.prove() from
the inference module.
DiscourseTester is a constructor for discourses.
The basic data structure is a list of sentences, stored as self._sentences. Each sentence in the list
is assigned a “sentence ID” (sid) of the form si. For example:
s0: A boxer walks
s1: Every boxer chases a girl
Each sentence can be ambiguous between a number of readings, each of which receives a
“reading ID” (rid) of the form si -rj. For example:
s0 readings:
s0-r1: some x.(boxer(x) & walk(x))
s0-r0: some x.(boxerdog(x) & walk(x))
A “thread” is a list of readings, represented as a list of rids.
Each thread receives a “thread ID” (tid) of the form di.
For example:
d0: ['s0-r0', 's1-r0']
The set of all threads for a discourse is the Cartesian product of all the readings of the sequences of sentences.
(This is not intended to scale beyond very short discourses!) The method readings(filter=True) will only show
those threads which are consistent (taking into account any background assumptions).
- class nltk.inference.discourse.CfgReadingCommand[source]¶
Bases:
ReadingCommand
- class nltk.inference.discourse.DiscourseTester[source]¶
Bases:
objectCheck properties of an ongoing discourse.
- __init__(input, reading_command=None, background=None)[source]¶
Initialize a
DiscourseTester.- Parameters:
input (list of str) – the discourse sentences
background (list(Expression)) – Formulas which express background assumptions
- add_background(background, verbose=False)[source]¶
Add a list of background assumptions for reasoning about the discourse.
When called, this method also updates the discourse model’s set of readings and threads. :param background: Formulas which contain background information :type background: list(Expression)
- add_sentence(sentence, informchk=False, consistchk=False)[source]¶
Add a sentence to the current discourse.
Updates
self._inputandself._sentences. :param sentence: An input sentence :type sentence: str :param informchk: ifTrue, check that the result of adding the sentence is thread-informative. Updatesself._readings. :param consistchk: ifTrue, check that the result of adding the sentence is thread-consistent. Updatesself._readings.
- expand_threads(thread_id, threads=None)[source]¶
Given a thread ID, find the list of
logic.Expressionobjects corresponding to the reading IDs in that thread.- Parameters:
thread_id (str) – thread ID
threads (dict) – a mapping from thread IDs to lists of reading IDs
- Returns:
A list of pairs
(rid, reading)where reading is thelogic.Expressionassociated with a reading ID- Return type:
list of tuple
- models(thread_id=None, show=True, verbose=False)[source]¶
Call Mace4 to build a model for each current discourse thread.
- Parameters:
thread_id (str) – thread ID
show – If
True, display the model that has been found.
- static multiply(discourse, readings)[source]¶
Multiply every thread in
discourseby every reading inreadings.Given discourse = [[‘A’], [‘B’]], readings = [‘a’, ‘b’, ‘c’] , returns [[‘A’, ‘a’], [‘A’, ‘b’], [‘A’, ‘c’], [‘B’, ‘a’], [‘B’, ‘b’], [‘B’, ‘c’]]
- Parameters:
discourse (list of lists) – the current list of readings
readings (list(Expression)) – an additional list of readings
- Return type:
A list of lists
- readings(sentence=None, threaded=False, verbose=True, filter=False, show_thread_readings=False)[source]¶
Construct and show the readings of the discourse (or of a single sentence).
- Parameters:
sentence (str) – test just this sentence
threaded – if
True, print out each thread ID and the corresponding thread.filter – if
True, only print out consistent thread IDs and threads.
- class nltk.inference.discourse.DrtGlueReadingCommand[source]¶
Bases:
ReadingCommand
- class nltk.inference.discourse.ReadingCommand[source]¶
Bases:
object- abstractmethod combine_readings(readings)[source]¶
- Parameters:
readings (list(Expression)) – readings to combine
- Returns:
one combined reading
- Return type:
- abstractmethod parse_to_readings(sentence)[source]¶
- Parameters:
sentence (str) – the sentence to read
- process_thread(sentence_readings)[source]¶
This method should be used to handle dependencies between readings such as resolving anaphora.
- Parameters:
sentence_readings (list(Expression)) – readings to process
- Returns:
the list of readings after processing
- Return type:
list(Expression)
- abstractmethod to_fol(expression)[source]¶
Convert this expression into a First-Order Logic expression.
- Parameters:
expression (Expression) – an expression
- Returns:
a FOL version of the input expression
- Return type:
- nltk.inference.discourse.discourse_demo(reading_command=None)[source]¶
Illustrate the various methods of
DiscourseTester
- nltk.inference.discourse.drt_discourse_demo(reading_command=None)[source]¶
Illustrate the various methods of
DiscourseTester
- nltk.inference.discourse.load_fol(s)[source]¶
Temporarily duplicated from
nltk.sem.util. Convert a file of first order formulas into a list ofExpressionobjects.- Parameters:
s (str) – the contents of the file
- Returns:
a list of parsed formulas.
- Return type:
list(Expression)