Skip to content

Commit

Permalink
rename language.to_dict(), remove other to_dict() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
shanest committed Feb 23, 2024
1 parent ee9014f commit ea3d45a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/examples/indefinites/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .meaning import universe as indefinites_universe


def read_natural_languages(filename: str) -> list[Language]:
def read_natural_languages(filename: str) -> set[Language]:
"""Read the natural languages from a CSV file.
Assumes that each row is one expression, with unique strings in "language" column identifying
which expressions belong to which languages.
Expand Down Expand Up @@ -48,12 +48,12 @@ def read_natural_languages(filename: str) -> list[Language]:
# add Expression with form and Meaning
cur_expressions.append(Expression(item.expression, cur_meaning))
# add Language with its Expressions
languages.add(Language(cur_expressions, name=lang, natural=True))
languages.add(Language(tuple(cur_expressions), name=lang, natural=True))
return languages


def read_expressions(
filename: str, universe: Universe = None, return_by_meaning=True
filename: str, universe: Universe | None = None, return_by_meaning=True
) -> tuple[list[GrammaticalExpression], dict[Meaning, Expression]]:
"""Read expressions from a YAML file.
Assumes that the file is a list, and that each item in the list has a field
Expand All @@ -77,10 +77,10 @@ def read_expressions(
def write_languages(
languages: list[Language],
filename: str,
properties_to_add: dict[str, Callable[[int, Language], Any]] = None,
properties_to_add: dict[str, Callable[[int, Language], Any]] = {},
) -> None:
lang_dicts = [
language.to_dict(
language.as_dict_with_properties(
**{key: properties_to_add[key](idx, language) for key in properties_to_add}
)
for idx, language in enumerate(languages)
Expand Down
2 changes: 1 addition & 1 deletion src/ultk/language/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def add_child(self, child) -> None:
self.children = self.children + (child,)

def to_dict(self) -> dict:
the_dict = super().to_dict()
the_dict = super().__dict__
the_dict["grammatical_expression"] = str(self)
the_dict["length"] = len(self)
return the_dict
Expand Down
8 changes: 4 additions & 4 deletions src/ultk/language/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ def can_express(self, referent: Referent) -> bool:
"""Return True if the expression can express the input single meaning point and false otherwise."""
return referent in self.meaning.referents

def to_dict(self) -> dict:
return {"form": self.form, "meaning": self.meaning.to_dict()}

def __str__(self) -> str:
return self.form
# return f"Expression {self.form}\nMeaning:\n\t{self.meaning}"
Expand Down Expand Up @@ -116,7 +113,10 @@ def universe(self) -> Universe:
def universe(self, val) -> None:
self._universe = val

def to_dict(self, **kwargs) -> dict:
def as_dict_with_properties(self, **kwargs) -> dict:
"""Return a dictionary representation of the language, including additional properties as keyword arguments.
This is used in some examples to serialize the language to outputs."""
the_dict = {"expressions": [str(expr) for expr in self.expressions]}
the_dict.update(kwargs)
return the_dict
Expand Down
6 changes: 0 additions & 6 deletions src/ultk/language/semantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ def __setattr__(self, __name: str, __value: Any) -> None:
else:
object.__setattr__(self, __name, __value)

def to_dict(self) -> dict:
return self.__dict__

def __str__(self) -> str:
return str(self.__dict__)

Expand Down Expand Up @@ -181,9 +178,6 @@ def dist(self) -> tuple:
for idx in range(len(self.universe.referents))
)

def to_dict(self) -> dict:
return {"referents": [referent.to_dict() for referent in self.referents]}

def __bool__(self):
return bool(self.referents) and bool(self.universe)

Expand Down

0 comments on commit ea3d45a

Please sign in to comment.