eta.lf

Classes for representing and modifying logical forms and eventualities.

TODO: short writeup

../_images/eventuality.png

Module attributes

KEYWORDS

a list of special keywords that are reserved for operators in ULF.

KEYWORDS_R

a subset of keywords that don't mirror natural language words/punctuation.

Functions

and_prop_p

Check whether a given wff is a conjunction predicate.

characterizes_prop_p

Check whether a given wff is a characterization predicate.

equal_prop_p

Check whether a given wff is an equality predicate.

expectation_p

Check whether eventuality e is an expectation or intention.

extract_set

Extract a list of items from a wff of form [set-of, x, y, ...], or of form [x, and, y, and, ...].

from_lisp_dirs

Recursively read all LISP files in a given dir or list of dirs, returning a list of eventualities.

from_lisp_file

Read a list of eventualities from a LISP file, modifying the list in-place.

is_set

Check whether a given S-expression is a set wff.

make_set

Create a set wff from a list of items.

not_prop_p

Check whether a given wff is a negative predicate.

or_prop_p

Check whether a given wff is a disjunction predicate.

parse_condition

Parse an S-expression containing conditional keywords into a Condition eventuality.

parse_eventuality

Parse an S-expression into an eventuality.

parse_eventuality_list

Parse a list of alternating episode symbols and formulas into a list of eventualities.

parse_repetition

Parse an S-expression containing repetition keywords into a Repetition eventuality.

remove_type

Remove the type suffix from a ULF atom.

set_union

Forms a union set wff from two set wffs.

Classes

Condition

A Condition is a special type of Eventuality that represents a conditional event.

Domain

Defines a domain of individuals.

ELF

Defines an ELF formula.

Eventuality

Defines an "eventuality", which represents an event or fact at several levels (natural language, ULF, and ELF).

Individual

Defines an individual, with some canonical name and a list of aliases.

LF

Defines an abstract logical form.

Repetition

A Repetition is a special type of Eventuality that represents a repeating event.

ULF

Defines a ULF formula.

Var

Defines a variable, with some variable symbol and some value assignment.

KEYWORDS = ['not', 'plur', 'past', 'pres', 'perf', 'prog', 'pasv', 'k', 'ka', 'ke', 'to', 'that', 'tht', 'fquan', 'nquan', 'nmod', 'amod', '*h', '*s', '*p', 'set-of', 'n+preds', 'np+preds', 'sub', 'rep', "'s", 'poss-by', 'adv-a', 'adv-e', 'adv-f', 'adv-s', '?', '!', '=', 'voc', 'voc-O', 'ds', 'ans-to', 'pu', 'cf', 'mod-a', 'mod-n', 'most-n', 'poss-ques', 'poss-ans']

a list of special keywords that are reserved for operators in ULF.

Type:

list[str]

KEYWORDS_R = ['plur', 'past', 'pres', 'perf', 'prog', 'pasv', 'k', 'ka', 'ke', 'to', 'tht', 'fquan', 'nquan', 'nmod', 'amod', '*h', '*s', '*p', 'set-of', 'n+preds', 'np+preds', 'sub', 'rep', 'poss-by', 'adv-a', 'adv-e', 'adv-f', 'adv-s', '=', 'voc', 'voc-O', 'ds', 'ans-to', 'pu', 'cf', 'mod-a', 'mod-n', 'most-n', 'poss-ques', 'poss-ans']

a subset of keywords that don’t mirror natural language words/punctuation.

Type:

list[str]

remove_type(atm)[source]

Remove the type suffix from a ULF atom.

class Domain(individuals=[])[source]

Bases: object

Defines a domain of individuals.

Parameters:

individuals (list[Individual], optional) – A list of initial individuals to add to the domain.

domain

A dict mapping the canonical names of an individual to that individual object.

Type:

dict

Notes

TODO: currently incomplete and unused.

add(individual)[source]

Add an individual to the domain.

add_aliases(name, aliases)[source]

Add a list of aliases (strings) to the individual denoted by the given canonical name.

class Individual(name, aliases=[])[source]

Bases: object

Defines an individual, with some canonical name and a list of aliases.

Parameters:
  • name (str) – The canonical name of the individual.

  • aliases (list[str], optional) – A list of aliases of the individual.

name
Type:

str

aliases
Type:

list[str]

Notes

TODO: currently incomplete and unused.

add_aliases(aliases)[source]

Add a list of aliases to this individual.

class Var(var, val=None)[source]

Bases: object

Defines a variable, with some variable symbol and some value assignment.

