Commit 3705398481c7d08d2f2648789d370530fe3abdd7
1 parent
4f534f4a
Exists in
master
and in
28 other branches
ActionItem128: filtered stop words from the query
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1183 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
3 changed files
with
18 additions
and
1 deletions
Show diff stats
app/controllers/public/search_controller.rb
| @@ -39,7 +39,8 @@ class SearchController < ApplicationController | @@ -39,7 +39,8 @@ class SearchController < ApplicationController | ||
| 39 | 39 | ||
| 40 | def index | 40 | def index |
| 41 | @query = params[:query] || '' | 41 | @query = params[:query] || '' |
| 42 | - @results = search(@query) | 42 | + @filtered_query = remove_stop_words(@query) |
| 43 | + @results = search(@filtered_query) | ||
| 43 | end | 44 | end |
| 44 | 45 | ||
| 45 | def tags | 46 | def tags |
app/helpers/search_helper.rb
| 1 | module SearchHelper | 1 | module SearchHelper |
| 2 | 2 | ||
| 3 | + STOP_WORDS = { | ||
| 4 | + 'pt_BR' => Ferret::Analysis::FULL_PORTUGUESE_STOP_WORDS, | ||
| 5 | + 'en' => Ferret::Analysis::FULL_ENGLISH_STOP_WORDS, | ||
| 6 | + } | ||
| 7 | + | ||
| 3 | def relevance_for(hit) | 8 | def relevance_for(hit) |
| 4 | n = (hit.ferret_score if hit.respond_to?(:ferret_score)) | 9 | n = (hit.ferret_score if hit.respond_to?(:ferret_score)) |
| 5 | n ||= 1.0 | 10 | n ||= 1.0 |
| 6 | (n * 100.0).round | 11 | (n * 100.0).round |
| 7 | end | 12 | end |
| 13 | + | ||
| 14 | + def remove_stop_words(query) | ||
| 15 | + (query.downcase.split(' ') - STOP_WORDS[Locale.current.to_s]).join(' ') | ||
| 16 | + end | ||
| 8 | end | 17 | end |
test/functional/search_controller_test.rb
| @@ -21,4 +21,11 @@ class SearchControllerTest < Test::Unit::TestCase | @@ -21,4 +21,11 @@ class SearchControllerTest < Test::Unit::TestCase | ||
| 21 | 21 | ||
| 22 | end | 22 | end |
| 23 | 23 | ||
| 24 | + should 'filter stop words' do | ||
| 25 | + get 'index', :query => 'a carne da vaca' | ||
| 26 | + assert_response :success | ||
| 27 | + assert_template 'index' | ||
| 28 | + assert_equal 'carne vaca', assigns('filtered_query') | ||
| 29 | + end | ||
| 30 | + | ||
| 24 | end | 31 | end |