-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement connectives and cleanup agent
- Loading branch information
Nathaniel Imel
authored and
Nathaniel Imel
committed
Jan 6, 2025
1 parent
d0570b8
commit 46aa081
Showing
24 changed files
with
984,004 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.