komainu 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,11 +26,20 @@ module Komainu
26
26
  def calculate_suggestion(query)
27
27
  words = split_into_words(@data_to_search.map { |searchable| searchable.text }.join(" "))
28
28
  levenshtein = Levenshtein.new(words)
29
+
30
+ found_suggestion = false
29
31
  suggestion = split_into_words(query).map do |word|
30
32
  matches = levenshtein.search(word, 2)
31
- matches.keys.first || word
33
+
34
+ if matches.empty? != true && matches.keys.first != word
35
+ found_suggestion = true
36
+ matches.sort {|a,b| a[1] <=> b[1]}.first.first
37
+ else
38
+ word
39
+ end
32
40
  end
33
- suggestion.join(" ")
41
+
42
+ suggestion.join(" ") if found_suggestion
34
43
  end
35
44
 
36
45
  def split_into_words(string)
@@ -1,3 +1,3 @@
1
1
  module Komainu
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -8,7 +8,7 @@ module Komainu
8
8
  let :data_to_search do
9
9
  item1 = OpenStruct.new
10
10
  item1.name = :item1
11
- item1.text = "This is some text"
11
+ item1.text = "This is some text."
12
12
  item2 = OpenStruct.new
13
13
  item2.name = :item2
14
14
  item2.text = "Batman has no parents"
@@ -37,5 +37,17 @@ module Komainu
37
37
  result = subject.search("btman")
38
38
  result.suggestion.must_equal "Batman"
39
39
  end
40
+
41
+ it "only suggests an alternate query if the alternate query is different" do
42
+ result = subject.search("Batman")
43
+ result.suggestion.must_be_nil
44
+ end
45
+
46
+ it "finds the best suggestion" do
47
+ item = OpenStruct.new
48
+ item.text = "helloo hellp"
49
+ result = SearchesText.new([item]).search("hell")
50
+ result.suggestion.must_equal "hellp"
51
+ end
40
52
  end
41
53
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: komainu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-22 00:00:00.000000000Z
12
+ date: 2011-11-23 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &70219264697540 !ruby/object:Gem::Requirement
16
+ requirement: &70315498054880 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70219264697540
24
+ version_requirements: *70315498054880
25
25
  description: ''
26
26
  email:
27
27
  - andrew.vos@gmail.com