Semantics

These are the classes that allow you to evaluate Language of Thought expressions generated by Semantic lexica.

class python_mg.semantics.Meaning(expr)

A language of thought expression that has been parsed.

You can always use a string instead of this class, but this class allows you to save time on parsing the LOT expression if you use it a lot.

Parameters:

s (str) – A Language of Thought Expression

apply(psi, reduce=True)

Applies psi to self.

Examples

Applying an argument to a function.

alpha = Meaning("lambda a x pa_nice(x) & pa_friendly(x)")
beta = Meaning("a_John")
assert Meaning("pa_nice(a_John) & pa_friendly(a_John)") == alpha.apply(beta)
Parameters:
  • psi (Meaning | str) – The argument that is to be applied.

  • reduce (bool) – Whether to reduce immediately after application or not (true by default)

Returns:

The resulting meaning after applying the argument

Return type:

Meaning

Raises:

ValueError – If the expression is of the wrong type or if the meaning is an unparseable string.

bind_free_variable(free_var, value, reduce=True)

Binds a free variable

Examples

Binding a free variable with a string.

psi = Meaning("pa_nice(Johnny#a) & pa_friendly(Johnny#a)") # "Johnny#a" is a free variable.
x = psi.bind_free_variable("Johnny", "a_John")
assert x == Meaning("pa_nice(a_John) & pa_friendly(a_John)")

Binding a free variable with an integer.

psi = Meaning("pa_nice(343#a) & pa_friendly(343#a)") # "343#a" is an integer free variable.
x = psi.bind_free_variable(343, "a_John")
assert x == Meaning("pa_nice(a_John) & pa_friendly(a_John)")
Parameters:
  • free_var (str | int) – The name (or int) of the free variables

  • value (Meaning | str) – The value of the free variable.

  • reduce (bool) – Whether to reduce immediately after application or not (true by default)

Returns:

The resulting meaning after binding the free variable.

Return type:

Meaning

Raises:

ValueError – If the free variable’s expression is of the wrong type if the meaning is an unparseable string.

reduce()

Reduces an expression.

Returns:

The resulting meaning after reduction.

Return type:

Meaning

Raises:

ValueError – If there is an error in how the meaning is constructed leading the reduction to fail.

class python_mg.semantics.Scenario(actors, events, questions)

Represents a Scenario, a model that meanings are evaluated in.

Parameters:
  • actors (list[Actor]) – The actors present in the scenario

  • events (list[str]) – The events happening in the scenario

  • events – The questions in a scenario. (Will raise a ValueError if set with a str which is not a valid Language of Thought expression)

  • questions (list[str | Meaning]) – Any questions to be asked in this scenario. (Must be LOT expressions)

Raises:

ValueError – If the questions are strings which are not proper LOT expressions.

actors

A list of Actors in the scenario

static all_scenarios(actors, event_kinds, actor_properties, max_number_of_events=None, max_number_of_actors=None, max_number_of_actor_properties=None)

Creates a generator that goes over all possible scenarios that can be generated according to the its parameters. This gets very large very quickly.

Parameters:
  • actors (list[str]) – The actors who may or may not be present.

  • event_kinds (list[PossibleEvent]) – The possible kinds of events

  • actor_properties (list[str]) – The possible predicates that can apply to actors

  • max_number_of_events (int | None) – The maximum number of events in a given scenario (default is None, so unbounded)

  • max_number_of_actors (int | None) – The maximum number of actors in a given scenario (default is None, so unbounded)

  • max_number_of_actor_properties (int | None) – The maximum number of properties an actor can have in a given scenario (default is None, so unbounded)

Return type:

ScenarioGenerator

evaluate(expression, max_steps=64, timeout=None)

Executes an language of thought expression in this scenario. Will potentially throw a PresuppositionException if something is referenced that isn’t in the scenario. It will also reduce any lambda expressions if possible, and then will only execute the expression if it is fully reducible.

Parameters:
  • expression (Meaning | str) – The expression in the language of thought to execute.

  • max_steps (int or None, optional) – The number of steps in the virtual machine to execute before giving up. Default is 64.

  • timeout (datetime.timedelta or None, optional) – The amount of time before the execution gives up. Default is None

Returns:

the value of the expression

Return type:

bool or Actor or Event or set[Actor] or set[Event]

Raises:

ValueError – If the expression is a string which is incorrectly formatted. If the expression’s lambda terms cannot be fully reduced. If there is a presupposition error.

events

A list of Events in the scenario

static from_str(s)

Parse a scenario from a string description:

Parameters:

s (str) – The description of the scenario.

Returns:

The scenario described by the string.

Return type:

Scenario

Raises:

ValueError – If the expression is not a valid description of a scenario

questions

A list of questions to be asked in the scenario

class python_mg.semantics.Actor(name, properties=None)

Represents an actor with a name and a set of properties to be used in Scenarios.

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

  • properties (set[str], optional) – Any properties that apply to the actor. Defaults to an empty set.

Examples

Creating an actor and modifying its properties:

actor = Actor("John", properties={"mean", "unfriendly"})
actor.name = "Alice"
actor.properties = {"nice", "friendly"}
name

The name of the actor

properties

An unordered set of properties that apply to this actor

class python_mg.semantics.Event(agent=None, patient=None, properties=None)

Represents an event to be used in a Scenario.

Parameters:
  • agent (str, optional) – The name of the agent (if there is one)

  • patient (str, optional) – The name of the patient (if there is one)

  • properties (set[str], optional) – Any properties that apply to the event. Defaults to an empty set.

Examples

Creating an event

running = Actor(agent="John", properties={"run", "quickly"})
agent

The agent of the event.

patient

The patient of the event.

properties

Any properties of the event.

class python_mg.semantics.PossibleEvent(name, has_agent=True, has_patient=False, is_reflexive=True)

A possible linguistic event with theta role structure.

Parameters:
  • name (str) – Identifier for the event.

  • has_agent (bool, optional) – Whether the event has an agent participant. Default is True.

  • has_patient (bool, optional) – Whether the event has a patient participant. Default is False.

  • is_reflexive (bool, optional) – Whether the event allows reflexive construal. Default is True.

event_type()

Classify the event based on its argument structure.

Return type:

Literal[‘Transitive’, ‘TransitiveNonReflexive’, ‘Unergative’, ‘Unaccusative’, ‘Avalent’].

has_agent

Whether the event takes an agent

has_patient

Whether the event takes a patient

is_reflexive

Whether the event can have the same agent and patient

name

The name of this kind of event (e.g. running could be a unaccusative event)

class python_mg.semantics.ScenarioGenerator
Yields:

Scenario – Another scenario that can be generated according to the parameters.