[parser/formatting] Appendinge suburb between the road line and any subsequent lines for all bottom-up address formats. Effectively inserts neighborhoods into our version without making the OpenCage formats overly verbose. Also fixing post-format replace with group capture
This commit is contained in:
@@ -97,8 +97,21 @@ class AddressFormatter(object):
|
|||||||
subprocess.check_call(['git', 'clone', FORMATTER_GIT_REPO, self.formatter_repo_path])
|
subprocess.check_call(['git', 'clone', FORMATTER_GIT_REPO, self.formatter_repo_path])
|
||||||
|
|
||||||
def load_config(self):
|
def load_config(self):
|
||||||
self.config = yaml.load(open(os.path.join(self.formatter_repo_path,
|
config = yaml.load(open(os.path.join(self.formatter_repo_path,
|
||||||
'conf/countries/worldwide.yaml')))
|
'conf/countries/worldwide.yaml')))
|
||||||
|
for key, value in config.items():
|
||||||
|
if hasattr(value, 'items'):
|
||||||
|
address_template = value.get('address_template')
|
||||||
|
if address_template:
|
||||||
|
value['address_template'] = self.add_suburb_tags(address_template)
|
||||||
|
|
||||||
|
post_format_replacements = value.get('postformat_replace')
|
||||||
|
if post_format_replacements:
|
||||||
|
value['postformat_replace'] = [[pattern, replacement.replace('$', '\\')] for pattern, replacement in post_format_replacements]
|
||||||
|
else:
|
||||||
|
address_template = value
|
||||||
|
config[key] = self.add_suburb_tags(value)
|
||||||
|
self.config = config
|
||||||
|
|
||||||
def component_aliases(self):
|
def component_aliases(self):
|
||||||
self.aliases = OrderedDict()
|
self.aliases = OrderedDict()
|
||||||
@@ -119,6 +132,18 @@ class AddressFormatter(object):
|
|||||||
def country_template(self, c):
|
def country_template(self, c):
|
||||||
return self.config.get(c, self.config['default'])
|
return self.config.get(c, self.config['default'])
|
||||||
|
|
||||||
|
post_suburb_keys = re.compile('|'.join((CITY, STATE, STATE_DISTRICT, POSTCODE, COUNTRY)), re.I)
|
||||||
|
|
||||||
|
def add_suburb_tags(self, template):
|
||||||
|
suburb_included = 'suburb' in template
|
||||||
|
new_components = []
|
||||||
|
for line in template.split('\n'):
|
||||||
|
post_suburb = self.post_suburb_keys.search(line)
|
||||||
|
new_components.append(line.rstrip('\n'))
|
||||||
|
if u'road' in line and not suburb_included and not post_suburb:
|
||||||
|
new_components.append(u'{{{suburb}}}')
|
||||||
|
return u'\n'.join(new_components)
|
||||||
|
|
||||||
def render_template(self, template, components, tagged=False):
|
def render_template(self, template, components, tagged=False):
|
||||||
def render_first(text):
|
def render_first(text):
|
||||||
text = pystache.render(text, **components)
|
text = pystache.render(text, **components)
|
||||||
|
|||||||
Reference in New Issue
Block a user