melisa 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 108e4a649293c67d566fec46997d324648ead6fb
4
- data.tar.gz: 199fdaf673933b9b369f8a0886e52efdb43e7791
3
+ metadata.gz: 32a6c2d8935c0153a541ebd24149c48285bfeb3e
4
+ data.tar.gz: 9ca141c1b3236bb9a58a548aca80cff24a7512c9
5
5
  SHA512:
6
- metadata.gz: df5a3f3fcecdc2f2b95064bfa5b68c0a51e5d5caa01eac352d81ae9f0f1a12f2fd7773dc8922765693b3cc22ecd719bc64c646e40610c7056e280a831df9eaa7
7
- data.tar.gz: 181e0348caefccb4f0247903dc51ec7b2a8091d295a8f5e1399906c0ca2926018b49c8086b859259ed5b6865ff919e064c2f43fbaa06a5ff676c943577983194
6
+ metadata.gz: 4adbb03528aaeb653e2d7d8221d02ba57729adbb0802102106556207bf738f409cf8cf4639efc93d110b8ed244411f009d05e3a8fce177edfe4d0f2da1f4f071
7
+ data.tar.gz: bd4f6f669ee41b4f3e0539f435617a30788b69e9dc7b53f636527d70af8417decaf2eadfb43b6e18278c84ed900ca9320e56adef15d8b5f71ce74dd8dd404c39
@@ -11,9 +11,9 @@ module Melisa
11
11
  add_many(hash, [])
12
12
  end
13
13
 
14
- def add_many(hash, weights)
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
- build unless @built
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
- build unless @built
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)
@@ -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)) unless @built
31
- @built = true
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
- build unless @built
73
+ build_if_necessary
50
74
  Search.new(self, prefix)
51
75
  end
52
76
 
53
77
  def each(&block)
54
- build unless @built
78
+ build_if_necessary
55
79
  search('').each(&block)
56
80
  end
57
81
 
58
82
  def size
59
- build unless @built
83
+ build_if_necessary
60
84
  @trie.num_keys()
61
85
  end
62
86
 
63
87
  def keys
64
- build unless @built
88
+ build_if_necessary
65
89
  search('').keys
66
90
  end
67
91
 
68
92
  def has_keys?
69
- build unless @built
93
+ build_if_necessary
70
94
  search('').has_keys?
71
95
  end
72
96
 
73
97
  def include?(key)
74
- build unless @built
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
- build unless @built
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
@@ -1,3 +1,3 @@
1
1
  module Melisa
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: melisa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Duane Johnson