diff --git a/scripts/geodata/addresses/numbering.py b/scripts/geodata/addresses/numbering.py index 30a6307d..45793cfd 100644 --- a/scripts/geodata/addresses/numbering.py +++ b/scripts/geodata/addresses/numbering.py @@ -137,8 +137,8 @@ class NumberedComponent(object): return num_type, num_type_props @classmethod - def numeric_phrase(cls, key, num, language, country=None, dictionaries=(), strict_numeric=False): - is_alpha = False + def numeric_phrase(cls, key, num, language, country=None, dictionaries=(), strict_numeric=False, is_alpha=False): + has_alpha = False is_none = False if num is not None: try: @@ -150,13 +150,20 @@ class NumberedComponent(object): if not all((c == token_types.NUMERIC) for t, c in tokenize(safe_decode(num))): if strict_numeric: return safe_decode(num) - is_alpha = True + has_alpha = True else: 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 - 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: return safe_decode(num) if not is_none else None @@ -198,7 +205,7 @@ class NumberedComponent(object): return phrase # 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')]) total = float(sum(probs)) probs = [p / total for p in probs] diff --git a/scripts/geodata/addresses/units.py b/scripts/geodata/addresses/units.py index e9b3c1db..bb5a9340 100644 --- a/scripts/geodata/addresses/units.py +++ b/scripts/geodata/addresses/units.py @@ -149,6 +149,8 @@ class Unit(NumberedComponent): if unit is not None: key = 'units.alphanumeric' if zone is None else 'units.zones.{}'.format(zone) + is_alpha = safe_decode(unit).isalpha() + direction_unit = None add_direction = address_config.get_property('{}.add_direction'.format(key), language, country=country) if add_direction: @@ -156,13 +158,15 @@ class Unit(NumberedComponent): if direction_unit and direction_unit != unit: unit = direction_unit + is_alpha = False else: add_quadrant = address_config.get_property('{}.add_quadrant'.format(key), language, country=country) if add_quadrant: unit = cls.add_quadrant(key, unit, language, country=country) + is_alpha = False 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: key = 'units.standalone' add_direction = address_config.get_property('{}.add_direction'.format(key), language, country=country)