rifle 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rifle.rb +24 -23
- data/lib/rifle/settings.rb +5 -1
- data/lib/rifle/version.rb +1 -1
- metadata +2 -2
data/lib/rifle.rb
CHANGED
@@ -21,22 +21,22 @@ module Rifle
|
|
21
21
|
# First get the old values
|
22
22
|
old_payload = get_payload_for_urn(urn)
|
23
23
|
if old_payload
|
24
|
-
old_words =
|
25
|
-
old_metaphones =
|
24
|
+
old_words = traverse_object_for_word_set(old_payload)
|
25
|
+
old_metaphones = get_metaphones_from_word_set(old_words)
|
26
26
|
else
|
27
27
|
old_metaphones = []
|
28
28
|
end
|
29
29
|
|
30
30
|
# Now get the new ones
|
31
|
-
words =
|
32
|
-
metaphones =
|
31
|
+
words = traverse_object_for_word_set(hash)
|
32
|
+
metaphones = get_metaphones_from_word_set(words)
|
33
33
|
|
34
|
-
# Clear out words that have been removed
|
34
|
+
# Clear out words that have been removed (but leave ones that are still present in the new version)
|
35
35
|
(old_metaphones - metaphones).each do |metaphone|
|
36
36
|
remove_urn_from_metaphone_set(urn, metaphone)
|
37
37
|
end
|
38
38
|
|
39
|
-
# And add the new ones
|
39
|
+
# And add the new ones (but don't bother with words that were in the old version or ignored words)
|
40
40
|
(metaphones - old_metaphones).each do |metaphone|
|
41
41
|
add_urn_to_metaphone_set(urn, metaphone)
|
42
42
|
end
|
@@ -47,8 +47,8 @@ module Rifle
|
|
47
47
|
end
|
48
48
|
|
49
49
|
def search_for(sentence, urns_only)
|
50
|
-
words =
|
51
|
-
metaphones =
|
50
|
+
words = get_words_array_from_text(sentence)
|
51
|
+
metaphones = get_metaphones_from_word_set(Set.new(words))
|
52
52
|
urns = Set.new
|
53
53
|
metaphones.each do |metaphone|
|
54
54
|
new_urns = get_urns_for_metaphone(metaphone)
|
@@ -66,39 +66,40 @@ module Rifle
|
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
|
-
|
70
|
-
|
71
|
-
def remove_old(urn)
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
def traverse_object(input, words = nil)
|
76
|
-
words ||= Set.new
|
69
|
+
def traverse_object_for_word_set(input)
|
70
|
+
words = Set.new
|
77
71
|
examine_value(input, words)
|
78
72
|
words
|
79
73
|
end
|
80
74
|
|
81
75
|
def examine_value(value, words)
|
82
76
|
if value.is_a? Hash
|
83
|
-
value.each do |k
|
84
|
-
examine_value(
|
77
|
+
(value.keys - Rifle.settings.ignored_keys).each do |k|
|
78
|
+
examine_value(value[k], words)
|
85
79
|
end
|
86
80
|
elsif value.is_a? Array
|
87
81
|
value.each do |a|
|
88
82
|
examine_value(a, words)
|
89
83
|
end
|
90
84
|
else
|
91
|
-
words.
|
85
|
+
words.merge(get_words_array_from_text(value))
|
92
86
|
end
|
93
87
|
end
|
94
88
|
|
95
|
-
def
|
89
|
+
def get_words_array_from_text(text)
|
96
90
|
return [] if !text.is_a?(String)
|
97
|
-
words = text.downcase.split(/[^a-zA-
|
98
|
-
return words
|
91
|
+
words = text.downcase.split(/[^a-zA-Z0-9]/).select { |w| w.length >= Rifle.settings.min_word_length }
|
92
|
+
return words
|
99
93
|
end
|
100
94
|
|
101
|
-
def
|
95
|
+
def get_metaphones_from_word_set(words)
|
96
|
+
# Get just text words (no numbers)
|
97
|
+
words.keep_if { |w|
|
98
|
+
w.to_f == 0
|
99
|
+
}
|
100
|
+
# Removed ignored words
|
101
|
+
words.subtract Rifle.settings.ignored_words
|
102
|
+
# Get metaphones
|
102
103
|
::Text::Metaphone.metaphone(words.to_a.join(' ')).split(' ')
|
103
104
|
end
|
104
105
|
|
data/lib/rifle/settings.rb
CHANGED
@@ -1,12 +1,16 @@
|
|
1
1
|
module Rifle
|
2
2
|
|
3
3
|
class Settings
|
4
|
-
attr_accessor :ignored_words, :min_word_length, :redis, :use_rest_server, :resque_queue
|
4
|
+
attr_accessor :ignored_words, :ignored_keys, :min_word_length, :redis, :use_rest_server, :resque_queue
|
5
5
|
|
6
6
|
def ignored_words
|
7
7
|
@ignored_words ||= ["the", "and", "you", "that"]
|
8
8
|
end
|
9
9
|
|
10
|
+
def ignored_keys
|
11
|
+
@ignored_keys ||= [:created_at, :updated_at]
|
12
|
+
end
|
13
|
+
|
10
14
|
def min_word_length
|
11
15
|
@min_word_length ||= 3
|
12
16
|
end
|
data/lib/rifle/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rifle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|