[addresses] Adding ability to determine unit numbers using a known floor number
This commit is contained in:
@@ -811,18 +811,14 @@ class AddressComponents(object):
|
||||
def generate_sub_building_component(self, component, address_components, language, country=None, **kw):
|
||||
existing = address_components.get(component, None)
|
||||
|
||||
component_class = self.sub_building_component_class_map[component]
|
||||
|
||||
if existing is None:
|
||||
generated_type = self.generated_type(component, address_components, language, country=country)
|
||||
if generated_type == self.ALPHANUMERIC_PHRASE:
|
||||
num = component_class.random(language, country=country, **kw)
|
||||
address_components[component] = num
|
||||
return num
|
||||
return True
|
||||
elif generated_type == self.STANDALONE_PHRASE:
|
||||
return None
|
||||
return False
|
||||
|
||||
return None
|
||||
return False
|
||||
|
||||
def add_sub_building_phrase(self, component, address_components, language, country, generated_components=None, **kw):
|
||||
num = address_components.get(component)
|
||||
@@ -845,16 +841,23 @@ class AddressComponents(object):
|
||||
def add_sub_building_components(self, address_components, language, country=None, num_floors=None, num_basements=None, zone=None):
|
||||
generated_components = set()
|
||||
|
||||
floor = None
|
||||
|
||||
if self.generate_sub_building_component(AddressFormatter.ENTRANCE, address_components, language, country=country):
|
||||
address_components[AddressFormatter.ENTRANCE] = Entrance.random(language, country=country)
|
||||
generated_components.add(AddressFormatter.ENTRANCE)
|
||||
|
||||
if self.generate_sub_building_component(AddressFormatter.STAIRCASE, address_components, language, country=country):
|
||||
address_components[AddressFormatter.STAIRCASE] = Staircase.random(language, country=country)
|
||||
generated_components.add(AddressFormatter.STAIRCASE)
|
||||
|
||||
if self.generate_sub_building_component(AddressFormatter.LEVEL, address_components, language, country=country, num_floors=num_floors, num_basements=num_basements):
|
||||
floor = Floor.random_int(language, country=country, num_floors=num_floors, num_basements=num_basements)
|
||||
address_components[AddressFormatter.LEVEL] = Floor.random_from_int(floor, language, country=country)
|
||||
generated_components.add(AddressFormatter.LEVEL)
|
||||
|
||||
if self.generate_sub_building_component(AddressFormatter.UNIT, address_components, language, country=country, num_floors=num_floors, num_basements=num_basements):
|
||||
address_components[AddressFormatter.UNIT] = Unit.random(language, country=country, num_floors=num_floors, num_basements=num_basements, floor=floor)
|
||||
generated_components.add(AddressFormatter.UNIT)
|
||||
|
||||
# Combine fields like unit/house_number here
|
||||
|
||||
@@ -37,16 +37,20 @@ class Floor(NumberedComponent):
|
||||
return random.randint(1, (num_floors - 1) if num_floors > 1 else 1)
|
||||
|
||||
@classmethod
|
||||
def random(cls, language, country=None, num_floors=None, num_basements=None):
|
||||
num_type, num_type_props = cls.choose_alphanumeric_type('levels.alphanumeric', language, country=country)
|
||||
if num_type is None:
|
||||
return None
|
||||
|
||||
def random_int(cls, language, country=None, num_floors=None, num_basements=None):
|
||||
if num_floors is not None:
|
||||
number = cls.sample_floors(num_floors, num_basements or 0)
|
||||
else:
|
||||
number = weighted_choice(cls.numbered_floors, cls.floor_probs_cdf)
|
||||
|
||||
return number
|
||||
|
||||
@classmethod
|
||||
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:
|
||||
@@ -73,6 +77,13 @@ class Floor(NumberedComponent):
|
||||
elif num_type == cls.NUMERIC_PLUS_ALPHA:
|
||||
return six.u('{}{}').format(number, letter)
|
||||
|
||||
return None
|
||||
|
||||
@classmethod
|
||||
def random(cls, language, country=None, num_floors=None, num_basements=None):
|
||||
number = cls.random_int(language, country=country, num_floors=num_floors, num_basements=num_basements)
|
||||
return cls.random_from_int(number, language, country=country)
|
||||
|
||||
@classmethod
|
||||
def phrase(cls, floor, language, country=None, num_floors=None):
|
||||
if floor is None:
|
||||
|
||||
@@ -51,17 +51,18 @@ class Unit(NumberedComponent):
|
||||
return six.u('{}{}').format(floor_number, safe_decode(unit).zfill(num_digits))
|
||||
|
||||
@classmethod
|
||||
def random(cls, language, country=None, num_floors=None, num_basements=None):
|
||||
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)
|
||||
|
||||
if num_floors is None or random.random() >= use_floor_prob:
|
||||
if (num_floors is None and floor is None) or random.random() >= use_floor_prob:
|
||||
number = weighted_choice(cls.numbered_units, cls.unit_probs_cdf)
|
||||
else:
|
||||
floor = Floor.random(language, country=country, num_floors=num_floors, num_basements=num_basements)
|
||||
if floor is None:
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user