[boundaries] Adding component-specific admin name probabilities to config (e.g. choose the ISO alpha-2 code 20% of the time, etc.)

This commit is contained in:
Al
2016-05-08 17:56:26 -04:00
parent dcae484851
commit 44f0054170
2 changed files with 30 additions and 2 deletions

View File

@@ -1,4 +1,5 @@
import os
import six
import yaml
from collections import defaultdict
@@ -28,6 +29,13 @@ class BoundaryNames(object):
self.name_keys = name_keys
self.name_key_probs = cdf(probs)
self.component_name_keys = {}
for component, component_config in six.iteritems(nested_get(config, ('names', 'components'), default={})):
component_names = component_config.get('keys')
component_name_keys, component_probs = alternative_probabilities(component_names)
self.component_name_keys[component] = (component_name_keys, cdf(component_probs))
self.exceptions = {}
for props in nested_get(config, ('names', 'exceptions'), default=[]):
@@ -58,7 +66,7 @@ class BoundaryNames(object):
condition_object_type = condition['type']
self.omit_conditions[(object_type, object_id)].add((condition_object_type, condition_object_id))
def name_key(self, props):
def name_key(self, props, component):
object_type = props.get('type')
object_id = safe_encode(props.get('id', ''))
@@ -66,7 +74,8 @@ class BoundaryNames(object):
values, probs = self.exceptions[(object_type, object_id)]
return weighted_choice(values, probs)
return weighted_choice(self.name_keys, self.name_key_probs)
name_keys, probs = self.component_name_keys.get(component, (self.name_keys, self.name_key_probs))
return weighted_choice(name_keys, probs)
def remove_excluded_components(self, components):
all_ids = set()