Class: Melisa::Trie

Inherits:
Object
  • Object
show all
Includes:
Enumerable, BaseConfigFlags
Defined in:
lib/melisa/trie.rb

Direct Known Subclasses

BytesTrie

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BaseConfigFlags

#binary_flag, #config_flags, #lookup_cache_size, #valid_node_order, #valid_num_tries

Constructor Details

#initialize(keys = [], weights = [], opts = {}) ⇒ Trie

Initialize a BaseTrie.



20
21
22
23
24
25
26
27
# File 'lib/melisa/trie.rb', line 20

def initialize(keys=[], weights=[], opts={})
  @trie = Marisa::Trie.new
  @keyset = Marisa::Keyset.new
  @options = opts
  @built = false

  add_many(keys, weights)
end

Instance Attribute Details

#trieObject (readonly)

Returns the value of attribute trie.



10
11
12
# File 'lib/melisa/trie.rb', line 10

def trie
  @trie
end

Instance Method Details

#add(key, weight = nil) ⇒ Object Also known as: <<

Note: weight is not the same thing as a value! use a BytesTrie or IntTrie subclass if you want a key/value dictionary

Raises:



48
49
50
51
# File 'lib/melisa/trie.rb', line 48

def add(key, weight=nil)
  raise ImmutableError, "Can't add #{key}, Trie already built" if @built
  self.tap { push(key, weight) }
end

#add_many(keys, weights) ⇒ Object

Raises:



54
55
56
57
58
59
# File 'lib/melisa/trie.rb', line 54

def add_many(keys, weights)
  raise ImmutableError, "Can't add keys, Trie already built" if @built
  for key, weight in keys.zip(weights)
    push(key, weight)
  end
end

#agentObject



29
30
31
# File 'lib/melisa/trie.rb', line 29

def agent
  @agent ||= Marisa::Agent.new
end

#buildObject



37
38
39
40
# File 'lib/melisa/trie.rb', line 37

def build
  @trie.build(@keyset, config_flags(@options))
  @built = true
end

#build_if_necessaryObject



42
43
44
# File 'lib/melisa/trie.rb', line 42

def build_if_necessary
  build unless built?
end

#built?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/melisa/trie.rb', line 33

def built?
  @built
end

#each(&block) ⇒ Object



87
88
89
90
# File 'lib/melisa/trie.rb', line 87

def each(&block)
  build_if_necessary
  search('').each(&block)
end

#get_id(key) ⇒ Object



61
62
63
64
65
66
# File 'lib/melisa/trie.rb', line 61

def get_id(key)
  build_if_necessary
  agent.set_query(key)
  trie.lookup(agent)
  agent.key_id if agent.key_str
end

#get_key(id) ⇒ Object



75
76
77
78
79
80
# File 'lib/melisa/trie.rb', line 75

def get_key(id)
  build_if_necessary
  agent.set_query(id)
  trie.reverse_lookup(agent)
  agent.key_str
end

#get_weight(key) ⇒ Object



68
69
70
71
72
73
# File 'lib/melisa/trie.rb', line 68

def get_weight(key)
  build_if_necessary
  agent.set_query(key)
  trie.lookup(agent)
  agent.key.weight if agent.key_str
end

#has_keys?Boolean

Returns:

  • (Boolean)


105
106
107
108
# File 'lib/melisa/trie.rb', line 105

def has_keys?
  build_if_necessary
  search('').has_keys?
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
# File 'lib/melisa/trie.rb', line 110

def include?(key)
  build_if_necessary
  search('').include?(key)
end

#keysObject



100
101
102
103
# File 'lib/melisa/trie.rb', line 100

def keys
  build_if_necessary
  search('').keys
end

#load(path) ⇒ Object



115
116
117
# File 'lib/melisa/trie.rb', line 115

def load(path)
  self.tap { @trie.load(path); @built = true }
end

#save(path) ⇒ Object



119
120
121
122
# File 'lib/melisa/trie.rb', line 119

def save(path)
  build_if_necessary
  self.tap { @trie.save(path) }
end

#search(prefix = '') ⇒ Object



82
83
84
85
# File 'lib/melisa/trie.rb', line 82

def search(prefix='')
  build_if_necessary
  Search.new(self, prefix)
end

#sizeObject



92
93
94
95
96
97
98
# File 'lib/melisa/trie.rb', line 92

def size
  if @built
    @trie.num_keys()
  else
    @keyset.num_keys()
  end
end