melisa 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/melisa/bytes_trie.rb +4 -6
- data/lib/melisa/trie.rb +34 -10
- data/lib/melisa/version.rb +1 -1
- data/spec/trie_spec.rb +15 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 32a6c2d8935c0153a541ebd24149c48285bfeb3e
|
4
|
+
data.tar.gz: 9ca141c1b3236bb9a58a548aca80cff24a7512c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4adbb03528aaeb653e2d7d8221d02ba57729adbb0802102106556207bf738f409cf8cf4639efc93d110b8ed244411f009d05e3a8fce177edfe4d0f2da1f4f071
|
7
|
+
data.tar.gz: bd4f6f669ee41b4f3e0539f435617a30788b69e9dc7b53f636527d70af8417decaf2eadfb43b6e18278c84ed900ca9320e56adef15d8b5f71ce74dd8dd404c39
|
data/lib/melisa/bytes_trie.rb
CHANGED
@@ -11,9 +11,9 @@ module Melisa
|
|
11
11
|
add_many(hash, [])
|
12
12
|
end
|
13
13
|
|
14
|
-
def add_many(hash,
|
14
|
+
def add_many(hash, weight=nil)
|
15
15
|
for key, value in hash
|
16
|
-
push(raw_key(key, value))
|
16
|
+
push(raw_key(key, value), weight)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
@@ -22,8 +22,7 @@ module Melisa
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def get(key)
|
25
|
-
|
26
|
-
agent = Marisa::Agent.new
|
25
|
+
build_if_necessary
|
27
26
|
agent.set_query(key + @sep)
|
28
27
|
if @trie.predictive_search(agent)
|
29
28
|
agent_key_value(agent)
|
@@ -38,8 +37,7 @@ module Melisa
|
|
38
37
|
|
39
38
|
# Search for many results with a given prefix
|
40
39
|
def get_all(key)
|
41
|
-
|
42
|
-
agent = Marisa::Agent.new
|
40
|
+
build_if_necessary
|
43
41
|
agent.set_query(key)
|
44
42
|
[].tap do |results|
|
45
43
|
while @trie.predictive_search(agent)
|
data/lib/melisa/trie.rb
CHANGED
@@ -26,9 +26,19 @@ module Melisa
|
|
26
26
|
add_many(keys, weights)
|
27
27
|
end
|
28
28
|
|
29
|
+
def agent
|
30
|
+
@agent ||= Marisa::Agent.new
|
31
|
+
end
|
32
|
+
|
29
33
|
def build
|
30
|
-
@trie.build(@keyset, config_flags(@options))
|
31
|
-
|
34
|
+
@trie.build(@keyset, config_flags(@options))
|
35
|
+
end
|
36
|
+
|
37
|
+
def build_if_necessary
|
38
|
+
unless @built
|
39
|
+
build
|
40
|
+
@built = true
|
41
|
+
end
|
32
42
|
end
|
33
43
|
|
34
44
|
# Note: weight is not the same thing as a value! use a BytesTrie
|
@@ -45,33 +55,47 @@ module Melisa
|
|
45
55
|
end
|
46
56
|
end
|
47
57
|
|
58
|
+
def get_id(key)
|
59
|
+
build_if_necessary
|
60
|
+
agent.set_query(key)
|
61
|
+
trie.lookup(agent)
|
62
|
+
agent.key_id if agent.key_str
|
63
|
+
end
|
64
|
+
|
65
|
+
def get_key(id)
|
66
|
+
build_if_necessary
|
67
|
+
agent.set_query(id)
|
68
|
+
trie.reverse_lookup(agent)
|
69
|
+
agent.key_str
|
70
|
+
end
|
71
|
+
|
48
72
|
def search(prefix)
|
49
|
-
|
73
|
+
build_if_necessary
|
50
74
|
Search.new(self, prefix)
|
51
75
|
end
|
52
76
|
|
53
77
|
def each(&block)
|
54
|
-
|
78
|
+
build_if_necessary
|
55
79
|
search('').each(&block)
|
56
80
|
end
|
57
81
|
|
58
82
|
def size
|
59
|
-
|
83
|
+
build_if_necessary
|
60
84
|
@trie.num_keys()
|
61
85
|
end
|
62
86
|
|
63
87
|
def keys
|
64
|
-
|
88
|
+
build_if_necessary
|
65
89
|
search('').keys
|
66
90
|
end
|
67
91
|
|
68
92
|
def has_keys?
|
69
|
-
|
93
|
+
build_if_necessary
|
70
94
|
search('').has_keys?
|
71
95
|
end
|
72
96
|
|
73
97
|
def include?(key)
|
74
|
-
|
98
|
+
build_if_necessary
|
75
99
|
search('').include?(key)
|
76
100
|
end
|
77
101
|
|
@@ -80,7 +104,7 @@ module Melisa
|
|
80
104
|
end
|
81
105
|
|
82
106
|
def save(path)
|
83
|
-
|
107
|
+
build_if_necessary
|
84
108
|
self.tap { @trie.save(path) }
|
85
109
|
end
|
86
110
|
|
@@ -89,7 +113,7 @@ module Melisa
|
|
89
113
|
|
90
114
|
def push(key, weight=nil)
|
91
115
|
if weight
|
92
|
-
@keyset.push_back(key, weight
|
116
|
+
@keyset.push_back(key) #, weight) # For now, ignore weight due to marisa C++ binding issue
|
93
117
|
else
|
94
118
|
@keyset.push_back(key)
|
95
119
|
end
|
data/lib/melisa/version.rb
CHANGED
data/spec/trie_spec.rb
CHANGED
@@ -27,4 +27,19 @@ describe Melisa::Trie do
|
|
27
27
|
|
28
28
|
expect(trie2.keys).to match_array ['one', 'two', 'onetwo']
|
29
29
|
end
|
30
|
+
|
31
|
+
it "gets a key's integer ID" do
|
32
|
+
expect(trie.get_id('NOT_KEY1')).to be_nil
|
33
|
+
expect(trie.get_id('NOT_KEY2')).to be_nil
|
34
|
+
expect(trie.get_id('one')).to eq 0
|
35
|
+
expect(trie.get_id('two')).to eq 1
|
36
|
+
expect(trie.get_id('onetwo')).to eq 2
|
37
|
+
end
|
38
|
+
|
39
|
+
it "gets a key given an ID" do
|
40
|
+
expect(trie.get_key(0)).to eq 'one'
|
41
|
+
expect(trie.get_key(1)).to eq 'two'
|
42
|
+
expect(trie.get_key(2)).to eq 'onetwo'
|
43
|
+
expect{ trie.get_key(3) }.to raise_error
|
44
|
+
end
|
30
45
|
end
|