[formatting] allowing a non-default option for components that can be inserted between road and house number

This commit is contained in:
Al
2016-09-09 01:38:39 -04:00
parent 4c6bcda3b2
commit 85ad3bf0f4
2 changed files with 30 additions and 26 deletions

View File

@@ -674,6 +674,7 @@ countries:
city_before_road: city_before_road:
before: road before: road
probability: 0.001 probability: 0.001
allow_between_house_number_and_road: true
# Andorra, uses same template as France # Andorra, uses same template as France
ad_ca: ad_ca:

View File

@@ -265,14 +265,16 @@ class AddressFormatter(object):
if k == 'conditional': if k == 'conditional':
continue continue
allow_between_house_number_and_road = v.get('allow_between_house_number_and_road', False)
if 'before' in v: if 'before' in v:
val = (self.BEFORE, v['before']) val = (self.BEFORE, v['before'], allow_between_house_number_and_road)
elif 'after' in v: elif 'after' in v:
val = (self.AFTER, v['after']) val = (self.AFTER, v['after'], allow_between_house_number_and_road)
elif 'last' in v: elif 'last' in v:
val = (self.LAST, None) val = (self.LAST, None, False)
elif 'first' in v: elif 'first' in v:
val = (self.FIRST, None) val = (self.FIRST, None, False)
else: else:
raise ValueError('Insertions must contain one of {{first, before, after, last}}. Value was: {}'.format(v)) raise ValueError('Insertions must contain one of {{first, before, after, last}}. Value was: {}'.format(v))
@@ -597,17 +599,17 @@ class AddressFormatter(object):
conditional_insertions = v conditional_insertions = v
break break
order, other = None, None order, other, allow_between_house_number_and_road = None, None, False
# Check the conditional probabilities first # Check the conditional probabilities first
if conditional_insertions is not None: if conditional_insertions is not None:
values, probs = conditional_insertions values, probs = conditional_insertions
order, other = weighted_choice(values, probs) order, other, allow_between_house_number_and_road = weighted_choice(values, probs)
# If there are no conditional probabilites or the "default" value was chosen, sample from the marginals # If there are no conditional probabilites or the "default" value was chosen, sample from the marginals
if other is None: if other is None:
values, probs = insertions values, probs = insertions
order, other = weighted_choice(values, probs) order, other, allow_between_house_number_and_road = weighted_choice(values, probs)
# Even though we may change the value of "other" below, use # Even though we may change the value of "other" below, use
# the original cache key because changes from here on are # the original cache key because changes from here on are
@@ -630,6 +632,7 @@ class AddressFormatter(object):
# house_number, unit, road, which we don't want. So effectively # house_number, unit, road, which we don't want. So effectively
# treat house_number and road as an atomic unit. # treat house_number and road as an atomic unit.
if not allow_between_house_number_and_road:
other_token = self.tag_token(other) other_token = self.tag_token(other)
if other == self.HOUSE_NUMBER and component != self.ROAD: if other == self.HOUSE_NUMBER and component != self.ROAD: