Module: Pirate

Defined in:
lib/pirate.rb,
lib/pirate/version.rb

Constant Summary collapse

VERSION =
"0.1.6"
@@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

Class Method Details

.calculate_stores_by_match(query) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/pirate.rb', line 146

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



136
137
138
139
140
141
142
143
144
# File 'lib/pirate.rb', line 136

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

.enable_tagObject



30
31
32
# File 'lib/pirate.rb', line 30

def self.enable_tag
  @@tag_enabled = true
end

.exact_match_redirects(query) ⇒ Object



73
74
75
76
77
78
79
80
81
82
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
# File 'lib/pirate.rb', line 73

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
    foundtags = Tag.find(:all, :conditions => [ "LOWER(name) = ?", query.downcase ] )
    if !foundtags.nil? and foundtags.length == 1
      @@result = { :redirect => 'exact', :instance => foundtags.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 #{DB.db_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 &quot;#{store.name}&quot;.  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 &quot;#{foundtags.first}&quot;.  Search instead for <a href='/searches/new?searchInput=#{URI.escape(query)}&no_redirect=true'>#{query}</a>" }
  #  end
  #end
end

.find(search_params) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pirate.rb', line 52

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]
    levenshtein_search_tags_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.related_tags(@query) if @@tag_enabled
  end
  
  return @@result
end

.levenshtein_search_stores_redirect(query) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/pirate.rb', line 109

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 &quot;#{store.name}&quot;.  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 &quot;#{store.name}&quot;.  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



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/pirate.rb', line 159

def self.levenshtein_search_tags_redirect(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 &quot;#{tag.name}&quot;.  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 &quot;#{tag.name}&quot;.  Search instead for <a href='/searches/new?searchInput=#{URI.escape(query)}&no_redirect=true'>#{query}</a>"
               }
    return
  end
end

.reset_resultObject



48
49
50
# File 'lib/pirate.rb', line 48

def self.reset_result
  @@result = { :redirect => nil, :instance => nil, :stores => [], :coupons => [], :tags => [], :notice => nil }
end

.tag_enabledObject



43
44
45
46
# File 'lib/pirate.rb', line 43

def self.tag_enabled
  ActiveSupport::Deprecation.warn "Pirate.tag_enabled is deprecated, please use Pirate.tag_enabled? instead"
  @@tag_enabled
end

.tag_enabled=(value) ⇒ Object



38
39
40
41
# File 'lib/pirate.rb', line 38

def self.tag_enabled=(value)
  ActiveSupport::Deprecation.warn "Pirate.tag_enabled= is deprecated, please use Pirate.enable_tag instead" 
  @@tag_enabled = value
end

.tag_enabled?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/pirate.rb', line 34

def self.tag_enabled?
  @@tag_enabled
end