def add_direction(cls, key, unit, language, country=None): add_direction_probability = address_config.get_property( '{}.add_direction_probability'.format(key), language, country=country, default=0.0) if not random.random() < add_direction_probability: return unit add_direction_numeric = address_config.get_property( '{}.add_direction_numeric'.format(key), language, country=country) try: unit = int(unit) integer_unit = True except (ValueError, TypeError): integer_unit = False if add_direction_numeric and integer_unit: return RelativeDirection.phrase(unit, language, country=country) elif not integer_unit: add_direction_standalone = address_config.get_property( '{}.add_direction_standalone'.format(key), language, country=country) if add_direction_standalone: return RelativeDirection.phrase(None, language, country=country)
def random(cls, language, country=None): num_type, num_type_props = cls.choose_alphanumeric_type('blocks.alphanumeric', language, country=country) if num_type is None: return None if num_type == cls.NUMERIC: number = weighted_choice(cls.block_range, cls.block_range_cdf) return safe_decode(number) else: alphabet = address_config.get_property('alphabet', language, country=country, default=latin_alphabet) alphabet_probability = address_config.get_property('alphabet_probability', language, country=country, default=None) if alphabet_probability is not None and random.random() >= alphabet_probability: alphabet = latin_alphabet letter = sample_alphabet(alphabet, 2.0) if num_type == cls.ALPHA: return safe_decode(letter) else: number = weighted_choice(cls.block_range, cls.block_range_cdf) whitespace_probability = float(num_type_props.get('whitespace_probability', 0.0)) whitespace_phrase = six.u(' ') if whitespace_probability and random.random() < whitespace_probability else six.u('') if num_type == cls.ALPHA_PLUS_NUMERIC: return six.u('{}{}{}').format(letter, whitespace_phrase, number) elif num_type == cls.NUMERIC_PLUS_ALPHA: return six.u('{}{}{}').format(number, whitespace_phrase, letter)
def phrase(cls, unit, language, country=None, zone=None): if unit is not None: key = 'units.alphanumeric' if zone is None else 'units.zones.{}'.format(zone) if not address_config.get_property(key, language, country=country): return None 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: direction_unit = cls.add_direction(key, unit, language, country=country) 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, is_alpha=is_alpha) else: key = 'units.standalone' values, probs = address_config.alternative_probabilities(key, language, dictionaries=['unit_types_standalone'], country=country) if values is None: return None phrase, phrase_props = weighted_choice(values, probs) return phrase.title()
def random(cls, language, country=None): num_type, num_type_props = cls.choose_alphanumeric_type( 'staircases.alphanumeric', language, country=country) if num_type is None: return None if num_type == cls.NUMERIC: number = weighted_choice(cls.staircase_range, cls.staircase_range_cdf) return safe_decode(number) elif num_type == cls.HYPHENATED_NUMBER: number = weighted_choice(cls.staircase_range, cls.staircase_range_cdf) number2 = number + weighted_choice(cls.staircase_range, cls.staircase_range_cdf) return u'{}-{}'.format(number, number2) else: alphabet = address_config.get_property('alphabet', language, country=country, default=latin_alphabet) alphabet_probability = address_config.get_property( 'alphabet_probability', language, country=country, default=None) if alphabet_probability is not None and random.random( ) >= alphabet_probability: alphabet = latin_alphabet letter = sample_alphabet(alphabet, 2.0) if num_type == cls.ALPHA: return safe_decode(letter) else: number = weighted_choice(cls.staircase_range, cls.staircase_range_cdf) whitespace_probability = float( num_type_props.get('whitespace_probability', 0.0)) hyphen_probability = float( num_type_props.get('hyphen_probability', 0.0)) whitespace_phrase = u'' r = random.random() if r < whitespace_probability: whitespace_phrase = u' ' elif r < (whitespace_probability + hyphen_probability): whitespace_phrase = u'-' if num_type == cls.ALPHA_PLUS_NUMERIC: return six.u('{}{}{}').format(letter, whitespace_phrase, number) elif num_type == cls.NUMERIC_PLUS_ALPHA: return six.u('{}{}{}').format(number, whitespace_phrase, letter)
def add_quadrant(cls, key, unit, language, country=None): add_quadrant_probability = address_config.get_property( '{}.add_quadrant_probability'.format(key), language, country=country, default=0.0) if not random.random() < add_quadrant_probability: return unit add_quadrant_numeric = address_config.get_property( '{}.add_quadrant_numeric'.format(key), language, country=country) try: unit = int(unit) integer_unit = True except (ValueError, TypeError): integer_unit = False first_direction = address_config.get_property( '{}.add_quadrant_first_direction'.format(key), language, country=country) if first_direction == 'lateral': ordering = (LateralDirection, AnteroposteriorDirection) elif first_direction == 'anteroposterior': ordering = (AnteroposteriorDirection, LateralDirection) else: return unit if not integer_unit: add_quadrant_standalone = address_config.get_property( '{}.add_quadrant_standalone'.format(key), language, country=country) if add_quadrant_standalone: unit = None else: return None last_num_type = None for i, c in enumerate(ordering): num_type, phrase, props = c.pick_phrase_and_type(unit, language, country=country) whitespace_default = num_type == c.NUMERIC or last_num_type == c.NUMERIC unit = c.combine_with_number(unit, phrase, num_type, props, whitespace_default=whitespace_default) last_num_type = num_type return unit
def random_from_int(cls, number, language, country=None): num_type, num_type_props = cls.choose_alphanumeric_type( 'levels.alphanumeric', language, country=country) if num_type is None: return None numbering_starts_at = int( address_config.get_property('levels.numbering_starts_at', language, country=country, default=0)) if number >= 0: number += numbering_starts_at if num_type == cls.NUMERIC: return safe_decode(number) elif num_type == cls.ROMAN_NUMERAL: roman_numeral = numeric_expressions.roman_numeral(number) if roman_numeral is not None: return roman_numeral else: return safe_decode(number) elif num_type == cls.HYPHENATED_NUMBER: number2 = number + sample_floors_range(1, cls.max_floors) return u'{}-{}'.format(number, number2) else: alphabet = address_config.get_property('alphabet', language, country=country, default=latin_alphabet) alphabet_probability = address_config.get_property( 'alphabet_probability', language, country=country, default=None) if alphabet_probability is not None and random.random( ) >= alphabet_probability: alphabet = latin_alphabet letter = sample_alphabet(alphabet) if num_type == cls.ALPHA: return letter else: number = weighted_choice(cls.floors_letters, cls.floors_letters_cdf) if num_type == cls.ALPHA_PLUS_NUMERIC: return six.u('{}{}').format(letter, number) elif num_type == cls.NUMERIC_PLUS_ALPHA: return six.u('{}{}').format(number, letter) return None
def phrase(cls, unit, language, country=None, zone=None): if unit is not None: key = 'units.alphanumeric' if zone is None else 'units.zones.{}'.format( zone) if not address_config.get_property(key, language, country=country): return None 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: direction_unit = cls.add_direction(key, unit, language, country=country) 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, is_alpha=is_alpha) else: key = 'units.standalone' values, probs = address_config.alternative_probabilities( key, language, dictionaries=['unit_types_standalone'], country=country) if values is None: return None phrase, phrase_props = weighted_choice(values, probs) return phrase.title()
def choose_alphanumeric_type(cls, key, language, country=None): alphanumeric_props = address_config.get_property(key, language, country=country, default=None) if alphanumeric_props is None: return None, None values = [] probs = [] for num_type in (cls.NUMERIC, cls.ALPHA, cls.ALPHA_PLUS_NUMERIC, cls.NUMERIC_PLUS_ALPHA, cls.HYPHENATED_NUMBER, cls.ROMAN_NUMERAL): key = '{}_probability'.format(num_type) prob = alphanumeric_props.get(key) if prob is not None: values.append(num_type) probs.append(prob) if not values: return None, None probs = cdf(probs) num_type = weighted_choice(values, probs) num_type_props = alphanumeric_props.get(num_type, {}) return num_type, num_type_props
def phrase(cls, station, language, country=None): if station is None: return None phrase_prob = address_config.get_property('metro_stations.alphanumeric_phrase_probability', language, country=country, default=0.0) if random.random() < phrase_prob: return MetroStationPhrase.phrase(station, language, country=country) return None
def phrase(cls, block, language, country=None): if block is None: return None phrase_prob = address_config.get_property('blocks.alphanumeric_phrase_probability', language, country=country, default=0.0) if random.random() < phrase_prob: return cls.numeric_phrase('blocks.alphanumeric', block, language, dictionaries=['qualifiers'], country=country) else: return None
def add_direction(cls, key, unit, language, country=None): add_direction_probability = address_config.get_property('{}.add_direction_probability'.format(key), language, country=country, default=0.0) if not random.random() < add_direction_probability: return unit add_direction_numeric = address_config.get_property('{}.add_direction_numeric'.format(key), language, country=country) try: unit = int(unit) integer_unit = True except (ValueError, TypeError): integer_unit = False if add_direction_numeric and integer_unit: return RelativeDirection.phrase(unit, language, country=country) elif not integer_unit: add_direction_standalone = address_config.get_property('{}.add_direction_standalone'.format(key), language, country=country) if add_direction_standalone: return RelativeDirection.phrase(None, language, country=country)
def random(cls, language, country=None): num_type, num_type_props = cls.choose_alphanumeric_type( 'blocks.alphanumeric', language, country=country) if num_type is None: return None if num_type == cls.NUMERIC: number = weighted_choice(cls.block_range, cls.block_range_cdf) return safe_decode(number) else: alphabet = address_config.get_property('alphabet', language, country=country, default=latin_alphabet) alphabet_probability = address_config.get_property( 'alphabet_probability', language, country=country, default=None) if alphabet_probability is not None and random.random( ) >= alphabet_probability: alphabet = latin_alphabet letter = sample_alphabet(alphabet, 2.0) if num_type == cls.ALPHA: return safe_decode(letter) else: number = weighted_choice(cls.block_range, cls.block_range_cdf) whitespace_probability = float( num_type_props.get('whitespace_probability', 0.0)) whitespace_phrase = six.u( ' ') if whitespace_probability and random.random( ) < whitespace_probability else six.u('') if num_type == cls.ALPHA_PLUS_NUMERIC: return six.u('{}{}{}').format(letter, whitespace_phrase, number) elif num_type == cls.NUMERIC_PLUS_ALPHA: return six.u('{}{}{}').format(number, whitespace_phrase, letter)
def add_quadrant(cls, key, unit, language, country=None): add_quadrant_probability = address_config.get_property('{}.add_quadrant_probability'.format(key), language, country=country, default=0.0) if not random.random() < add_quadrant_probability: return unit add_quadrant_numeric = address_config.get_property('{}.add_quadrant_numeric'.format(key), language, country=country) try: unit = int(unit) integer_unit = True except (ValueError, TypeError): integer_unit = False first_direction = address_config.get_property('{}.add_quadrant_first_direction'.format(key), language, country=country) if first_direction == 'lateral': ordering = (LateralDirection, AnteroposteriorDirection) elif first_direction == 'anteroposterior': ordering = (AnteroposteriorDirection, LateralDirection) else: return unit if not integer_unit: add_quadrant_standalone = address_config.get_property('{}.add_quadrant_standalone'.format(key), language, country=country) if add_quadrant_standalone: unit = None else: return None last_num_type = None for i, c in enumerate(ordering): num_type, phrase, props = c.pick_phrase_and_type(unit, language, country=country) whitespace_default = num_type == c.NUMERIC or last_num_type == c.NUMERIC unit = c.combine_with_number(unit, phrase, num_type, props, whitespace_default=whitespace_default) last_num_type = num_type return unit
def random_from_int(cls, number, language, country=None): num_type, num_type_props = cls.choose_alphanumeric_type('levels.alphanumeric', language, country=country) if num_type is None: return None numbering_starts_at = int(address_config.get_property('levels.numbering_starts_at', language, country=country, default=0)) if number >= 0: number += numbering_starts_at if num_type == cls.NUMERIC: return safe_decode(number) elif num_type == cls.ROMAN_NUMERAL: roman_numeral = numeric_expressions.roman_numeral(number) if roman_numeral is not None: return roman_numeral else: return safe_decode(number) elif num_type == cls.HYPHENATED_NUMBER: number2 = number + sample_floors_range(1, cls.max_floors) return u'{}-{}'.format(number, number2) else: alphabet = address_config.get_property('alphabet', language, country=country, default=latin_alphabet) alphabet_probability = address_config.get_property('alphabet_probability', language, country=country, default=None) if alphabet_probability is not None and random.random() >= alphabet_probability: alphabet = latin_alphabet letter = sample_alphabet(alphabet) if num_type == cls.ALPHA: return letter else: number = weighted_choice(cls.floors_letters, cls.floors_letters_cdf) if num_type == cls.ALPHA_PLUS_NUMERIC: return six.u('{}{}').format(letter, number) elif num_type == cls.NUMERIC_PLUS_ALPHA: return six.u('{}{}').format(number, letter) return None
def phrase(cls, station, language, country=None): if station is None: return None phrase_prob = address_config.get_property( 'metro_stations.alphanumeric_phrase_probability', language, country=country, default=0.0) if random.random() < phrase_prob: return MetroStationPhrase.phrase(station, language, country=country) return None
def random(cls, language, country=None): num_type, num_type_props = cls.choose_alphanumeric_type('staircases.alphanumeric', language, country=country) if num_type is None: return None if num_type == cls.NUMERIC: number = weighted_choice(cls.staircase_range, cls.staircase_range_cdf) return safe_decode(number) elif num_type == cls.HYPHENATED_NUMBER: number = weighted_choice(cls.staircase_range, cls.staircase_range_cdf) number2 = number + weighted_choice(cls.staircase_range, cls.staircase_range_cdf) return u'{}-{}'.format(number, number2) else: alphabet = address_config.get_property('alphabet', language, country=country, default=latin_alphabet) alphabet_probability = address_config.get_property('alphabet_probability', language, country=country, default=None) if alphabet_probability is not None and random.random() >= alphabet_probability: alphabet = latin_alphabet letter = sample_alphabet(alphabet, 2.0) if num_type == cls.ALPHA: return safe_decode(letter) else: number = weighted_choice(cls.staircase_range, cls.staircase_range_cdf) whitespace_probability = float(num_type_props.get('whitespace_probability', 0.0)) hyphen_probability = float(num_type_props.get('hyphen_probability', 0.0)) whitespace_phrase = u'' r = random.random() if r < whitespace_probability: whitespace_phrase = u' ' elif r < (whitespace_probability + hyphen_probability): whitespace_phrase = u'-' if num_type == cls.ALPHA_PLUS_NUMERIC: return six.u('{}{}{}').format(letter, whitespace_phrase, number) elif num_type == cls.NUMERIC_PLUS_ALPHA: return six.u('{}{}{}').format(number, whitespace_phrase, letter)
def phrase(cls, number, language, country=None): if number is not None: prob_key = 'house_numbers.alphanumeric_phrase_probability' key = 'house_numbers.alphanumeric' dictionaries = ['house_numbers', 'number'] default = safe_decode(number) else: prob_key = 'house_numbers.no_number_probability' key = 'house_numbers.no_number' dictionaries = ['no_number'] default = None phrase_prob = address_config.get_property(prob_key, language, country=country, default=0.0) if random.random() < phrase_prob: return cls.numeric_phrase(key, safe_decode(number), language, dictionaries=dictionaries, country=country) return default
def phrase(cls, block, language, country=None): if block is None: return None phrase_prob = address_config.get_property( 'blocks.alphanumeric_phrase_probability', language, country=country, default=0.0) if random.random() < phrase_prob: return cls.numeric_phrase('blocks.alphanumeric', block, language, dictionaries=['qualifiers'], country=country) else: return None
def random(cls, language, country=None): category_props = address_config.get_property('categories', language, country=country) if category_props is None: return None values = [] probs = [] for prep_phrase_type in (cls.NEAR, cls.NEARBY, cls.NEAR_ME, cls.IN, cls.NULL): k = '{}_probability'.format(prep_phrase_type) prob = category_props.get(k, None) if prob is not None: values.append(prep_phrase_type) probs.append(prob) probs = cdf(probs) return weighted_choice(values, probs)
def random(cls, language, country=None): num_type, num_type_props = cls.choose_alphanumeric_type( 'po_boxes.alphanumeric', language, country=country) if num_type is None: return None if num_type != cls.ALPHA: digit_config = address_config.get_property('po_boxes.digits', language, country=country, default=[]) values = [] probs = [] for val in digit_config: values.append(val['length']) probs.append(val['probability']) probs = cdf(probs) num_digits = weighted_choice(values, probs) digits = cls.random_digits(num_digits) number = Digits.rewrite(digits, language, num_type_props) if num_type == cls.NUMERIC: return safe_decode(number) else: letter = cls.random_letter(language, country=country) whitespace_probability = float( num_type_props.get('whitespace_probability', 0.0)) whitespace_phrase = six.u( ' ') if whitespace_probability and random.random( ) < whitespace_probability else six.u('') if num_type == cls.ALPHA_PLUS_NUMERIC: return six.u('{}{}{}').format(letter, whitespace_phrase, number) elif num_type == cls.NUMERIC_PLUS_ALPHA: return six.u('{}{}{}').format(number, whitespace_phrase, letter) else: return cls.random_letter(language, country=country)
def choose_alphanumeric_type(cls, key, language, country=None): alphanumeric_props = address_config.get_property(key, language, country=country, default=None) if alphanumeric_props is None: return None, None values = [] probs = [] for num_type in (cls.NUMERIC, cls.ALPHA, cls.ALPHA_PLUS_NUMERIC, cls.NUMERIC_PLUS_ALPHA, cls.HYPHENATED_NUMBER, cls.ROMAN_NUMERAL): key = '{}_probability'.format(num_type) prob = alphanumeric_props.get(key) if prob is not None: values.append(num_type) probs.append(prob) if not values: return None, None probs = cdf(probs) num_type = weighted_choice(values, probs) num_type_props = alphanumeric_props.get(num_type, {}) return num_type, num_type_props
def random(cls, language, country=None): num_type, num_type_props = cls.choose_alphanumeric_type('po_boxes.alphanumeric', language, country=country) if num_type is None: return None if num_type != cls.ALPHA: digit_config = address_config.get_property('po_boxes.digits', language, country=country, default=[]) values = [] probs = [] for val in digit_config: values.append(val['length']) probs.append(val['probability']) probs = cdf(probs) num_digits = weighted_choice(values, probs) digits = cls.random_digits(num_digits) number = Digits.rewrite(digits, language, num_type_props) if num_type == cls.NUMERIC: return safe_decode(number) else: letter = cls.random_letter(language, country=country) whitespace_probability = float(num_type_props.get('whitespace_probability', 0.0)) whitespace_phrase = six.u(' ') if whitespace_probability and random.random() < whitespace_probability else six.u('') if num_type == cls.ALPHA_PLUS_NUMERIC: return six.u('{}{}{}').format(letter, whitespace_phrase, number) elif num_type == cls.NUMERIC_PLUS_ALPHA: return six.u('{}{}{}').format(number, whitespace_phrase, letter) else: return cls.random_letter(language, country=country)
def random(cls, language, country=None, num_floors=None, num_basements=None, floor=None): num_type, num_type_props = cls.choose_alphanumeric_type( 'units.alphanumeric', language, country=country) if num_type is None: return None use_floor_prob = address_config.get_property( 'units.alphanumeric.use_floor_probability', language, country=country, default=0.0) use_positive_numbers_prob = address_config.get_property( 'units.alphanumeric.use_positive_numbers_probability', language, country=country, default=0.0) if (num_floors is None and floor is None) or random.random() >= use_floor_prob: if random.random() >= use_positive_numbers_prob: number = weighted_choice(cls.numbered_units, cls.unit_probs_cdf) else: number = weighted_choice(cls.positive_units, cls.positive_units_cdf) else: if floor is None or not floor.isdigit(): floor = Floor.random_int(language, country=country, num_floors=num_floors, num_basements=num_basements) floor_numbering_starts_at = address_config.get_property( 'levels.numbering_starts_at', language, country=country, default=0) ground_floor_starts_at = address_config.get_property( 'units.alphanumeric.use_floor_ground_starts_at', language, country=country, default=None) if ground_floor_starts_at is not None: try: floor = int(floor) if floor >= floor_numbering_starts_at: floor -= floor_numbering_starts_at floor += ground_floor_starts_at floor = safe_decode(floor) except (TypeError, ValueError): pass use_floor_affix_prob = address_config.get_property( 'units.alphanumeric.use_floor_numeric_affix_probability', language, country=country, default=0.0) if use_floor_affix_prob and random.random() < use_floor_affix_prob: floor_phrase = Floor.phrase(floor, language, country=country) # Only works if the floor phrase is strictly numeric e.g. "1" or "H1" if is_numeric_strict(floor_phrase): unit = weighted_choice(cls.positive_units, cls.positive_units_cdf) unit_num_digits = address_config.get_property( 'units.alphanumeric.use_floor_unit_num_digits', language, country=country, default=None) if unit_num_digits is not None: unit = safe_decode(unit).zfill(unit_num_digits) return six.u('{}{}').format(floor_phrase, unit) floor_num_digits = address_config.get_property( 'units.alphanumeric.use_floor_floor_num_digits', language, country=country, default=None) if floor_num_digits is not None and floor.isdigit(): floor = floor.zfill(floor_num_digits) number = cls.for_floor(floor) if num_type == cls.NUMERIC: return safe_decode(number) elif num_type == cls.HYPHENATED_NUMBER: number2 = weighted_choice(cls.positive_units, cls.positive_units_cdf) range_prob = float( address_config.get_property( 'units.alphanumeric.hyphenated_number.range_probability', language, country=country, default=0.5)) direction = address_config.get_property( 'units.alphanumeric.hyphenated_number.direction', language, country=country, default='right') direction_prob = float( address_config.get_property( 'units.alphanumeric.hyphenated_number.direction_probability', language, country=country, default=0.0)) if random.random() < direction_prob: direction = 'left' if direction == 'right' else 'right' direction_right = direction == 'right' if random.random() < range_prob: if direction_right: number2 += number else: number2 = max(0, number - number2) if direction == 'right': return u'{}-{}'.format(number, number2) else: return u'{}-{}'.format(number2, number) else: alphabet = address_config.get_property('alphabet', language, country=country, default=latin_alphabet) alphabet_probability = address_config.get_property( 'alphabet_probability', language, country=country, default=None) if alphabet_probability is not None and random.random( ) >= alphabet_probability: alphabet = latin_alphabet letter = sample_alphabet(alphabet) if num_type == cls.ALPHA: return safe_decode(letter) else: if num_floors is None: number = weighted_choice(cls.positive_units_letters, cls.positive_units_letters_cdf) whitespace_probability = float( num_type_props.get('whitespace_probability', 0.0)) hyphen_probability = float( num_type_props.get('hyphen_probability', 0.0)) whitespace_phrase = u'' r = random.random() if r < whitespace_probability: whitespace_phrase = u' ' elif r < (whitespace_probability + hyphen_probability): whitespace_phrase = u'-' if num_type == cls.ALPHA_PLUS_NUMERIC: return six.u('{}{}{}').format(letter, whitespace_phrase, number) elif num_type == cls.NUMERIC_PLUS_ALPHA: return six.u('{}{}{}').format(number, whitespace_phrase, letter)
def random_letter(cls, language, country=None): alphabet = address_config.get_property('alphabet', language, country=country, default=latin_alphabet) return sample_alphabet(alphabet)
def random_letter(cls, language, country=None): alphabet = address_config.get_property('alphabet', language, country=country, default=latin_alphabet) return sample_alphabet(alphabet)
def random(cls, language, country=None, num_floors=None, num_basements=None, floor=None): num_type, num_type_props = cls.choose_alphanumeric_type('units.alphanumeric', language, country=country) if num_type is None: return None use_floor_prob = address_config.get_property('units.alphanumeric.use_floor_probability', language, country=country, default=0.0) use_positive_numbers_prob = address_config.get_property('units.alphanumeric.use_positive_numbers_probability', language, country=country, default=0.0) if (num_floors is None and floor is None) or random.random() >= use_floor_prob: if random.random() >= use_positive_numbers_prob: number = weighted_choice(cls.numbered_units, cls.unit_probs_cdf) else: number = weighted_choice(cls.positive_units, cls.positive_units_cdf) else: if floor is None or not floor.isdigit(): floor = Floor.random_int(language, country=country, num_floors=num_floors, num_basements=num_basements) floor_numbering_starts_at = address_config.get_property('levels.numbering_starts_at', language, country=country, default=0) ground_floor_starts_at = address_config.get_property('units.alphanumeric.use_floor_ground_starts_at', language, country=country, default=None) if ground_floor_starts_at is not None: try: floor = int(floor) if floor >= floor_numbering_starts_at: floor -= floor_numbering_starts_at floor += ground_floor_starts_at floor = safe_decode(floor) except (TypeError, ValueError): pass use_floor_affix_prob = address_config.get_property('units.alphanumeric.use_floor_numeric_affix_probability', language, country=country, default=0.0) if use_floor_affix_prob and random.random() < use_floor_affix_prob: floor_phrase = Floor.phrase(floor, language, country=country) # Only works if the floor phrase is strictly numeric e.g. "1" or "H1" if is_numeric_strict(floor_phrase): unit = weighted_choice(cls.positive_units, cls.positive_units_cdf) unit_num_digits = address_config.get_property('units.alphanumeric.use_floor_unit_num_digits', language, country=country, default=None) if unit_num_digits is not None: unit = safe_decode(unit).zfill(unit_num_digits) return six.u('{}{}').format(floor_phrase, unit) floor_num_digits = address_config.get_property('units.alphanumeric.use_floor_floor_num_digits', language, country=country, default=None) if floor_num_digits is not None and floor.isdigit(): floor = floor.zfill(floor_num_digits) number = cls.for_floor(floor) if num_type == cls.NUMERIC: return safe_decode(number) elif num_type == cls.HYPHENATED_NUMBER: number2 = weighted_choice(cls.positive_units, cls.positive_units_cdf) range_prob = float(address_config.get_property('units.alphanumeric.hyphenated_number.range_probability', language, country=country, default=0.5)) direction = address_config.get_property('units.alphanumeric.hyphenated_number.direction', language, country=country, default='right') direction_prob = float(address_config.get_property('units.alphanumeric.hyphenated_number.direction_probability', language, country=country, default=0.0)) if random.random() < direction_prob: direction = 'left' if direction == 'right' else 'right' direction_right = direction == 'right' if random.random() < range_prob: if direction_right: number2 += number else: number2 = max(0, number - number2) if direction == 'right': return u'{}-{}'.format(number, number2) else: return u'{}-{}'.format(number2, number) else: alphabet = address_config.get_property('alphabet', language, country=country, default=latin_alphabet) alphabet_probability = address_config.get_property('alphabet_probability', language, country=country, default=None) if alphabet_probability is not None and random.random() >= alphabet_probability: alphabet = latin_alphabet letter = sample_alphabet(alphabet) if num_type == cls.ALPHA: return safe_decode(letter) else: if num_floors is None: number = weighted_choice(cls.positive_units_letters, cls.positive_units_letters_cdf) whitespace_probability = float(num_type_props.get('whitespace_probability', 0.0)) hyphen_probability = float(num_type_props.get('hyphen_probability', 0.0)) whitespace_phrase = u'' r = random.random() if r < whitespace_probability: whitespace_phrase = u' ' elif r < (whitespace_probability + hyphen_probability): whitespace_phrase = u'-' if num_type == cls.ALPHA_PLUS_NUMERIC: return six.u('{}{}{}').format(letter, whitespace_phrase, number) elif num_type == cls.NUMERIC_PLUS_ALPHA: return six.u('{}{}{}').format(number, whitespace_phrase, letter)
def phrase(cls, floor, language, country=None, num_floors=None): if floor is None: return None integer_floor = False floor = safe_decode(floor) try: floor = int(floor) integer_floor = True except (ValueError, TypeError): try: floor = float(floor) integer_floor = int(floor) == floor except (ValueError, TypeError): return cls.numeric_phrase('levels.alphanumeric', floor, language, dictionaries=['level_types_numbered'], country=country) numbering_starts_at = int(address_config.get_property('levels.numbering_starts_at', language, country=country, default=0)) try: num_floors = int(num_floors) top_floor = num_floors if numbering_starts_at == 1 else num_floors - 1 is_top = num_floors and floor == top_floor except (ValueError, TypeError): is_top = False alias_prefix = 'levels.aliases' aliases = address_config.get_property(alias_prefix, language, country=country) if aliases: alias = None if not integer_floor and floor >= 0 and 'half_floors' in aliases: floor = int(floor) alias = 'half_floors' elif not integer_floor and floor < 0 and 'half_floors_negative' in aliases: floor = int(floor) alias = 'half_floors_negative' elif floor < -1 and '<-1' in aliases: alias = '<-1' elif is_top and 'top' in aliases: alias = 'top' elif safe_decode(floor) in aliases: alias = safe_decode(floor) floor = safe_decode(floor) if alias: alias_props = aliases.get(alias) # Aliases upon aliases, e.g. for something like "Upper Mezzanine" # where it's an alias for "1" under the half_floors key if safe_decode(floor) in alias_props.get('aliases', {}): alias_prefix = '{}.{}.aliases'.format(alias_prefix, alias) alias = safe_decode(floor) if alias: return cls.numeric_phrase('{}.{}'.format(alias_prefix, alias), floor, language, dictionaries=['level_types_basement', 'level_types_mezzanine', 'level_types_numbered', 'level_types_standalone', 'level_types_sub_basement'], country=country) return cls.numeric_phrase('levels.alphanumeric', floor, language, dictionaries=['level_types_numbered'], country=country)
def phrase(cls, floor, language, country=None, num_floors=None): if floor is None: return None integer_floor = False floor = safe_decode(floor) try: floor = int(floor) integer_floor = True except (ValueError, TypeError): try: floor = float(floor) integer_floor = int(floor) == floor except (ValueError, TypeError): return cls.numeric_phrase( 'levels.alphanumeric', floor, language, dictionaries=['level_types_numbered'], country=country) numbering_starts_at = int( address_config.get_property('levels.numbering_starts_at', language, country=country, default=0)) try: num_floors = int(num_floors) top_floor = num_floors if numbering_starts_at == 1 else num_floors - 1 is_top = num_floors and floor == top_floor except (ValueError, TypeError): is_top = False alias_prefix = 'levels.aliases' aliases = address_config.get_property(alias_prefix, language, country=country) if aliases: alias = None if not integer_floor and floor >= 0 and 'half_floors' in aliases: floor = int(floor) alias = 'half_floors' elif not integer_floor and floor < 0 and 'half_floors_negative' in aliases: floor = int(floor) alias = 'half_floors_negative' elif floor < -1 and '<-1' in aliases: alias = '<-1' elif is_top and 'top' in aliases: alias = 'top' elif safe_decode(floor) in aliases: alias = safe_decode(floor) floor = safe_decode(floor) if alias: alias_props = aliases.get(alias) # Aliases upon aliases, e.g. for something like "Upper Mezzanine" # where it's an alias for "1" under the half_floors key if safe_decode(floor) in alias_props.get('aliases', {}): alias_prefix = '{}.{}.aliases'.format(alias_prefix, alias) alias = safe_decode(floor) if alias: return cls.numeric_phrase('{}.{}'.format(alias_prefix, alias), floor, language, dictionaries=[ 'level_types_basement', 'level_types_mezzanine', 'level_types_numbered', 'level_types_standalone', 'level_types_sub_basement' ], country=country) return cls.numeric_phrase('levels.alphanumeric', floor, language, dictionaries=['level_types_numbered'], country=country)