Parameters:
  • var (str) – The variable symbol.

  • val (object, optional) – The value this variable is currently bound to. Generally this will be a string, though it may in principle be some other object (e.g., an Individual) as well.

var
Type:

str

val
Type:

object

Notes

TODO: currently incomplete and unused.

bind(val)[source]

Bind this variable to the given value.

unbind()[source]

Unbind this variable.

class LF(formula)[source]

Bases: object

Defines an abstract logical form.

A logical form (whether ULF or ELF) must contain a formula, variable bindings, functions for binding/unbinding variables, for replacing variables, and for getting the formula (or a natural language representation thereof) after making all variable assignments.

Parameters:

formula (str or s-expr) – The formula for this logical form (an S-expression or LISP-formatted string representation thereof).

formula

The formula for this logical form.

Type:

s-expr

bindings

A mapping from variables to bound values.

Type:

dict

bind(var, val)[source]

Bind the given variable symbol to the given value.

unbind(var)[source]

Unbind the given variable symbol.

replacevar(var1, var2)[source]

Replace the first variable symbol with the second variable symbol throughout the logical form.

get_formula()[source]

Get the formula, applying any variable assignments first.

to_nl()[source]

Convert the formula to a natural language string.

class ULF(formula)[source]

Bases: LF

Defines a ULF formula.

Parameters:

formula (str or s-expr) – The formula for this logical form (an S-expression or LISP-formatted string representation thereof).

class ELF(formula)[source]

Bases: LF

Defines an ELF formula.

Parameters:

formula (str or s-expr) – The formula for this logical form (an S-expression or LISP-formatted string representation thereof).

class Eventuality(ep, nl, ulf, elf, prob=1.0)[source]

Bases: object

Defines an “eventuality”, which represents an event or fact at several levels (natural language, ULF, and ELF).

An Eventuality subsumes logical forms as well as their natural language expression, encapsulating them in one object. Minimally, an Eventuality has an episode variable/constant and a natural language string expressing the formula characterizing that event. On top of that, a ULF representation of that formula may be provided. At the highest level, a fully deindexed ELF representation may be provided.

We assume that the level of representation builds upward, i.e., a natural language string may be easily derived from the ULF, and likewise, a ULF may be easily derived from the full ELF. On the other hand, mapping from natural language to ULF requires semantic parsing, and mapping from ULF to ELF requires disambiguation, scoping, and deindexing. Hence, we want to regard those upper levels of representation as “optional”, since they may not be needed in simpler dialogue applications.

Parameters:
  • ep (str) – A symbol denoting the episode variable or constant.

  • nl (str) – The natural language representation of the formula characterizing the event.

  • ulf (ULF, optional) – The ULF formula characterizing the event.

  • elf (ELF, optional) – The ELF formula characterizing the event.

  • prob (float, default=1.) – The probability associated with this event.

ep
Type:

str

nl
Type:

str

ulf
Type:

ULF or None

elf
Type:

ELF or None

prob
Type:

float

bindings

A mapping from variables to bound values.

Type:

dict

embedding

A vector embedding of this eventuality.

Type:

list[float]

set_ep(ep)[source]

Set the episode symbol.

set_ulf(ulf)[source]

Set the ULF formula.

set_elf(elf)[source]

Set the ULF formula.

set_prob(prob)[source]

Set the probability of this event.

bind(var, val)[source]

Bind the given variable symbol to the given value.

unbind(var)[source]

Unbind the given variable symbol.

replacevar(var1, var2)[source]

Replace the first variable symbol with the second variable symbol throughout the eventuality.

embed(embedder)[source]

Embed the eventuality based on the natural language representation, given an embedder object.

get_ep()[source]

Get the episode symbol for this eventuality, applying any variable assignments.

get_nl()[source]

Get the natural language representation for this eventuality, applying any variable assignments.

get_ulf()[source]

Get the ULF representation for this eventuality, applying any variable assignments.

get_elf()[source]

Get the ELF representation for this eventuality, applying any variable assignments.

get_wff(no_bind=False)[source]

Get the highest level of representation available for this eventuality (if no_bind is given as True, do not apply bindings).

format()[source]

Format an eventuality as ((<ep> <wff>) <prob>), where <wff> is the highest level of representation for this eventuality.

class Condition(ep, nl, ulf, elf, conditions, prob=1.0)[source]

Bases: Eventuality

A Condition is a special type of Eventuality that represents a conditional event.

In addition to the basic Eventuality attributes, it also has a list of (<condition>, <eventualities>) pairs, where <condition> is either a ULF or True (for a default condition), and <eventualities> is a list of sub-eventualities that occur if the condition is true.

Such an event is indicated by a :try-in-sequence or :if keyword in e.g. a schema.

