Skip to content

Commit

Permalink
Split Feature.Profile copy and update methods in prep for adding rest…
Browse files Browse the repository at this point in the history
…rictions to modifying profiles.

PiperOrigin-RevId: 658490317
  • Loading branch information
isingoo authored and copybara-github committed Aug 1, 2024
1 parent 049c12e commit a0704f5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,24 @@
def _profiles() -> inventory.Inventory:
f = descriptive_features.FEATURES
profiles = inventory.Inventory.from_list([
f.consonant.copy_and_update(
'p',
f.consonant.copy('p').update(
f.manner.stop,
f.bilabial,
f.voicing.voiceless,
),
f.consonant.copy_and_update(
'f',
f.consonant.copy('f').update(
f.manner.non_sibilant,
f.labiodental,
f.voicing.voiceless,
),
f.consonant.copy_and_update(
's',
f.consonant.copy('s').update(
f.manner.sibilant,
f.para_alveolar,
f.voicing.voiceless,
),
])
profiles.add_item(
profiles.s.copy_and_update('s_low_amplitude', f.manner.non_sibilant)
profiles.s.copy('s_low_amplitude').update(f.manner.non_sibilant)
)
return profiles

Expand All @@ -54,7 +51,7 @@ class DescriptiveFeaturesTest(test_op.TestCase):
def test_profile_p(self):
self.AssertStrEqual(
_P.p,
'Profile: {\n'
'p profile: {\n'
' ph_class: {consonant}\n'
' airstream: {pulmonic}\n'
' manner: {stop}\n'
Expand Down
55 changes: 25 additions & 30 deletions nisaba/scripts/natural_translit/utils/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,16 +640,16 @@ def __str__(self):
)

def add_profile(
self, alias: str,
*params: 'Feature.ITERABLE',
) -> None:
self.add_suppl(Feature.Profile(self, alias, *params))
self, alias: str, *features: 'Feature.ITERABLE'
) -> 'Feature.Profile':
new = Feature.Profile(self, alias, *features)
self.add_suppl(new)
return new

def copy_and_update_profile(
self, old: 'Feature.Profile', alias: str,
*params: 'Feature.ITERABLE',
) -> None:
self.add_suppl(old.copy_and_update(alias, *params))
def copy_profile(
self, old: 'Feature.Profile', alias: str
) -> 'Feature.Profile':
return self.add_profile(alias, *old)

def make_sets(
self, *alias_features: tuple[str, 'Feature.ITERABLE']
Expand Down Expand Up @@ -716,7 +716,7 @@ def __init__(
self.max_dist += aspect.max_dist

def __str__(self):
text = 'Profile: {\n'
text = self.alias + ' profile: {\n'
for item in self:
text += ' ' + str(item) + '\n'
text += '}\n'
Expand All @@ -738,49 +738,44 @@ def get(self, aspect: 'Feature.Aspect') -> 'Feature.Set':
),
)

def copy_and_update(
self, alias: str,
*updates: 'Feature.ITERABLE'
) -> 'Feature.Profile':
"""Copies the whole profile and applies the updates if provided..
def copy(self, alias: str) -> 'Feature.Profile':
return Feature.Profile(self.inventory, alias, *self)

def update(self, *features: 'Feature.ITERABLE') -> 'Feature.Profile':
"""Updates the profile with the given features.
Args:
alias: alias of the new profile.
*updates: values to be replaced in the new profile.
*features: values to be replaced.
Returns:
Profile.
Example:
room_features.add_profile(
Feature.Profile(
room_features,
'white_office'
room_features.function.office,
room_features.color.white,
room_features.ligthing.any,
room_features.warmth.any,
room_features.door.any,
)
white_office.copy_and_update(
'wo_hot'
room_features.warmth.hot
)
white_office.update(room_features.warmth.hot)
yields
Profile(
'wo_hot'
Feature.Profile(
room_features,
'white_office'
room_features.function.office,
room_features.color.white,
room_features.ligthing.any,
room_features.warmth.hot,
room_features.door.any,
)
"""
new = Feature.Profile(self.inventory, alias, *self)
update_dict = Feature.Set.aspect_dict(*updates)
for aspect, value in update_dict.items():
new.get(aspect).update(value)
return new
for aspect, value in Feature.Set.aspect_dict(*features).items():
self.get(aspect).update(value)
return self

def compare(
self,
Expand Down
37 changes: 31 additions & 6 deletions nisaba/scripts/natural_translit/utils/feature_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ def _room_features() -> f.Inventory:
room_features.color.n_a,
room_features.door.shut,
)
room_features.copy_and_update_profile(
room_features.room2, 'room3',
room_features.copy_profile(room_features.room2, 'room3').update(
room_features.warmth.cold,
)
return room_features
Expand Down Expand Up @@ -344,7 +343,7 @@ def test_inventory_set(self):
def test_profile_room1(self):
self.AssertStrEqual(
_R.room1,
'Profile: {\n'
'room1 profile: {\n'
' warmth: {cold}\n'
' lighting: {any}\n'
' function: {bedroom}\n'
Expand All @@ -356,7 +355,7 @@ def test_profile_room1(self):
def test_profile_room2(self):
self.AssertStrEqual(
_R.room2,
'Profile: {\n'
'room2 profile: {\n'
' warmth: {chilly, hot, tepid, very_hot, warm}\n'
' lighting: {fluorescent}\n'
' function: {living_room}\n'
Expand All @@ -368,7 +367,7 @@ def test_profile_room2(self):
def test_profile_room3(self):
self.AssertStrEqual(
_R.room3,
'Profile: {\n'
'room3 profile: {\n'
' warmth: {cold}\n'
' lighting: {fluorescent}\n'
' function: {living_room}\n'
Expand All @@ -377,6 +376,32 @@ def test_profile_room3(self):
'}\n'
)

def test_profile_copy(self):
self.AssertStrEqual(
_A.cat.copy('copycat'),
'copycat profile: {\n'
' size: {small}\n'
' weight: {light}\n'
' speed: {fast}\n'
' life_span: {medium}\n'
' gestation: {medium}\n'
' litter_size: {medium}\n'
'}\n',
)

def test_profile_update(self):
self.AssertStrEqual(
_A.cat.copy('kitten').update(_A.size.tiny),
'kitten profile: {\n'
' size: {tiny}\n'
' weight: {light}\n'
' speed: {fast}\n'
' life_span: {medium}\n'
' gestation: {medium}\n'
' litter_size: {medium}\n'
'}\n',
)

def test_profile_default_value_n_a(self):
self.AssertStrEqual(
f.Profile(
Expand All @@ -386,7 +411,7 @@ def test_profile_default_value_n_a(self):
_A.weight.any,
unspecified_aspect_n_a=True,
),
'Profile: {\n'
'ornamental profile: {\n'
' size: {any}\n'
' weight: {any}\n'
' speed: {not_applicable}\n'
Expand Down

0 comments on commit a0704f5

Please sign in to comment.