TomTom normalization
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
from uritemplate import URITemplate
|
from uritemplate import URITemplate
|
||||||
|
from math import tanh
|
||||||
from cartodb_services.metrics import Traceable
|
from cartodb_services.metrics import Traceable
|
||||||
from cartodb_services.tools.exceptions import ServiceException
|
from cartodb_services.tools.exceptions import ServiceException
|
||||||
from cartodb_services.tools.qps import qps_retry
|
from cartodb_services.tools.qps import qps_retry
|
||||||
@@ -20,6 +21,7 @@ ENTRY_LON = 'lon'
|
|||||||
ENTRY_LAT = 'lat'
|
ENTRY_LAT = 'lat'
|
||||||
EMPTY_RESPONSE = [[], {}]
|
EMPTY_RESPONSE = [[], {}]
|
||||||
|
|
||||||
|
SCORE_NORMALIZATION_FACTOR = 0.15
|
||||||
|
|
||||||
class TomTomGeocoder(Traceable):
|
class TomTomGeocoder(Traceable):
|
||||||
'''
|
'''
|
||||||
@@ -132,5 +134,8 @@ class TomTomGeocoder(Traceable):
|
|||||||
|
|
||||||
def _extract_metadata_from_result(self, result):
|
def _extract_metadata_from_result(self, result):
|
||||||
return {
|
return {
|
||||||
'relevance': result['score'] # TODO: normalize
|
'relevance': self._normalize_score(result['score'])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def _normalize_score(self, score):
|
||||||
|
return tanh(score * SCORE_NORMALIZATION_FACTOR)
|
||||||
|
|||||||
@@ -73,15 +73,21 @@ class TestStreetFunctionsSetUp(TestCase):
|
|||||||
'Plaza España, Barcelona': 1
|
'Plaza España, Barcelona': 1
|
||||||
}
|
}
|
||||||
|
|
||||||
MAPBOX_RELEVANCES = {
|
MAPBOX_RELEVANCES = HERE_RELEVANCES.copy()
|
||||||
|
MAPBOX_RELEVANCES.update({
|
||||||
'Plaza España, Barcelona': 0.75
|
'Plaza España, Barcelona': 0.75
|
||||||
}
|
})
|
||||||
|
|
||||||
|
TOMTOM_RELEVANCES = MAPBOX_RELEVANCES.copy()
|
||||||
|
TOMTOM_RELEVANCES.update({
|
||||||
|
'Plaza España, Barcelona': 0.85
|
||||||
|
})
|
||||||
|
|
||||||
RELEVANCES = {
|
RELEVANCES = {
|
||||||
|
'google': HERE_RELEVANCES,
|
||||||
'here': HERE_RELEVANCES,
|
'here': HERE_RELEVANCES,
|
||||||
'tomtom': HERE_RELEVANCES,
|
'tomtom': TOMTOM_RELEVANCES,
|
||||||
'mapbox': MAPBOX_RELEVANCES,
|
'mapbox': MAPBOX_RELEVANCES
|
||||||
'google': HERE_RELEVANCES
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@@ -346,8 +352,10 @@ class TestBulkStreetFunctions(TestStreetFunctionsSetUp):
|
|||||||
", 'street', 'city', NULL, 'country')"
|
", 'street', 'city', NULL, 'country')"
|
||||||
response = self._run_authenticated(query)
|
response = self._run_authenticated(query)
|
||||||
|
|
||||||
assert_true(isclose(response['rows'][0]['metadata']['relevance'],
|
relevance = response['rows'][0]['metadata']['relevance']
|
||||||
self.relevances['Plaza España, Barcelona']))
|
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):
|
def _run_authenticated(self, query):
|
||||||
authenticated_query = "{}&api_key={}".format(query,
|
authenticated_query = "{}&api_key={}".format(query,
|
||||||
|
|||||||
Reference in New Issue
Block a user