[categories] Updated category query to have add_address variable (e.g. for 'near 123 Main Street' vs. 'near Brooklyn, NY'), reuses category preposition

This commit is contained in:
Al
2016-05-21 02:19:21 -04:00
parent 26fe87ff4a
commit 0b67fe70b9

View File

@@ -1,25 +1,17 @@
import random
import six
from collections import namedtuple
from geodata.addresses.config import address_config
from geodata.categories.config import category_config
from geodata.categories.preposition import CategoryPreposition
from geodata.encoding import safe_decode
from geodata.math.sampling import weighted_choice, cdf
from geodata.math.sampling import weighted_choice
CategoryQuery = namedtuple('CategoryQuery', 'category, prep, add_place_name')
CategoryQuery = namedtuple('CategoryQuery', 'category, prep, add_place_name, add_address')
NULL_CATEGORY_QUERY = CategoryQuery(None, None, False)
NULL_CATEGORY_QUERY = CategoryQuery(None, None, False, False)
class Category(object):
NEAR = 'near'
NEARBY = 'nearby'
NEAR_ME = 'near_me'
IN = 'in'
NULL = 'null'
@classmethod
def phrase(cls, language, key, value, is_plural=False, country=None):
category_phrase = category_config.get_phrase(language, key, value, is_plural=is_plural)
@@ -28,25 +20,9 @@ class Category(object):
category_phrase = safe_decode(category_phrase)
category_props = address_config.get_property('categories', language, country=country)
if category_props is None:
return CategoryQuery(category_phrase, prep=None, add_place_name=True)
prep_phrase_type = CategoryPreposition.random(language, country=country)
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)
prep_phrase_type = weighted_choice(values, probs)
if prep_phrase_type == cls.NULL:
if prep_phrase_type in (None, CategoryPreposition.NULL):
return CategoryQuery(category_phrase, prep=None, add_place_name=True)
values, probs = address_config.alternative_probabilities('categories.{}'.format(prep_phrase_type), language, country=country)
@@ -56,6 +32,7 @@ class Category(object):
prep_phrase, prep_phrase_props = weighted_choice(values, probs)
prep_phrase = safe_decode(prep_phrase)
add_place_name = prep_phrase_type not in (cls.NEARBY, cls.NEAR_ME)
add_address = prep_phrase_type not in (CategoryPreposition.NEARBY, CategoryPreposition.NEAR_ME, CategoryPreposition.IN)
add_place_name = prep_phrase_type not in (CategoryPreposition.NEARBY, CategoryPreposition.NEAR_ME)
return CategoryQuery(category_phrase, prep=prep_phrase, add_place_name=add_place_name)
return CategoryQuery(category_phrase, prep=prep_phrase, add_place_name=add_place_name, add_address=add_address)