[numbering] Adding ability to have alpha-specific config (phrases that only apply to lettered apartments, etc.)

This commit is contained in:
Al
2016-05-20 14:04:52 -04:00
parent 319e4649cf
commit 24bd95a2fa
2 changed files with 17 additions and 6 deletions

View File

@@ -137,8 +137,8 @@ class NumberedComponent(object):
return num_type, num_type_props return num_type, num_type_props
@classmethod @classmethod
def numeric_phrase(cls, key, num, language, country=None, dictionaries=(), strict_numeric=False): def numeric_phrase(cls, key, num, language, country=None, dictionaries=(), strict_numeric=False, is_alpha=False):
is_alpha = False has_alpha = False
is_none = False is_none = False
if num is not None: if num is not None:
try: try:
@@ -150,13 +150,20 @@ class NumberedComponent(object):
if not all((c == token_types.NUMERIC) for t, c in tokenize(safe_decode(num))): if not all((c == token_types.NUMERIC) for t, c in tokenize(safe_decode(num))):
if strict_numeric: if strict_numeric:
return safe_decode(num) return safe_decode(num)
is_alpha = True has_alpha = True
else: else:
is_none = True is_none = True
values, probs = None, None
if is_alpha:
values, probs = address_config.alternative_probabilities('{}.alpha'.format(key), language, dictionaries=dictionaries, country=country)
# Pick a phrase given the probability distribution from the config # Pick a phrase given the probability distribution from the config
values, probs = address_config.alternative_probabilities(key, language, dictionaries=dictionaries, country=country) if values is None:
values, probs = address_config.alternative_probabilities(key, language, dictionaries=dictionaries, country=country)
if not values: if not values:
return safe_decode(num) if not is_none else None return safe_decode(num) if not is_none else None
@@ -198,7 +205,7 @@ class NumberedComponent(object):
return phrase return phrase
# If we're using something like "Floor A" or "Unit 2L", remove ordinal/affix items # If we're using something like "Floor A" or "Unit 2L", remove ordinal/affix items
if is_alpha: if has_alpha:
values, probs = zip(*[(v, p) for v, p in zip(values, probs) if v in ('numeric', 'null', 'standalone')]) values, probs = zip(*[(v, p) for v, p in zip(values, probs) if v in ('numeric', 'null', 'standalone')])
total = float(sum(probs)) total = float(sum(probs))
probs = [p / total for p in probs] probs = [p / total for p in probs]

View File

@@ -149,6 +149,8 @@ class Unit(NumberedComponent):
if unit is not None: if unit is not None:
key = 'units.alphanumeric' if zone is None else 'units.zones.{}'.format(zone) key = 'units.alphanumeric' if zone is None else 'units.zones.{}'.format(zone)
is_alpha = safe_decode(unit).isalpha()
direction_unit = None direction_unit = None
add_direction = address_config.get_property('{}.add_direction'.format(key), language, country=country) add_direction = address_config.get_property('{}.add_direction'.format(key), language, country=country)
if add_direction: if add_direction:
@@ -156,13 +158,15 @@ class Unit(NumberedComponent):
if direction_unit and direction_unit != unit: if direction_unit and direction_unit != unit:
unit = direction_unit unit = direction_unit
is_alpha = False
else: else:
add_quadrant = address_config.get_property('{}.add_quadrant'.format(key), language, country=country) add_quadrant = address_config.get_property('{}.add_quadrant'.format(key), language, country=country)
if add_quadrant: if add_quadrant:
unit = cls.add_quadrant(key, unit, language, country=country) unit = cls.add_quadrant(key, unit, language, country=country)
is_alpha = False
return cls.numeric_phrase(key, safe_decode(unit), language, return cls.numeric_phrase(key, safe_decode(unit), language,
dictionaries=['unit_types_numbered'], country=country) dictionaries=['unit_types_numbered'], country=country, is_alpha=is_alpha)
else: else:
key = 'units.standalone' key = 'units.standalone'
add_direction = address_config.get_property('{}.add_direction'.format(key), language, country=country) add_direction = address_config.get_property('{}.add_direction'.format(key), language, country=country)