Class: Glaemscribe::API::TranscriptionTreeNode
- Inherits:
- 
      Object
      
        - Object
- Glaemscribe::API::TranscriptionTreeNode
 
- Defined in:
- lib/api/transcription_tree_node.rb
Instance Attribute Summary collapse
- 
  
    
      #character  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute character. 
- 
  
    
      #replacement  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute replacement. 
- 
  
    
      #siblings  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute siblings. 
Instance Method Summary collapse
- #_p ⇒ Object
- #_pchain(chain) ⇒ Object
- #add_subpath(source, rep) ⇒ Object
- #effective? ⇒ Boolean
- 
  
    
      #initialize(character, replacement)  ⇒ TranscriptionTreeNode 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of TranscriptionTreeNode. 
- #transcribe(string, chain = []) ⇒ Object
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
#character ⇒ Object
Returns the value of attribute character.
| 26 27 28 | # File 'lib/api/transcription_tree_node.rb', line 26 def character @character end | 
#replacement ⇒ Object
Returns the value of attribute replacement.
| 26 27 28 | # File 'lib/api/transcription_tree_node.rb', line 26 def replacement @replacement end | 
#siblings ⇒ Object
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
#_p ⇒ Object
| 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
| 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_CHAR_OUTPUT], 1 end |