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 b064ea5..4b3843b 100644 --- a/server/lib/python/cartodb_services/cartodb_services/tomtom/geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/tomtom/geocoder.py @@ -4,6 +4,7 @@ import json import requests from uritemplate import URITemplate +from math import tanh from cartodb_services.metrics import Traceable from cartodb_services.tools.exceptions import ServiceException from cartodb_services.tools.qps import qps_retry @@ -20,6 +21,7 @@ ENTRY_LON = 'lon' ENTRY_LAT = 'lat' EMPTY_RESPONSE = [[], {}] +SCORE_NORMALIZATION_FACTOR = 0.15 class TomTomGeocoder(Traceable): ''' @@ -132,5 +134,8 @@ class TomTomGeocoder(Traceable): def _extract_metadata_from_result(self, result): return { - 'relevance': result['score'] # TODO: normalize + 'relevance': self._normalize_score(result['score']) } + + def _normalize_score(self, score): + return tanh(score * SCORE_NORMALIZATION_FACTOR) diff --git a/test/integration/test_street_functions.py b/test/integration/test_street_functions.py index b0c6deb..79af788 100644 --- a/test/integration/test_street_functions.py +++ b/test/integration/test_street_functions.py @@ -73,15 +73,21 @@ class TestStreetFunctionsSetUp(TestCase): 'Plaza España, Barcelona': 1 } - MAPBOX_RELEVANCES = { + MAPBOX_RELEVANCES = HERE_RELEVANCES.copy() + MAPBOX_RELEVANCES.update({ 'Plaza España, Barcelona': 0.75 - } + }) + + TOMTOM_RELEVANCES = MAPBOX_RELEVANCES.copy() + TOMTOM_RELEVANCES.update({ + 'Plaza España, Barcelona': 0.85 + }) RELEVANCES = { + 'google': HERE_RELEVANCES, 'here': HERE_RELEVANCES, - 'tomtom': HERE_RELEVANCES, - 'mapbox': MAPBOX_RELEVANCES, - 'google': HERE_RELEVANCES + 'tomtom': TOMTOM_RELEVANCES, + 'mapbox': MAPBOX_RELEVANCES } def setUp(self): @@ -346,8 +352,10 @@ class TestBulkStreetFunctions(TestStreetFunctionsSetUp): ", 'street', 'city', NULL, 'country')" response = self._run_authenticated(query) - assert_true(isclose(response['rows'][0]['metadata']['relevance'], - self.relevances['Plaza España, Barcelona'])) + relevance = response['rows'][0]['metadata']['relevance'] + expected_relevance = self.relevances['Plaza España, Barcelona'] + assert_true(isclose(relevance, expected_relevance, 0.05), + '{} not close to {}'.format(relevance, expected_relevance)) def _run_authenticated(self, query): authenticated_query = "{}&api_key={}".format(query,