hunt 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hunt (0.1)
4
+ hunt (0.2)
5
5
  fast-stemmer (~> 1.0)
6
6
  mongo_mapper (~> 0.8.6)
7
7
 
@@ -0,0 +1,38 @@
1
+ $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
2
+ require 'pp'
3
+ require 'hunt'
4
+
5
+ MongoMapper.database = 'testing'
6
+
7
+ class Note
8
+ include MongoMapper::Document
9
+ plugin Hunt
10
+
11
+ key :title, String
12
+ key :tags, Array
13
+
14
+ # Declare which fields to search on.
15
+ # No problem declaring an array key.
16
+ searches :title, :tags
17
+ end
18
+ Note.delete_all # clean the slate
19
+
20
+ # Note that the search terms are stored in a searches hash in the key default.
21
+ # This is so that future versions can allow different combinations of search
22
+ # fields and use different keys in searches.
23
+
24
+ # Also, meaningless words are stripped out and all words less than 3 characters
25
+ # long. The words are then stemmed (http://en.wikipedia.org/wiki/Stemming) so
26
+ # exact matches when searching are not necessary.
27
+
28
+ pp Note.create(:title => 'I am lost without Mongo!', :tags => %w(mongo database nosql))
29
+ #<Note searches: {"default"=>["test", "mongo", "databas", "nosql"]}, title: "This is a test", _id: BSON::ObjectId('...'), tags: ["mongo", "database", "nosql"]>
30
+
31
+ pp Note.create(:title => 'Lost is a tv show', :tags => %w(lost tv))
32
+ #<Note searches: {"default"=>["lost", "tv", "show"]}, title: "Lost is a tv show", _id: BSON::ObjectId('...'), tags: ["lost", "tv"]>
33
+
34
+ pp Note.search('lost').count # 2
35
+ pp Note.search('tv').count # 1
36
+
37
+ # Multiple words work
38
+ pp Note.search('mongo tv').count # 2
@@ -0,0 +1,41 @@
1
+ $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
2
+ require 'pp'
3
+ require 'hunt'
4
+
5
+ MongoMapper.database = 'testing'
6
+
7
+ class Note
8
+ include MongoMapper::Document
9
+ plugin Hunt
10
+
11
+ key :title, String
12
+
13
+ # Declare which fields to search on.
14
+ searches :title
15
+ end
16
+ Note.delete_all # clean the slate
17
+
18
+ # Note that the search terms are stored in a searches hash in the key default.
19
+ # This is so that future versions can allow different combinations of search
20
+ # fields and use different keys in searches.
21
+ #
22
+ # Also, meaningless words are stripped out and all words less than 3 characters
23
+ # long. The words are then stemmed (http://en.wikipedia.org/wiki/Stemming) so
24
+ # exact matches when searching are not necessary.
25
+
26
+ note = Note.create(:title => 'This is a test')
27
+ pp note
28
+ # #<Note searches: {"default"=>["test"]}, title: "This is a test", _id: BSON::ObjectId('...')>
29
+
30
+ note.update_attributes(:title => 'A different test')
31
+ pp note
32
+ # #<Note searches: {"default"=>["differ", "test"]}, title: "A different test", _id: BSON::ObjectId('...')>
33
+
34
+ # Also, you get a handy
35
+ pp Note.search('test').count # 1
36
+ pp Note.search('testing').count # 1
37
+ pp Note.search('testing').all
38
+ # [#<Note searches: {"default"=>["differ", "test"]}, title: "A different test", _id: BSON::ObjectId('...')>]
39
+
40
+ pp Note.search('test').paginate(:page => 1)
41
+ # [#<Note searches: {"default"=>["differ", "test"]}, title: "A different test", _id: BSON::ObjectId('...')>]
@@ -1,4 +1,5 @@
1
1
  require 'fast_stemmer'
2
+ require 'mongo_mapper'
2
3
  require 'hunt/util'
3
4
 
4
5
  module Hunt
@@ -4,22 +4,22 @@ module Hunt
4
4
  StripPunctuationRegex = /[^a-zA-Z0-9]/
5
5
  PunctuationReplacement = ''
