Browse Source

Merge pull request #827 from davidar/spell

[enh] show spelling corrections
Adam Tauber 8 years ago
parent
commit
b1d49bacb0
5 changed files with 23 additions and 0 deletions
  1. 4 0
      searx/engines/google.py
  2. 4 0
      searx/results.py
  3. 12 0
      searx/templates/oscar/results.html
  4. 2 0
      searx/webapp.py
  5. 1 0
      tests/unit/test_webapp.py

+ 4 - 0
searx/engines/google.py

@@ -112,6 +112,7 @@ title_xpath = './/h3'
 content_xpath = './/span[@class="st"]'
 content_misc_xpath = './/div[@class="f slp"]'
 suggestion_xpath = '//p[@class="_Bmc"]'
+spelling_suggestion_xpath = '//a[@class="spell"]'
 
 # map : detail location
 map_address_xpath = './/div[@class="s"]//table//td[2]/span/text()'
@@ -275,6 +276,9 @@ def response(resp):
         # append suggestion
         results.append({'suggestion': extract_text(suggestion)})
 
+    for correction in dom.xpath(spelling_suggestion_xpath):
+        results.append({'correction': extract_text(correction)})
+
     # return results
     return results
 

+ 4 - 0
searx/results.py

@@ -127,6 +127,7 @@ class ResultContainer(object):
         self.infoboxes = []
         self.suggestions = set()
         self.answers = set()
+        self.corrections = set()
         self._number_of_results = []
         self._ordered = False
         self.paging = False
@@ -140,6 +141,9 @@ class ResultContainer(object):
             elif 'answer' in result:
                 self.answers.add(result['answer'])
                 results.remove(result)
+            elif 'correction' in result:
+                self.corrections.add(result['correction'])
+                results.remove(result)
             elif 'infobox' in result:
                 self._merge_infobox(result)
                 results.remove(result)

+ 12 - 0
searx/templates/oscar/results.html

@@ -16,6 +16,18 @@
             <h1 class="sr-only">{{ _('Search results') }}</h1>
             {% include 'oscar/search.html' %}
 
+            {% if corrections %}
+            <div class="result">
+                <span class="result_header text-muted form-inline pull-left suggestion_item">{{ _('Try searching for:') }}</span>
+                {% for correction in corrections %}
+                    <form method="{{ method or 'POST' }}" action="{{ url_for('index') }}" role="navigation" class="form-inline pull-left suggestion_item">
+                        <input type="hidden" name="q" value="{{ correction }}">
+                        <button type="submit" class="btn btn-default btn-xs">{{ correction }}</button>
+                    </form>
+                {% endfor %}
+            </div>
+            {% endif %}
+
             {% if answers %}
             {% for answer in answers %}
             <div class="result well">

+ 2 - 0
searx/webapp.py

@@ -479,6 +479,7 @@ def index():
                                     'number_of_results': number_of_results,
                                     'results': results,
                                     'answers': list(result_container.answers),
+                                    'corrections': list(result_container.corrections),
                                     'infoboxes': result_container.infoboxes,
                                     'suggestions': list(result_container.suggestions)}),
                         mimetype='application/json')
@@ -515,6 +516,7 @@ def index():
         advanced_search=advanced_search,
         suggestions=result_container.suggestions,
         answers=result_container.answers,
+        corrections=result_container.corrections,
         infoboxes=result_container.infoboxes,
         paging=result_container.paging,
         current_language=search_query.lang,

+ 1 - 0
tests/unit/test_webapp.py

@@ -36,6 +36,7 @@ class ViewsTestCase(SearxTestCase):
         def search_mock(search_self, *args):
             search_self.result_container = Mock(get_ordered_results=lambda: self.test_results,
                                                 answers=set(),
+                                                corrections=set(),
                                                 suggestions=set(),
                                                 infoboxes=[],
                                                 results=self.test_results,