adjust suggestion algorithm to get better results (#20573)
This commit is contained in:
parent
48dd43d3ea
commit
610cc3da0b
|
@ -16,6 +16,7 @@
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import datetime
|
||||
import math
|
||||
import random
|
||||
import re
|
||||
import urllib2
|
||||
|
@ -233,9 +234,9 @@ def get_suggestions(request, cell, user_data, places_data):
|
|||
'properties': feature['properties']})
|
||||
break
|
||||
|
||||
if random.random() < 0.6:
|
||||
tile_data.append({'key': 'airquality'})
|
||||
if random.random() < 0.3:
|
||||
tile_data.append({'key': 'airquality'})
|
||||
if random.random() < 0.1:
|
||||
tile_data.append({'key': 'pollen'})
|
||||
|
||||
if user_data.get('address_street'):
|
||||
|
@ -271,12 +272,18 @@ def get_suggestions(request, cell, user_data, places_data):
|
|||
cache_duration=300).json()
|
||||
except RequestException:
|
||||
continue
|
||||
if not data_result.get('features'):
|
||||
features = data_result.get('features')
|
||||
if not features:
|
||||
continue
|
||||
for feature in random.sample(
|
||||
data_result.get('features'), min(len(data_result.get('features')), 2)):
|
||||
tile_data.append({'key': maplayer.slug,
|
||||
'properties': feature['properties']})
|
||||
for feature in features:
|
||||
# thanks to the flat earth society
|
||||
feature['distance'] = math.sqrt(
|
||||
(float(coord['lon']) - feature['geometry']['coordinates'][0])**2 +
|
||||
(float(coord['lat']) - feature['geometry']['coordinates'][1])**2)
|
||||
features.sort(key=lambda x: x['distance'])
|
||||
# take two closest features
|
||||
for feature in features[:2]:
|
||||
tile_data.append({'key': maplayer.slug, 'properties': feature['properties']})
|
||||
|
||||
dashboard = DashboardCell.objects.all()[0]
|
||||
cells = []
|
||||
|
|
Loading…
Reference in New Issue