class Glaemscribe::API::TranscriptionTreeNode

Attributes

character[RW]
replacement[RW]
siblings[RW]

Public Class Methods

new(character, replacement) click to toggle source
# File lib/api/transcription_tree_node.rb, line 28
def initialize(character, replacement)
  @character    = character
  @replacement  = replacement
  @siblings     = {}
end

Public Instance Methods

_p() click to toggle source
# 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) click to toggle source
# File lib/api/transcription_tree_node.rb, line 41
def _pchain(chain)
  "[" + chain.map{|node| node.character||"ROOT"}.join(", ") + "]"
end
add_subpath(source, rep) click to toggle source
# 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?() click to toggle source
# File lib/api/transcription_tree_node.rb, line 45
def effective?
  !@replacement.nil?
end
transcribe(string, chain=[]) click to toggle source
# 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