Skip to content

Commit

Permalink
cached autocorrect results and other things. resolves Unicaronas#30
Browse files Browse the repository at this point in the history
  • Loading branch information
Maronato committed Oct 25, 2018
1 parent ad8b31d commit 4732768
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 29 deletions.
19 changes: 17 additions & 2 deletions search/finder/grammar.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from .base import BaseFinder
import re
import pickle
from collections import Counter
from unidecode import unidecode
from .base import BaseFinder
from .redis import RedisFinder


def words(text):
Expand Down Expand Up @@ -92,6 +93,20 @@ def _transform(self, term):
to perform word corrections on it
and find a known synonym that is better
understood by the later finders
Since it takes a long time to correct each sentence, cache
corrected results for some time.
"""
corrected_term = self.correct_sentence(term.query)
# Use redis as cache
cache = RedisFinder()

# Get the cached results if available
cached_result = cache.get_key(term.query)
if cached_result is not None:
corrected_term = cached_result
else:
# If the corrected term is not cached, process it normally
corrected_term = self.correct_sentence(term.query)
# and then cache it
cache.set_key(term.query, corrected_term)
return self.perform_transform(term, corrected_term)
54 changes: 29 additions & 25 deletions search/finder/synonym.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,43 @@ def get_synonym_map(self):
"""Get a map of synonyms
Used on autocorrected words to find the result
"""
POSTO_IPIRANGA = 'posto ipiranga, unicamp'
SP = 'sao paulo, sp'
RODOVIARIA_TIETE = 'rodoviaria tiete, sao paulo'

s_map = [
# Map to posto ipiranga, Barão geraldo
('unicamp', 'posto ipiranga, unicamp'),
('unicamp', 'posto ipiranga unicamp'),
('universidade estadual de campinas', 'posto ipiranga, unicamp'),
('unicamp', POSTO_IPIRANGA),
('unichamps', POSTO_IPIRANGA),
('universidade estadual de campinas', POSTO_IPIRANGA),

('posto ipiranga, unicamp', 'posto ipiranga, unicamp'),
('posto ipiranga unicamp', 'posto ipiranga, unicamp'),
('posto ipiranga', 'posto ipiranga, unicamp'),
('posto da 1', 'posto ipiranga, unicamp'),
('posto', 'posto ipiranga, unicamp'),
('posto ipiranga, unicamp', POSTO_IPIRANGA),
('posto ipiranga unicamp', POSTO_IPIRANGA),
('posto ipiranga', POSTO_IPIRANGA),
('posto da 1', POSTO_IPIRANGA),
('posto', POSTO_IPIRANGA),

('barao geraldo', 'posto ipiranga, unicamp'),
('barao', 'posto ipiranga, unicamp'),
('bg', 'posto ipiranga, unicamp'),
('barao geraldo', POSTO_IPIRANGA),
('barao', POSTO_IPIRANGA),
('bg', POSTO_IPIRANGA),

# Map to generic São Paulo, SP
('sao paulo, sao paulo', 'sao paulo, sp'),
('sao paulo sao paulo', 'sao paulo, sp'),
('sao paulo, sp', 'sao paulo, sp'),
('sao paulo sp', 'sao paulo, sp'),
('sao paulo', 'sao paulo, sp'),
('sp', 'sao paulo, sp'),
('sampa', 'sao paulo, sp'),
('sao paulo, sao paulo', SP),
('sao paulo sao paulo', SP),
('sao paulo, sp', SP),
('sao paulo sp', SP),
('sao paulo', SP),
('sp', SP),
('sampa', SP),

# Map to rodoviária tietê
('rodoviaria tiete, sao paulo', 'rodoviaria tiete, sao paulo'),
('rodoviaria tiete sao paulo', 'rodoviaria tiete, sao paulo'),
('rodoviaria tiete, sp', 'rodoviaria tiete, sao paulo'),
('rodoviaria tiete sp', 'rodoviaria tiete, sao paulo'),
('rodoviaria tiete', 'rodoviaria tiete, sao paulo'),
('estacao tiete', 'rodoviaria tiete, sao paulo'),
('tiete', 'rodoviaria tiete, sao paulo'),
('rodoviaria tiete, sao paulo', RODOVIARIA_TIETE),
('rodoviaria tiete sao paulo', RODOVIARIA_TIETE),
('rodoviaria tiete, sp', RODOVIARIA_TIETE),
('rodoviaria tiete sp', RODOVIARIA_TIETE),
('rodoviaria tiete', RODOVIARIA_TIETE),
('estacao tiete', RODOVIARIA_TIETE),
('tiete', RODOVIARIA_TIETE),
]
return OrderedDict(s_map)

Expand Down
2 changes: 1 addition & 1 deletion third_parties/query/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.http import HttpRequest
from search.term.base import BaseTerm
from datetime import datetime
from rest_framework.request import Request
from django.http import HttpRequest


class BaseSearchQuery(object):
Expand Down
4 changes: 3 additions & 1 deletion third_parties/query/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@


class SearchQuery(BaseSearchQuery):
pass

def __str__(self):
return f"Query: (dest '{self.destination.query[:10]}', ori '{self.origin.query[:10]}', dt_gt '{self.datetime_gte}', dt_lt '{self.datetime_lte}', price '{self.price_lte}')"
12 changes: 12 additions & 0 deletions third_parties/result/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
class Result(BaseResult):
"""Results"""

def __str__(self):
return f"Result: ({len(self.items)} items)"

def __repr__(self):
return str(self)


class ResultItem(BaseResultItem):
"""Result item"""

def __str__(self):
return f"ResultItem: (dest '{self.destination[:10]}', ori '{self.origin[:10]}', dt '{self.datetime}', price '{self.price}')"

def __repr__(self):
return str(self)
12 changes: 12 additions & 0 deletions trips/api/versions/v1_0/serializers/search.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.utils import timezone
from rest_framework import serializers
from project.utils import local_versioned_url_name
from third_parties.utils import get_search_keys
Expand Down Expand Up @@ -47,3 +48,14 @@ class ThirdPartyQuerySerializer(serializers.Serializer):
required=False,
help_text=f"Fontes das caronas. Lista separada por espaços com as opções `{', '.join(get_search_keys())}`, ou `all` para todas. `all` por padrão"
)

def validate_datetime_gte(self, value):
return max(timezone.now(), value)

def validate_datetime_lte(self, value):
return max(timezone.now(), value)

def validate(self, data):
if data['datetime_lte'] < data['datetime_gte']:
raise serializers.ValidationError("'datetime_gte' deve ser antes que 'datetime_lte'")
return data

0 comments on commit 4732768

Please sign in to comment.