Class: TSS::Vertex

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

Overview

Trie vertex class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) {|_self| ... } ⇒ Vertex

Initializes new vertex

  • parent is parent TSS::Vertex

Example:

>> TSS::Vertex.new(@root_vertex)
>> TSS::Vertex.new(@root_vertex)

Optional arguments:

parent: (TSS::Vertex)

Yields:

  • (_self)

Yield Parameters:

  • _self (TSS::Vertex)

    the object that the method was called on



39
40
41
42
43
44
45
46
# File 'lib/tss/vertex.rb', line 39

def initialize(parent = nil)
  @char = nil
  @parent = parent
  @children = []
  @links = []
  @end_indexes = []
  yield(self) if block_given?
end

Instance Attribute Details

#charObject

Letter representing this vertex



29
30
31
# File 'lib/tss/vertex.rb', line 29

def char
  @char
end

#childrenObject (readonly)

Array of children TSS::Vertex references for nested models (full trie, Aho-Corasick trie) or as list of nested vertexes of root vertex of flat trie



18
19
20
# File 'lib/tss/vertex.rb', line 18

def children
  @children
end

#end_indexesObject

Array of indexes of word in dictionary Empty if it is intermediate TSS::Vertex in chain



26
27
28
# File 'lib/tss/vertex.rb', line 26

def end_indexes
  @end_indexes
end

Array of TSS::Vertex links for flat trie model, also used as suffixes of Aho-Corasick trie



22
23
24
# File 'lib/tss/vertex.rb', line 22

def links
  @links
end

#parentObject

Reference to the parent TSS::Vertex



13
14
15
# File 'lib/tss/vertex.rb', line 13

def parent
  @parent
end

#rootObject

Reference to the root vertex of the trie



10
11
12
# File 'lib/tss/vertex.rb', line 10

def root
  @root
end

Instance Method Details

#add_child(char, end_index) ⇒ Object

Initializes new TSS::Vertex and adds it to the parent attribute



50
51
52
53
54
55
56
57
58
# File 'lib/tss/vertex.rb', line 50

def add_child(char, end_index)
  child = get_child(char)
  if child
    child.end_indexes << end_index unless end_index.nil?
    child
  else
    init_subchild(char, end_index)
  end
end

Adds reference to the linked vertexes



68
69
70
# File 'lib/tss/vertex.rb', line 68

def add_link(vertex, end_index = nil)
  @links << TSS::Link.new(vertex, end_index)
end

#children_charsObject

Returns array of characters from array of children TSS::Vertex



80
81
82
# File 'lib/tss/vertex.rb', line 80

def children_chars
  @children.map(&:char)
end

#get_child(char) ⇒ Object

Returns child TSS::Vertex by letter, from children attribute



62
63
64
# File 'lib/tss/vertex.rb', line 62

def get_child(char)
  @children.find { |c| c.char == char }
end

Returns link by character



74
75
76
# File 'lib/tss/vertex.rb', line 74

def get_link(char)
  @links.find { |l| l.char == char }
end

#vertexObject



84
85
86
# File 'lib/tss/vertex.rb', line 84

def vertex
  self
end