diff --git a/server/lib/python/cartodb_services/cartodb_services/tomtom/geocoder.py b/server/lib/python/cartodb_services/cartodb_services/tomtom/geocoder.py index 4b3843b..42970f8 100644 --- a/server/lib/python/cartodb_services/cartodb_services/tomtom/geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/tomtom/geocoder.py @@ -5,6 +5,7 @@ import json import requests from uritemplate import URITemplate from math import tanh +from cartodb_services import PRECISION_PRECISE, PRECISION_INTERPOLATED from cartodb_services.metrics import Traceable from cartodb_services.tools.exceptions import ServiceException from cartodb_services.tools.qps import qps_retry @@ -22,6 +23,7 @@ ENTRY_LAT = 'lat' EMPTY_RESPONSE = [[], {}] SCORE_NORMALIZATION_FACTOR = 0.15 +PRECISION_SCORE_THRESHOLD = 0.5 class TomTomGeocoder(Traceable): ''' @@ -133,9 +135,15 @@ class TomTomGeocoder(Traceable): return EMPTY_RESPONSE def _extract_metadata_from_result(self, result): + score = self._normalize_score(result['score']) return { - 'relevance': self._normalize_score(result['score']) + 'relevance': score, + 'precision': self._precision_from_score(score) } def _normalize_score(self, score): return tanh(score * SCORE_NORMALIZATION_FACTOR) + + def _precision_from_score(self, score): + return PRECISION_PRECISE \ + if score > PRECISION_SCORE_THRESHOLD else PRECISION_INTERPOLATED diff --git a/test/integration/test_street_functions.py b/test/integration/test_street_functions.py index cfc4547..970e1f7 100644 --- a/test/integration/test_street_functions.py +++ b/test/integration/test_street_functions.py @@ -73,7 +73,7 @@ class TestStreetFunctionsSetUp(TestCase): 'Plaza España, Barcelona': {'relevance': 0.9, 'precision': 'precise'}, 'Santiago Rusiñol 123, Valladolid': - {'relevance': 0.8, 'precision': 'interpolated'} + {'relevance': 0.56, 'precision': 'interpolated'} } HERE_METADATAS = { @@ -84,9 +84,10 @@ class TestStreetFunctionsSetUp(TestCase): } TOMTOM_METADATAS = { - 'Plaza España, Barcelona': { - 'relevance': 0.85 - } + 'Plaza España, Barcelona': + {'relevance': 0.85, 'precision': 'precise'}, + 'Santiago Rusiñol 123, Valladolid': + {'relevance': 0.448283494533, 'precision': 'interpolated'} } MAPBOX_METADATAS = { @@ -369,7 +370,7 @@ class TestBulkStreetFunctions(TestStreetFunctionsSetUp): "UNION " \ "select 2 as cartodb_id, ''Spain'' as country, " \ "''Valladolid'' as city, " \ - "''Calle Santiago Rusiñol 123'' as street' " \ + "''Santiago Rusiñol 123'' as street' " \ ", 'street', 'city', NULL, 'country')" response = self._run_authenticated(query)