[enh] multilingual autocomplete

implemented for wikipedia, qwant and google
This commit is contained in:
a01200356 2016-03-29 18:53:31 -06:00
parent 71de593a5b
commit 94cb3a7f11
2 changed files with 18 additions and 12 deletions

View file

@ -110,7 +110,7 @@ def searx_bang(full_query):
return list(result_set)
def dbpedia(query):
def dbpedia(query, lang):
# dbpedia autocompleter, no HTTPS
autocomplete_url = 'http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?'
@ -126,7 +126,7 @@ def dbpedia(query):
return results
def duckduckgo(query):
def duckduckgo(query, lang):
# duckduckgo autocompleter
url = 'https://ac.duckduckgo.com/ac/?{0}&type=list'
@ -136,11 +136,11 @@ def duckduckgo(query):
return []
def google(query):
def google(query, lang):
# google autocompleter
autocomplete_url = 'https://suggestqueries.google.com/complete/search?client=toolbar&'
response = get(autocomplete_url + urlencode(dict(q=query)))
response = get(autocomplete_url + urlencode(dict(hl=lang, q=query)))
results = []
@ -151,8 +151,8 @@ def google(query):
return results
def startpage(query):
# wikipedia autocompleter
def startpage(query, lang):
# startpage autocompleter
url = 'https://startpage.com/do/suggest?{query}'
resp = get(url.format(query=urlencode({'query': query}))).text.split('\n')
@ -161,11 +161,11 @@ def startpage(query):
return []
def qwant(query):
def qwant(query, lang):
# qwant autocompleter (additional parameter : lang=en_en&count=xxx )
url = 'https://api.qwant.com/api/suggest?{query}'
resp = get(url.format(query=urlencode({'q': query})))
resp = get(url.format(query=urlencode({'q': query, 'lang': lang})))
results = []
@ -178,9 +178,9 @@ def qwant(query):
return results
def wikipedia(query):
def wikipedia(query, lang):
# wikipedia autocompleter
url = 'https://en.wikipedia.org/w/api.php?action=opensearch&{0}&limit=10&namespace=0&format=json'
url = 'https://' + lang + '.wikipedia.org/w/api.php?action=opensearch&{0}&limit=10&namespace=0&format=json'
resp = loads(get(url.format(urlencode(dict(search=query)))).text)
if len(resp) > 1:

View file

@ -492,7 +492,7 @@ def autocompleter():
if not query.getSearchQuery():
return '', 400
# run autocompleter
# get autocompleter
completer = autocomplete_backends.get(request.cookies.get('autocomplete', settings['search']['autocomplete']))
# parse searx specific autocompleter results like !bang
@ -500,8 +500,14 @@ def autocompleter():
# normal autocompletion results only appear if max 3 inner results returned
if len(raw_results) <= 3 and completer:
# get language from cookie
language = request.cookies.get('language')
if not language or language == 'all':
language = 'en'
else:
language = language.split('_')[0]
# run autocompletion
raw_results.extend(completer(query.getSearchQuery()))
raw_results.extend(completer(query.getSearchQuery(), language))
# parse results (write :language and !engine back to result string)
results = []