6
6
  WordsToIgnore = [
7
- "about", "above", "after", "again", "against", "all",
8
- "and", "any", "are", "aren't", "because", "been",
9
- "before", "being", "below", "between", "both", "but", "cannot",
10
- "can't", "could", "couldn't", "did", "didn't", "does", "doesn't",
7
+ "a", "about", "above", "after", "again", "against", "all", "am", "an",
8
+ "and", "any", "are", "aren't", "as", "at", "be", "because", "been",
9
+ "before", "being", "below", "between", "both", "but", "by", "cannot",
10
+ "can't", "could", "couldn't", "did", "didn't", "do", "does", "doesn't",
11
11
  "doing", "don't", "down", "during", "each", "few", "for", "from",
12
12
  "further", "had", "hadn't", "has", "hasn't", "have", "haven't", "having",
13
- "he'd", "he'll", "her", "here", "here's", "hers", "herself", "he's",
14
- "him", "himself", "his", "how", "how's", "i'd", "if", "i'll", "i'm",
15
- "into", "isn't", "its", "it's", "itself", "i've", "let's",
16
- "more", "most", "mustn't", "myself", "nor", "not",
17
- "off", "once", "only", "other", "ought", "our", "ours", "ourselves",
13
+ "he", "he'd", "he'll", "her", "here", "here's", "hers", "herself", "he's",
14
+ "him", "himself", "his", "how", "how's", "i", "i'd", "if", "i'll", "i'm",
15
+ "in", "into", "is", "isn't", "it", "its", "it's", "itself", "i've", "let's",
16
+ "me", "more", "most", "mustn't", "my", "myself", "no", "nor", "not", "of",
17
+ "off", "on", "once", "only", "or", "other", "ought", "our", "ours", "ourselves",
18
18
  "out", "over", "own", "same", "shan't", "she", "she'd", "she'll", "she's",
19
- "should", "shouldn't", "some", "such", "than", "that", "that's", "the",
19
+ "should", "shouldn't", "so", "some", "such", "than", "that", "that's", "the",
20
20
  "their", "theirs", "them", "themselves", "then", "there", "there's", "these",
21
21
  "they", "they'd", "they'll", "they're", "they've", "this", "those", "through",
22
- "too", "under", "until", "up", "very", "was", "wasn't", "we'd",
22
+ "to", "too", "under", "until", "up", "very", "was", "wasn't", "we", "we'd",
23
23
  "we'll", "were", "we're", "weren't", "we've", "what", "what's", "when",
24
24
  "when's", "where", "where's", "which", "while", "who", "whom", "who's",
25
25
  "why", "why's", "with", "won't", "would", "wouldn't", "you", "you'd",
@@ -41,7 +41,7 @@ module Hunt
41
41
  squeeze(Separator).
42
42
  split(Separator).
43
43
  map { |word| word.downcase }.
44
- reject { |word| word.size < 3 }.
44
+ reject { |word| word.size < 2 }.
45
45
  reject { |word| WordsToIgnore.include?(word) }.
46
46
  map { |word| strip_puncuation(word) }.
47
47
  reject { |word| word.blank? }.
@@ -1,3 +1,3 @@
1
1
  module Hunt
2
- VERSION = "0.1"
2
+ VERSION = "0.2"
3
3
  end
@@ -40,8 +40,8 @@ describe Hunt::Util do
40
40
  Hunt::Util.to_words('Sweet First Sentence').should == %w(sweet first sentence)
41
41
  end
42
42
 
43
- it "removes any words under 3 characters" do
44
- Hunt::Util.to_words('my first sentence').should == %w(first sentence)
43
+ it "removes any words under 2 characters" do
44
+ Hunt::Util.to_words('a tv show').should == %w(tv show)
45
45
  end
46
46
 
47
47
  it "removes words that should be ignored" do
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hunt
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- version: "0.1"
8
+ - 2
9
+ version: "0.2"
10
10
  platform: ruby
11
11
  authors:
12
12
  - John Nunemaker
@@ -79,6 +79,8 @@ files:
79
79
  - LICENSE
80
80
  - README.rdoc
81
81
  - Rakefile
82
+ - examples/multiple.rb
83
+ - examples/single.rb
82
84
  - hunt.gemspec
83
85
  - lib/hunt.rb
84
86
  - lib/hunt/util.rb