Module: Pirate
- Defined in:
- lib/pirate.rb
Constant Summary collapse
- @@result =
:redirect => ‘exact’ || ‘partial’ || ‘levenshtein’ || nil
{ :redirect => nil, :instance => nil, :stores => [], :coupons => [], :tags => [], :notice => nil }
- @@tag_enabled =
false
- @@store_per_page =
4
- @@coupon_per_page =
10
- @@page =
{ :stores => 1, :coupons => 1 }
Class Method Summary collapse
- .calculate_stores_by_match(query) ⇒ Object
- .calculate_stores_by_similarity(query) ⇒ Object
- .exact_match_redirects(query) ⇒ Object
- .find(search_params) ⇒ Object
- .levenshtein_search_stores_redirect(query) ⇒ Object
- .levenshtein_search_tags_redirect(query) ⇒ Object
- .reset_result ⇒ Object
Class Method Details
.calculate_stores_by_match(query) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/pirate.rb', line 120 def self.calculate_stores_by_match(query) # Misspelling Match -- search term is a misspelling of the store name # create Levenshtein matcher for our user's query, with only alphanumerics # downcase each store name and remove non-alphanumeric characters, then match em, and pick the minimum. m = Amatch::Levenshtein.new(query) # select only names make an array of names store_names = Store.active.all(:select => 'id, name').collect { |s| [s.id, s.name.gsub(/[^\w]/, '')] } # array of pairs: [ levdistance, name ] take maximum return store_names.collect { |n| [ m.match(n[1].downcase.gsub(/[^\w]/, '')), n[0], n[1] ] } end |
.calculate_stores_by_similarity(query) ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/pirate.rb', line 110 def self.calculate_stores_by_similarity(query) m = Amatch::Levenshtein.new(query) # select only names make an array of names store_names = Store.active.all(:select => 'id, name').collect { |s| [s.id, s.name.gsub(/[^\w]/, '')] } # array of pairs: [ levdistance, name ] take maximum return store_names.collect { |n| [ m.similar(n[1].downcase.gsub(/[^\w]/, '')), n[0], n[1] ] } end |
.exact_match_redirects(query) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/pirate.rb', line 47 def self.exact_match_redirects(query) reset_result # to force a search page and avoid redirection, add ?no_redirect=true to your url # Case Insensitive Match -- try to match store by near-exact name or domain match foundstores = Store.active.find(:all, :conditions => [ "LOWER(name) = ? or LOWER(homepage) LIKE ?", query.downcase, "%#{query.downcase}" ] ) if !foundstores.nil? and foundstores.length == 1 @@result = { :redirect => 'exact', :instance => foundstores.first } and return end if @@tag_enabled = Tag.find(:all, :conditions => [ "LOWER(name) = ?", query.downcase ] ) if !.nil? and .length == 1 @@result = { :redirect => 'exact', :instance => .first } and return end end # Partial Match -- a known store name or tag name exists within the search query foundstores = Store.active.find(:all, :conditions => [ "? LIKE CONCAT('%',LOWER(name),'%')", query.downcase ]) if !foundstores.nil? and foundstores.length == 1 store = foundstores.first @@result = { :redirect => 'partial', :instance => store, :notice => "We matched your search to "#{store.name}". Search instead for <a href='/searches/new?searchInput=#{URI.escape(query)}&no_redirect=true'>#{query}</a>" } return end # I don't think we need this, partial match in Tag is confusing users #if @@tag_enabled # foundtags = Tag.find(:all, :conditions => [ "? LIKE CONCAT('%',LOWER(name),'%')", query.downcase ]) # if !foundtags.nil? and foundtags.length == 1 # @@result = { :redirect => 'partial', :instance => foundtags.first, # :notice => "We matched your search to "#{foundtags.first}". Search instead for <a href='/searches/new?searchInput=#{URI.escape(query)}&no_redirect=true'>#{query}</a>" } # end #end end |
.find(search_params) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/pirate.rb', line 26 def self.find(search_params) reset_result @query = search_params[:searchInput] if search_params[:no_redirect].nil? exact_match_redirects(@query) levenshtein_search_stores_redirect(@query) unless @@result[:redirect] (@query) if @@tag_enabled && !@@result[:redirect] end if @@result[:redirect].nil? @@page[search_params[:cat].to_sym] = search_params[:page] if search_params[:cat] && search_params[:page] @@result[:stores] = Store.active.remove_punct_from_name(@query.downcase).paginate(:per_page => @@store_per_page, :page => @@page[:stores]) @@result[:coupons] = Coupon.active.ordered.name_or_store_name_or_description_like(@query).paginate(:per_page => @@coupon_per_page, :page => @@page[:coupons]) @@result[:tags] = Coupon.(@query) if @@tag_enabled end return @@result end |
.levenshtein_search_stores_redirect(query) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/pirate.rb', line 83 def self.levenshtein_search_stores_redirect(query) reset_result lowquery = query.downcase.gsub(/[^\w]/, '') foundstores = calculate_stores_by_similarity(lowquery) foundstore = foundstores.max if !foundstore.nil? && foundstore[0] > 0.5 store = Store.active.find(foundstore[1]) @@result = { :redirect => 'levenshtein', :instance => store, :notice => "We matched your search to "#{store.name}". Search instead for <a href='/searches/new?searchInput=#{URI.escape(query)}&no_redirect=true'>#{query}</a>" } return end maxdist = (lowquery.length / 4).to_i foundstores = calculate_stores_by_match(lowquery) foundstore = foundstores.min if !foundstore.nil? && foundstore[0] <= maxdist store = Store.active.find(foundstore[1]) @@result = { :redirect => 'levenshtein', :instance => store, :notice => "We matched your search to "#{store.name}". Search instead for <a href='/searches/new?searchInput=#{URI.escape(query)}&no_redirect=true'>#{query}</a>" } return end end |
.levenshtein_search_tags_redirect(query) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/pirate.rb', line 133 def self.(query) reset_result # Misspelling Match -- search term is a misspelling of the store name # create Levenshtein matcher for our user's query, with only alphanumerics # downcase each store name and remove non-alphanumeric characters, then match em, and pick the minimum. lowquery = query.downcase.gsub(/[^\w]/, '') m = Amatch::Levenshtein.new(lowquery) tag_names = Tag.all(:select => 'name').collect { |t| t.name } foundtag = tag_names.collect { |n| [ m.similar(n.downcase.gsub(/[^\w]/, '')), n ] }.max if !foundtag.nil? && foundtag[0] > 0.5 tag = Tag.find_by_name(foundtag[1]) @@result = { :redirect => 'levenshtein', :instance => tag, :notice => "We matched your search to "#{tag.name}". Search instead for <a href='/searches/new?searchInput=#{URI.escape(query)}&no_redirect=true'>#{query}</a>" } return end maxdist = (lowquery.length / 4).to_i # select only names make an array of names array of pairs: [ levdistance, name ] take minimum foundtag = tag_names.collect { |n| [ m.match(n.downcase.gsub(/[^\w]/, '')), n ] }.min if !foundtag.nil? and foundtag[0] <= maxdist tag = Tag.find_by_name(foundtag[1]) @@result = { :redirect => 'levenshtein', :instance => tag, :notice => "We matched your search to "#{tag.name}". Search instead for <a href='/searches/new?searchInput=#{URI.escape(query)}&no_redirect=true'>#{query}</a>" } return end end |
.reset_result ⇒ Object
22 23 24 |
# File 'lib/pirate.rb', line 22 def self.reset_result @@result = { :redirect => nil, :instance => nil, :stores => [], :coupons => [], :tags => [], :notice => nil } end |