on master help sign in
[enh] show spelling corrections
commit1d30141c207e51c142cab3eee97783f08c1cb5c9 authorDavid A Roberts <d@vidr.cc> dateSat 14 Jan 2017, 18:40 (10 years ago) parentd6cbc6b
5 files changed+23
searx/engines/google.py+4 searx/results.py+4 searx/templates/oscar/results.html+12 searx/webapp.py+2 tests/unit/test_webapp.py+1
searx/engines/google.py+4
@@ -112,6 +112,7 @@ title_xpath = './/h3'
112112content_xpath = './/span[@class="st"]'
113113content_misc_xpath = './/div[@class="f slp"]'
114114suggestion_xpath = '//p[@class="_Bmc"]'
115+spelling_suggestion_xpath = '//a[@class="spell"]'
115116
116117# map : detail location
117118map_address_xpath = './/div[@class="s"]//table//td[2]/span/text()'
@@ -275,6 +276,9 @@ def response(resp):
275276 # append suggestion
276277 results.append({'suggestion': extract_text(suggestion)})
277278
279+ for correction in dom.xpath(spelling_suggestion_xpath):
280+ results.append({'correction': extract_text(correction)})
281+
278282 # return results
279283 return results
280284
searx/results.py+4
@@ -127,6 +127,7 @@ class ResultContainer(object):
127127 self.infoboxes = []
128128 self.suggestions = set()
129129 self.answers = set()
130+ self.corrections = set()
130131 self._number_of_results = []
131132 self._ordered = False
132133 self.paging = False
@@ -140,6 +141,9 @@ class ResultContainer(object):
140141 elif 'answer' in result:
141142 self.answers.add(result['answer'])
142143 results.remove(result)
144+ elif 'correction' in result:
145+ self.corrections.add(result['correction'])
146+ results.remove(result)
143147 elif 'infobox' in result:
144148 self._merge_infobox(result)
145149 results.remove(result)
searx/templates/oscar/results.html+12
@@ -16,6 +16,18 @@
1616 <h1 class="sr-only">{{ _('Search results') }}</h1>
1717 {% include 'oscar/search.html' %}
1818
19+ {% if corrections %}
20+ <div class="result">
21+ <span class="result_header text-muted form-inline pull-left suggestion_item">{{ _('Try searching for:') }}</span>
22+ {% for correction in corrections %}
23+ <form method="{{ method or 'POST' }}" action="{{ url_for('index') }}" role="navigation" class="form-inline pull-left suggestion_item">
24+ <input type="hidden" name="q" value="{{ correction }}">
25+ <button type="submit" class="btn btn-default btn-xs">{{ correction }}</button>
26+ </form>
27+ {% endfor %}
28+ </div>
29+ {% endif %}
30+
1931 {% if answers %}
2032 {% for answer in answers %}
2133 <div class="result well">
searx/webapp.py+2
@@ -479,6 +479,7 @@ def index():
479479 'number_of_results': number_of_results,
480480 'results': results,
481481 'answers': list(result_container.answers),
482+ 'corrections': list(result_container.corrections),
482483 'infoboxes': result_container.infoboxes,
483484 'suggestions': list(result_container.suggestions)}),
484485 mimetype='application/json')
@@ -515,6 +516,7 @@ def index():
515516 advanced_search=advanced_search,
516517 suggestions=result_container.suggestions,
517518 answers=result_container.answers,
519+ corrections=result_container.corrections,
518520 infoboxes=result_container.infoboxes,
519521 paging=result_container.paging,
520522 current_language=search_query.lang,
tests/unit/test_webapp.py+1
@@ -36,6 +36,7 @@ class ViewsTestCase(SearxTestCase):
3636 def search_mock(search_self, *args):
3737 search_self.result_container = Mock(get_ordered_results=lambda: self.test_results,
3838 answers=set(),
39+ corrections=set(),
3940 suggestions=set(),
4041 infoboxes=[],
4142 results=self.test_results,