Skip to content

Commit

Permalink
implement connectives and cleanup agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaniel Imel authored and Nathaniel Imel committed Jan 6, 2025
1 parent d0570b8 commit 46aa081
Show file tree
Hide file tree
Showing 24 changed files with 984,004 additions and 25 deletions.
4 changes: 4 additions & 0 deletions src/examples/connectives/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import pandas as pd

if __name__ == "__main__":
lang_data = pd.read_csv("../data/natural_language_indefinites.csv")
Empty file.
3 changes: 3 additions & 0 deletions src/examples/connectives/grammar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from ultk.language.grammar import Grammar, Rule

connectives_grammar = Grammar.from_module("connectives.grammar_functions")
21 changes: 21 additions & 0 deletions src/examples/connectives/grammar_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from ultk.language.semantics import Referent


def _and(a: bool, b: bool) -> bool:
return a and b


def _or(a: bool, b: bool) -> bool:
return a or b


def _not(a: bool) -> bool:
return not a


def _p(w: Referent, name="P") -> bool:
return w.name == "w1" or w.name == "w2"


def _q(w: Referent, name="Q") -> bool:
return w.name == "w1" or w.name == "w3"
5 changes: 5 additions & 0 deletions src/examples/connectives/meaning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import pandas as pd
from ultk.language.semantics import Universe

referents = pd.read_csv("connectives/referents.csv")
universe = Universe.from_dataframe(referents)
52 changes: 52 additions & 0 deletions src/examples/connectives/measures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from ultk.effcomm.informativity import informativity
from ultk.language.grammar import GrammaticalExpression, Grammar
from ultk.language.language import Language, aggregate_expression_complexity, Expression
from ultk.language.semantics import Meaning

from .meaning import universe as connectives_universe


def complexity(
language: Language, expressions_by_meaning: dict[Meaning, GrammaticalExpression]
) -> float:
"""Get complexity of a language via minimal expression length in LoT.
Args:
language: the Language to measure
expressions_by_meaning: a dictionary with keys as `Meaning`s, that returns the shortest GrammaticalExpression which expresses that Meaning
Returns:
sum of the length of the shortest LoT expression for each meaning in the language
"""
return aggregate_expression_complexity(
language, lambda expr: len(expressions_by_meaning[expr.meaning])
)


prior = connectives_universe.prior_numpy


# TODO: Uegaki (2022) includes sophisticated scalar inference in informativity. For now, we just assume bayesian pragmatic listener.
def comm_cost(language: Language) -> float:
"""Get C(L) := 1 - informativity(L).
Passes in the prior from `connectives_universe` to ultk's informativity calculator.
"""
return 1 - informativity(language, prior, agent_type="pragmatic")


def swap_p_q(input_string):
return input_string.translate(str.maketrans("PQ", "QP"))


def commutative(gr_expr: Expression, grammar: Grammar) -> bool:
original_meaning = gr_expr.evaluate(connectives_universe)
# this is a little hacky
expr_swapped = str(gr_expr).translate(str.maketrans("PQ", "QP"))
gr_expr_swapped = grammar.parse(expr_swapped)
candidate_meaning = gr_expr_swapped.evaluate(connectives_universe)

return original_meaning == candidate_meaning


def commutative_only(language: Language, grammar: Grammar) -> bool:
return all(commutative(e, grammar) for e in language.expressions)
Loading

0 comments on commit 46aa081

Please sign in to comment.