Class: TSS::Trie

Inherits:
Object
  • Object
show all
Defined in:
lib/tss/trie.rb

Overview

Main class for creating Trie Substring Search from array of words of dictionary

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dictionary, type = :full) ⇒ Trie

Initialize new trie and fill it with words from dictionary



27
28
29
30
31
32
# File 'lib/tss/trie.rb', line 27

def initialize(dictionary, type = :full)
  switch_trie_type(type)
  @root = TSS::Vertex.new
  @dictionary = dictionary
  build_trie
end

Instance Attribute Details

#dictionaryObject (readonly)

Dictionary attribute



14
15
16
# File 'lib/tss/trie.rb', line 14

def dictionary
  @dictionary
end

#rootObject (readonly)

Root vertex



11
12
13
# File 'lib/tss/trie.rb', line 11

def root
  @root
end

#trieObject (readonly)

Trie attribute



17
18
19
# File 'lib/tss/trie.rb', line 17

def trie
  @trie
end

#trie_classObject (readonly)

Trie class reference switched by type



20
21
22
# File 'lib/tss/trie.rb', line 20

def trie_class
  @trie_class
end

#trie_instanceObject (readonly)

Trie class instance



23
24
25
# File 'lib/tss/trie.rb', line 23

def trie_instance
  @trie_instance
end

Instance Method Details

#backtrace_to_word(vertex) ⇒ Object

Returns hash with word and indexes at dictionary

  • Ending vertex of chain should be used as argument, it means that it should contain at least one value in the array of end_indexes attribute



45
46
47
# File 'lib/tss/trie.rb', line 45

def backtrace_to_word(vertex)
  @trie_instance.backtrace_to_word(vertex)
end

#extend_dictionary(dict) ⇒ Object

Adds additional words(chains of vertexes) to the trie object

  • Argument should be array of words

Example:

>> tss.extend_dictionary(["our", "it", "them"])


54
55
56
# File 'lib/tss/trie.rb', line 54

def extend_dictionary(dict)
  @trie_instance.extend_dictionary(dict)
end

#parse(text) ⇒ Object

Executes text analyzis and returns map occurring words with indexes from dictionary



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

def parse(text)
  @trie_instance.parse(text)
end