Skip to content

Commit

Permalink
run black
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaniel Imel authored and Nathaniel Imel committed Jan 10, 2025
1 parent 8ae7b3a commit 2e2ce97
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 75 deletions.
14 changes: 7 additions & 7 deletions src/examples/connectives/scripts/generate_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
from ..meaning import universe as connectives_universe

if __name__ == "__main__":
expressions_by_meaning: dict[Meaning, GrammaticalExpression] = (
connectives_grammar.get_unique_expressions(
6,
max_size=2 ** len(connectives_universe),
unique_key=lambda expr: expr.evaluate(connectives_universe),
compare_func=lambda e1, e2: len(e1) < len(e2),
)
expressions_by_meaning: dict[
Meaning, GrammaticalExpression
] = connectives_grammar.get_unique_expressions(
6,
max_size=2 ** len(connectives_universe),
unique_key=lambda expr: expr.evaluate(connectives_universe),
compare_func=lambda e1, e2: len(e1) < len(e2),
)

print(f"Generated {len(expressions_by_meaning)} unique expressions.")
Expand Down
14 changes: 7 additions & 7 deletions src/examples/indefinites/scripts/generate_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
from ..meaning import universe as indefinites_universe

if __name__ == "__main__":
expressions_by_meaning: dict[Meaning, GrammaticalExpression] = (
indefinites_grammar.get_unique_expressions(
5,
max_size=2 ** len(indefinites_universe),
unique_key=lambda expr: expr.evaluate(indefinites_universe),
compare_func=lambda e1, e2: len(e1) < len(e2),
)
expressions_by_meaning: dict[
Meaning, GrammaticalExpression
] = indefinites_grammar.get_unique_expressions(
5,
max_size=2 ** len(indefinites_universe),
unique_key=lambda expr: expr.evaluate(indefinites_universe),
compare_func=lambda e1, e2: len(e1) < len(e2),
)

# filter out the trivial meaning, results in NaNs
Expand Down
1 change: 0 additions & 1 deletion src/examples/kinship/meaning/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,5 @@ def test_structure(kinship_structure, domain, parent_child_data, sex_data):
)

if __name__ == "__main__":

# Run a minimal test 'suite'
test_structure(kinship_structure, domain, parent_child_data, sex_data)
18 changes: 8 additions & 10 deletions src/examples/kinship/scripts/generate_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


def write_data(expressions_by_meaning: dict[Meaning, GrammaticalExpression]) -> None:

# For inspecting
fn = "kinship/outputs/expressions_and_extensions.txt"
results = {
Expand All @@ -32,15 +31,14 @@ def write_data(expressions_by_meaning: dict[Meaning, GrammaticalExpression]) ->


if __name__ == "__main__":

expressions_by_meaning: dict[Meaning, GrammaticalExpression] = (
kinship_grammar.get_unique_expressions(
5, # I found 6 is too high
max_size=2 ** len(kinship_universe),
# max_size=100,
unique_key=lambda expr: expr.evaluate(kinship_universe),
compare_func=lambda e1, e2: len(e1) < len(e2),
)
expressions_by_meaning: dict[
Meaning, GrammaticalExpression
] = kinship_grammar.get_unique_expressions(
5, # I found 6 is too high
max_size=2 ** len(kinship_universe),
# max_size=100,
unique_key=lambda expr: expr.evaluate(kinship_universe),
compare_func=lambda e1, e2: len(e1) < len(e2),
)

# filter out the trivial meaning, results in NaNs
Expand Down
3 changes: 2 additions & 1 deletion src/examples/quantifiers/grammar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ultk.language.grammar import Grammar
from ultk.language.grammar import Grammar

quantifiers_grammar = Grammar.from_yaml("quantifiers/grammar/grammar.yml")
# quantifiers_grammar = Grammar.from_module("quantifiers.grammar.full")
quantifiers_grammar_natural = Grammar.from_module("quantifiers.grammar.natural")
quantifiers_grammar_natural = Grammar.from_module("quantifiers.grammar.natural")
20 changes: 20 additions & 0 deletions src/examples/quantifiers/grammar/full.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,88 @@

# Boolean operations


# bool -> bool bool
def _and(p: bool, q: bool, name="and") -> bool:
return p and q


def _or(p: bool, q: bool, name="or") -> bool:
return p or q


# bool -> bool
def _not(p: bool, name="not") -> bool:
return not p


# Set operations


# bool -> frozenset frozenset
def intersection(a: frozenset, b: frozenset) -> bool:
return a & b


def union(a: frozenset, b: frozenset) -> bool:
return a | b


def subset(a: frozenset, b: frozenset) -> bool:
return a < b


# int -> frozenset
def cardinality(a: frozenset) -> int:
return len(a)


# Numeric operations


# float -> int int
def divide(x: int, y: int, name="/") -> float:
return x / y if y > 0 else 0


# int -> int int
def plus(x: int, y: int, name="+") -> int:
return x + y


def minus(x: int, y: int, name="-") -> int:
return x - y


# bool -> int int
def gt(x: int, y: int, name=">") -> bool:
return x > y


def eq(x: int, y: int, name="=") -> bool:
return x == y


def mod(x: int, y: int, name="%") -> bool:
return x % y if y > 0 else 0


# bool -> float float
def eqf(x: float, y: float, name="=f") -> bool:
return math.isclose(x, y)


def gtf(x: float, y: float, name=">f") -> bool:
return x > y


# Primitives


# frozenset -> model
def _A(model: QuantifierModel, name="A") -> frozenset:
return model.A


def _A(model: QuantifierModel, name="B") -> frozenset:
return model.B
24 changes: 21 additions & 3 deletions src/examples/quantifiers/grammar/natural.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,89 @@

# Boolean operations


# bool -> bool bool
def _and(p: bool, q: bool, name="and") -> bool:
return p and q


def _or(p: bool, q: bool, name="or") -> bool:
return p or q


# bool -> bool
def _not(p: bool, name="not") -> bool:
return not p


# Set operations

# we removed union and subset.
# Generalized Existential: depending only on |A \cap B|
# Generalized Intersective: depending only on |A \ B|
# Proportional: depending only on comparing |A \cap B| and |A \ B|
# Generalized Existential: depending only on |A \cap B|
# Generalized Intersective: depending only on |A \ B|
# Proportional: depending only on comparing |A \cap B| and |A \ B|


# bool -> frozenset frozenset
def intersection(a: frozenset, b: frozenset) -> bool:
return a & b


def diff(a: frozenset, b: frozenset) -> bool:
return a - b


# int -> frozenset
def cardinality(a: frozenset) -> int:
return len(a)


# Numeric operations


# float -> int int
def divide(x: int, y: int, name="/") -> float:
return x / y if y > 0 else 0


# int -> int int
def plus(x: int, y: int, name="+") -> int:
return x + y


def minus(x: int, y: int, name="-") -> int:
return x - y


# bool -> int int
def gt(x: int, y: int, name=">") -> bool:
return x > y


def eq(x: int, y: int, name="=") -> bool:
return x == y


def mod(x: int, y: int, name="%") -> bool:
return x % y if y > 0 else 0


# bool -> float float
def eqf(x: float, y: float, name="=f") -> bool:
return math.isclose(x, y)


def gtf(x: float, y: float, name=">f") -> bool:
return x > y


# Primitives


# frozenset -> model
def _A(model: QuantifierModel, name="A") -> frozenset:
return model.A


def _B(model: QuantifierModel, name="B") -> frozenset:
return model.B
33 changes: 15 additions & 18 deletions src/examples/quantifiers/meaning.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

from itertools import chain, combinations, permutations, product
from typing import Iterable, Iterator
from ultk.language.semantics import Universe, Meaning, Referent


class QuantifierModel(Referent):
"""A QuantifierModel represents a tuple <M, A, B> where A, B are subsets of M."""

Expand All @@ -19,10 +19,10 @@ def __str__(self) -> str:

def __hash__(self) -> int:
return hash((self.M, self.A, self.B))

def zones(self) -> tuple[int, int, int, int]:
"""Return the zones (via the name) as a tuple of four integers."""
return tuple(map(int, self.name.strip('()').split(', ')))
return tuple(map(int, self.name.strip("()").split(", ")))


def powerset(iterable) -> Iterable:
Expand All @@ -33,7 +33,6 @@ def powerset(iterable) -> Iterable:
return chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))



def all_models_up_to_size(max_size: int) -> Universe:
"""Generate all isomorphically unique quantifier models up to a specified maximum size.
Expand All @@ -44,7 +43,9 @@ def all_models_up_to_size(max_size: int) -> Universe:
Universe: A collection of isomorphically unique quantifier models.
"""

def canonical_form(model: tuple[frozenset[int], frozenset[int], frozenset[int]]) -> tuple[int, int, int, int]:
def canonical_form(
model: tuple[frozenset[int], frozenset[int], frozenset[int]]
) -> tuple[int, int, int, int]:
"""
Calculate the canonical form of a model using cardinalities of the four sets:
A ∩ B, A \ B, B \ A, and M \ (A ∪ B).
Expand All @@ -61,26 +62,21 @@ def canonical_form(model: tuple[frozenset[int], frozenset[int], frozenset[int]])
a_minus_b = A - B
b_minus_a = B - A
complement = set(M) - (A | B)
return (
len(intersection),
len(a_minus_b),
len(b_minus_a),
len(complement)
)
return (len(intersection), len(a_minus_b), len(b_minus_a), len(complement))

def generate_all_unique_referents(max_size: int) -> Iterator['QuantifierModel']:
def generate_all_unique_referents(max_size: int) -> Iterator["QuantifierModel"]:
"""
Generate all isomorphically unique quantifier models up to a specified maximum size.
Args:
max_size (int): The maximum size of the model universe.
Yields:
QuantifierModel: A quantifier model represented by a universe of objects (`M`)
QuantifierModel: A quantifier model represented by a universe of objects (`M`)
and two subsets (`A`, `B`), along with a canonical representation.
Notes:
- Models are deduplicated using the canonical form derived from the cardinalities of
- Models are deduplicated using the canonical form derived from the cardinalities of
four sets: A ∩ B, A \ B, B \ A, and M \ (A ∪ B).
"""
seen_forms = set()
Expand All @@ -92,14 +88,15 @@ def generate_all_unique_referents(max_size: int) -> Iterator['QuantifierModel']:
canonical = canonical_form(model)
if canonical not in seen_forms:
seen_forms.add(canonical)
yield QuantifierModel(frozenset(M), frozenset(A), frozenset(B), str(canonical))
yield QuantifierModel(
frozenset(M), frozenset(A), frozenset(B), str(canonical)
)

return Universe(tuple(sorted(generate_all_unique_referents(max_size))))



universe = all_models_up_to_size(7)


if __name__ == '__main__':
print(len(all_models_up_to_size(7))) # 8 was too many
if __name__ == "__main__":
print(len(all_models_up_to_size(7))) # 8 was too many
Loading

0 comments on commit 2e2ce97

Please sign in to comment.