add wrapper class for place data passed to tokenizer

This is mostly for convenience and documentation purposes.
This commit is contained in:
Sarah Hoffmann
2021-09-29 10:37:54 +02:00
parent d44a428b74
commit 231250f2eb
9 changed files with 84 additions and 34 deletions

View File

@@ -0,0 +1,44 @@
"""
Wrapper around place information the indexer gets from the database and hands to
the tokenizer.
"""
import psycopg2.extras
class PlaceInfo:
""" Data class containing all information the tokenizer gets about a
place it should process the names for.
"""
def __init__(self, info):
self._info = info
def analyze(self, analyzer):
""" Process this place with the given tokenizer and return the
result in psycopg2-compatible Json.
"""
return psycopg2.extras.Json(analyzer.process_place(self))
@property
def name(self):
""" A dictionary with the names of the place or None if the place
has no names.
"""
return self._info.get('name')
@property
def address(self):
""" A dictionary with the address elements of the place
or None if no address information is available.
"""
return self._info.get('address')
@property
def country_feature(self):
""" Return the country code if the place is a valid country boundary.
"""
return self._info.get('country_feature')

View File

@@ -4,14 +4,16 @@ tasks.
"""
import functools
import psycopg2.extras
from psycopg2 import sql as pysql
from nominatim.indexer.place_info import PlaceInfo
# pylint: disable=C0111
def _mk_valuelist(template, num):
return pysql.SQL(',').join([pysql.SQL(template)] * num)
class AbstractPlacexRunner:
""" Returns SQL commands for indexing of the placex table.
"""
@@ -47,7 +49,7 @@ class AbstractPlacexRunner:
for place in places:
for field in ('place_id', 'name', 'address', 'linked_place_id'):
values.append(place[field])
values.append(psycopg2.extras.Json(self.analyzer.process_place(place)))
values.append(PlaceInfo(place).analyze(self.analyzer))
worker.perform(self._index_sql(len(places)), values)
@@ -141,7 +143,7 @@ class InterpolationRunner:
values = []
for place in places:
values.extend((place[x] for x in ('place_id', 'address')))
values.append(psycopg2.extras.Json(self.analyzer.process_place(place)))
values.append(PlaceInfo(place).analyze(self.analyzer))
worker.perform(self._index_sql(len(places)), values)