Class: Glaemscribe::API::TranscriptionTreeNode

Inherits:
Object
  • Object
show all
Defined in:
lib/api/transcription_tree_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(character, replacement) ⇒ TranscriptionTreeNode

Returns a new instance of TranscriptionTreeNode.



28
29
30
31
32
# File 'lib/api/transcription_tree_node.rb', line 28

def initialize(character, replacement)
  @character    = character
  @replacement  = replacement
  @siblings     = {}
end

Instance Attribute Details

#characterObject

Returns the value of attribute character.



26
27
28
# File 'lib/api/transcription_tree_node.rb', line 26

def character
  @character
end

#replacementObject

Returns the value of attribute replacement.



26
27
28
# File 'lib/api/transcription_tree_node.rb', line 26

def replacement
  @replacement
end

#siblingsObject

Returns the value of attribute siblings.



26
27
28
# File 'lib/api/transcription_tree_node.rb', line 26

def siblings
  @siblings
end

Instance Method Details

#_pObject



34
35
36
37
38
39
# File 'lib/api/transcription_tree_node.rb', line 34

def _p
  puts "Node has #{@siblings.keys.count} siblings."
  @siblings.each{ |k,v|
    puts "#{k}, effective: #{v.effective?}"
  }
end

#_pchain(chain) ⇒ Object



41
42
43
# File 'lib/api/transcription_tree_node.rb', line 41

def _pchain(chain)
  "[" + chain.map{|node| node.character||"ROOT"}.join(", ") + "]"
end

#add_subpath(source, rep) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/api/transcription_tree_node.rb', line 49

def add_subpath(source, rep)
  return if source.nil? || source.empty?
  cc = source[0..0]

  sibling       = @siblings[cc]
  sibling       = TranscriptionTreeNode.new(cc, nil) if !sibling
  @siblings[cc] = sibling
    
  if source.length == 1 
    # Sibling is effective
    sibling.replacement = rep
  else
    sibling.add_subpath(source[1..-1], rep)
  end
end

#effective?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/api/transcription_tree_node.rb', line 45

def effective?
  !@replacement.nil?
end

#transcribe(string, chain = []) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/api/transcription_tree_node.rb', line 65

def transcribe(string, chain=[])
 
  chain << self
 
  if !string.empty?
    cc = string[0..0]
    sibling = @siblings[cc]

    if sibling    
      return sibling.transcribe(string[1..-1], chain)
    end # Else we are at the end
  end # Else we are at the end

  # puts "End of chain: #{chain.count}, #{_pchain(chain)}"

  # We are at the end of the chain
  while chain.count > 1
    last_node = chain.pop
    return last_node.replacement, chain.count if last_node.effective?
  end

  # Only the root node is in the chain, we could not find anything; return the "unknown char"
  return ["*UNKNOWN"], 1    
end