1from geodata.addresses.config import address_config 2from geodata.categories.config import category_config 3from geodata.math.sampling import weighted_choice, cdf 4 5 6class CategoryPreposition(object): 7 NEAR = 'near' 8 NEARBY = 'nearby' 9 NEAR_ME = 'near_me' 10 IN = 'in' 11 NULL = 'null' 12 13 @classmethod 14 def random(cls, language, country=None): 15 category_props = address_config.get_property('categories', language, country=country) 16 if category_props is None: 17 return None 18 19 values = [] 20 probs = [] 21 22 for prep_phrase_type in (cls.NEAR, cls.NEARBY, cls.NEAR_ME, cls.IN, cls.NULL): 23 k = '{}_probability'.format(prep_phrase_type) 24 prob = category_props.get(k, None) 25 if prob is not None: 26 values.append(prep_phrase_type) 27 probs.append(prob) 28 29 probs = cdf(probs) 30 31 return weighted_choice(values, probs) 32