Class: Glaemscribe::API::TranscriptionProcessor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode) ⇒ TranscriptionProcessor

Returns a new instance of TranscriptionProcessor.



30
31
32
33
# File 'lib/api/transcription_processor.rb', line 30

def initialize(mode)
  @mode         = mode
  @rule_groups  = {}
end

Instance Attribute Details

#modeObject (readonly)

Returns the value of attribute mode.



28
29
30
# File 'lib/api/transcription_processor.rb', line 28

def mode
  @mode
end

#rule_groupsObject (readonly)

Returns the value of attribute rule_groups.



27
28
29
# File 'lib/api/transcription_processor.rb', line 27

def rule_groups
  @rule_groups
end

Instance Method Details

#add_subrule(sub_rule) ⇒ Object



35
36
37
38
# File 'lib/api/transcription_processor.rb', line 35

def add_subrule(sub_rule)
  path = sub_rule.src_combination.join("")
  @transcription_tree.add_subpath(path, sub_rule.dst_combination)
end

#apply(l, debug_context) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/api/transcription_processor.rb', line 76

def apply(l, debug_context)
  ret = []
  current_group     = nil
  accumulated_word  = ""
 
  l.split("").each{ |c|
    case c
    when " ", "\t" 
      ret += transcribe_word(accumulated_word, debug_context)
      ret += ["*SPACE"]
      
      accumulated_word = ""
    when "\r"
      # Ignore
    when "\n"
      ret += transcribe_word(accumulated_word, debug_context)
      ret += ["*LF"]
      
      accumulated_word = ""
    else
      c_group = @in_charset[c]
      if c_group == current_group
        accumulated_word += c
      else
        ret += transcribe_word(accumulated_word, debug_context)
        current_group    = c_group
        accumulated_word = c
      end
    end            
  }
  # Just in case
  ret += transcribe_word(accumulated_word, debug_context)
  ret
end

#finalize(trans_options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/api/transcription_processor.rb', line 40

def finalize(trans_options)
  @errors = []
  
  @transcription_tree = TranscriptionTreeNode.new(nil,nil)
  
  # Add WORD_BOUNDARY and WORD_BREAKER in the tree
  @transcription_tree.add_subpath(WORD_BOUNDARY_TREE,   [""])
  @transcription_tree.add_subpath(WORD_BREAKER,         [""])
  
  rule_groups.each{ |rgname, rg| 
    rg.finalize(trans_options) 
  }
  
  # Build the input charset
  @in_charset = {}
  rule_groups.each{ |rgname, rg| 
    rg.in_charset.each{ |char, group|
      group_for_char = @in_charset[char]
      if group_for_char
        mode.errors << Glaeml::Error.new(-1,"Group #{rgname} uses input character #{char} which is also used by group #{group_for_char.name}. Input charsets should not intersect between groups.") 
      else
        @in_charset[char] = group
      end
    }
  }
  
  # Build the transcription tree
  rule_groups.each{ |rgname, rg|
    rg.rules.each { |r|
      r.sub_rules.each{ |sr|      
        add_subrule(sr)
      }
    }
  }        
end

#transcribe_word(word, debug_context) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/api/transcription_processor.rb', line 111

def transcribe_word(word, debug_context)
  res = []
  word = WORD_BOUNDARY_TREE + word + WORD_BOUNDARY_TREE
  while word.length != 0
    tokens, len = @transcription_tree.transcribe(word)       
    word        = word[len..-1]
    eaten       = word[0..len-1]
    res         += tokens
    
    debug_context.processor_pathes << [eaten, tokens, tokens]
  end
  # Return token list
  res
end