ep
Type:

str

nl
Type:

str

ulf
Type:

ULF or None

elf
Type:

ELF or None

prob
Type:

float

bindings
Type:

dict

embedding
Type:

list[float]

conditions

A list of pairs of conditions and eventuality lists.

Type:

list[tuple[True or ULF, list[Eventuality]]]

bind(var, val)[source]

Bind the given variable symbol to the given value.

unbind(var)[source]

Unbind the given variable symbol.

replacevar(var1, var2)[source]

Replace the first variable symbol with the second variable symbol throughout the eventuality.

class Repetition(ep, nl, ulf, elf, condition, eventualities, prob=1.0)[source]

Bases: Eventuality

A Repetition is a special type of Eventuality that represents a repeating event.

In addition to the basic Eventuality attributes, it also has a condition and a sub-list of eventualities. The condition is either a ULF (or True for a repetition with no termination), such that the eventualities will occur until the condition is no longer true.

Such an event is indicated by a :repeat-until keyword in a schema.

ep
Type:

str

nl
Type:

str

ulf
Type:

ULF or None

elf
Type:

ELF or None

prob
Type:

float

bindings
Type:

dict

embedding
Type:

list[float]

condition

The stopping condition for repetition.

Type:

True or ULF

eventualities

The list of eventualities to repeat.

Type:

list[Eventuality]

bind(var, val)[source]

Bind the given variable symbol to the given value.

unbind(var)[source]

Unbind the given variable symbol.

replacevar(var1, var2)[source]

Replace the first variable symbol with the second variable symbol throughout the eventuality.

parse_eventuality(s, ep=None, expectation=False, prob_dict={})[source]

Parse an S-expression into an eventuality.

Parameters:
  • s (s-expr) – An S-expression to parse. If s contains a condition or repetition keyword as the first element, a Condition or Repetition object is created respectively; otherwise, a basic Eventuality object is created containing s as a formula.

  • ep (str, optional) – If given, use this episode symbol for the created eventuality, rather than generating a new symbol.

  • expectation (bool, default=False) – If True, this will interpret the eventuality as an expected episode rather than an observed episode, i.e., ep will become an episode variable instead of an episode constant.

  • prob_dict (dict, optional) – A dictionary mapping episode symbols to probabilities, used to set the probability of this eventuality.

Returns:

The eventuality generated according to the above parameters.

Return type:

Eventuality

parse_condition(s, ulf, ep, prob, prob_dict={})[source]

Parse an S-expression containing conditional keywords into a Condition eventuality.

parse_repetition(s, ulf, ep, prob, prob_dict={})[source]

Parse an S-expression containing repetition keywords into a Repetition eventuality.

parse_eventuality_list(lst, prob_dict={})[source]

Parse a list of alternating episode symbols and formulas into a list of eventualities.

Parameters:
  • lst (list[s-expr]) – A list of format [ep1, wff1, ep2, wff2, ...], where each (ep, wff) pair is to be parsed into an eventuality.

  • prob_dict (dict, optional) – A dictionary mapping episode symbols to probabilities, used to set the probability of this eventuality.

Return type:

list[Eventuality]

from_lisp_file(fname, eventualities)[source]

Read a list of eventualities from a LISP file, modifying the list in-place.

Parameters:
  • fname (str) – The LISP file to read.

  • eventualities (list[Eventuality]) – The list of eventualities to modify in-place.

from_lisp_dirs(dirs)[source]

Recursively read all LISP files in a given dir or list of dirs, returning a list of eventualities.

Parameters:

dirs (str or list[str]) – Either a directory name or a list of directory names to read.

Return type:

list[Eventuality]

expectation_p(e)[source]

Check whether eventuality e is an expectation or intention.

An eventuality is an expectation iff it is not a special eventuality type, and the subject of its wff is not Eta.

extract_set(ulf)[source]

Extract a list of items from a wff of form [set-of, x, y, ...], or of form [x, and, y, and, ...].

make_set(lst)[source]

Create a set wff from a list of items.

is_set(lst)[source]

Check whether a given S-expression is a set wff.

set_union(lst1, lst2)[source]

Forms a union set wff from two set wffs.

equal_prop_p(ulf)[source]

Check whether a given wff is an equality predicate.

not_prop_p(ulf)[source]

Check whether a given wff is a negative predicate.

and_prop_p(ulf)[source]

Check whether a given wff is a conjunction predicate.

or_prop_p(ulf)[source]

Check whether a given wff is a disjunction predicate.

characterizes_prop_p(ulf)[source]

Check whether a given wff is a characterization predicate.