Skip to content

Commit

Permalink
[natural_translit] Refactored nodes_str() and states_str() of Lattice…
Browse files Browse the repository at this point in the history
…. Added test for Feature.__str__.

PiperOrigin-RevId: 723139492
  • Loading branch information
isingoo authored and copybara-github committed Feb 4, 2025
1 parent 7d43b76 commit 64eac03
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
15 changes: 5 additions & 10 deletions nisaba/scripts/natural_translit/utils/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,18 +756,13 @@ def table_contents(self) -> list[list[str]]:

def get(self, aspect: 'Feature.Aspect') -> 'Feature.Set':
"""Gets the value set for the given aspect."""
if aspect.inventory == self.inventory:
inventory_alias = 'missing'
else:
inventory_alias = aspect.inventory.alias
return super().get(
aspect.alias,
Feature.Set(
aspect.n_a,
alias='%s_%s'
% (
'missing'
if aspect.inventory == self.inventory
else aspect.inventory.alias,
aspect.alias,
),
),
Feature.Set(aspect.n_a, alias=f'{inventory_alias}_{aspect.alias}'),
)

def copy(self, alias: str) -> 'Feature.Profile':
Expand Down
13 changes: 13 additions & 0 deletions nisaba/scripts/natural_translit/utils/feature_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,19 @@ class FeatureTest(test_op.TestCase):
def test_feature_text(self):
self.assertEqual(_R.warmth.vcold.text, 'very_cold')

def test_feature_str(self):
self.AssertStrEqual(
_R.warmth.vcold,
' vcold (very_cold) = {\n'
' cold (cold): 0.50\n'
' chl (chilly): 1.00\n'
' tpd (tepid): 1.50\n'
' warm (warm): 2.00\n'
' hot (hot): 2.50\n'
' vhot (very_hot): 3.00\n'
' }\n'
)

def test_aspect_of_feature(self):
self.assertEqual(_R.warmth.warm.aspect, _R.warmth)

Expand Down
33 changes: 23 additions & 10 deletions nisaba/scripts/natural_translit/utils/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,33 @@ def __init__(
def __str__(self):
return self.text

def _str_nodes(self, state_str: bool) -> str:
"""Returns a string representation of the nodes or states in the lattice.
Args:
state_str: If True, nodes are represented as the int value in the FST they
belong to. If the node has no state, it's represented as ∅. If False,
nodes are represented as the alias of the lattice they belong to and their
coordinates on the lattice.
Returns:
A string representation of the nodes in the lattice.
"""
return '\n'.join([
'\t'.join(
[node.state_str() if state_str else node.text for node in row]
)
for row in self._nodes
])

def nodes_str(self) -> str:
return 'Lattice: %s\nNodes:\n%s\n' % (
self.alias,
'\n'.join(
['\t'.join([node.text for node in row]) for row in self._nodes]
),
return (
f'Lattice: {self.alias}\nNodes:\n{self._str_nodes(state_str=False)}\n'
)

def states_str(self) -> str:
return 'Lattice: %s\nStates:\n%s\n' % (
self.alias,
'\n'.join([
'\t'.join([node.state_str() for node in row]) for row in self._nodes
]),
return (
f'Lattice: {self.alias}\nStates:\n{self._str_nodes(state_str=True)}\n'
)

def _new_node(self, x: int, y: int, populate: bool = False) -> Node:
Expand Down

0 comments on commit 64eac03

Please sign in to comment.