Class: Glaemscribe::API::Mode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Mode

Returns a new instance of Mode.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/api/mode.rb', line 66

def initialize(name)
  @name               = name
  @errors             = []
  @warnings           = []
  @supported_charsets = {}
  @options            = {}
  @last_raw_options   = nil
  @has_tts            = false
  @current_tts_voice  = nil
 
  @pre_processor    = TranscriptionPreProcessor.new(self)
  @processor        = TranscriptionProcessor.new(self)
  @post_processor   = TranscriptionPostProcessor.new(self)
end

Instance Attribute Details

#authorsObject

Returns the value of attribute authors.



49
50
51
# File 'lib/api/mode.rb', line 49

def authors
  @authors
end

#current_tts_voiceObject (readonly)

Returns the value of attribute current_tts_voice.



62
63
64
# File 'lib/api/mode.rb', line 62

def current_tts_voice
  @current_tts_voice
end

#default_charsetObject

Returns the value of attribute default_charset.



55
56
57
# File 'lib/api/mode.rb', line 55

def default_charset
  @default_charset
end

#errorsObject

Returns the value of attribute errors.



44
45
46
# File 'lib/api/mode.rb', line 44

def errors
  @errors
end

#has_ttsObject

Returns the value of attribute has_tts.



61
62
63
# File 'lib/api/mode.rb', line 61

def has_tts
  @has_tts
end

#human_nameObject

Returns the value of attribute human_name.



49
50
51
# File 'lib/api/mode.rb', line 49

def human_name
  @human_name
end

#inventionObject

Returns the value of attribute invention.



59
60
61
# File 'lib/api/mode.rb', line 59

def invention
  @invention
end

#languageObject

Returns the value of attribute language.



49
50
51
# File 'lib/api/mode.rb', line 49

def language
  @language
end

#latest_option_valuesObject (readonly)

Returns the value of attribute latest_option_values.



64
65
66
# File 'lib/api/mode.rb', line 64

def latest_option_values
  @latest_option_values
end

#nameObject

Returns the value of attribute name.



47
48
49
# File 'lib/api/mode.rb', line 47

def name
  @name
end

#optionsObject

Returns the value of attribute options.



52
53
54
# File 'lib/api/mode.rb', line 52

def options
  @options
end

#post_processorObject

Returns the value of attribute post_processor.



50
51
52
# File 'lib/api/mode.rb', line 50

def post_processor
  @post_processor
end

#pre_processorObject

Returns the value of attribute pre_processor.



50
51
52
# File 'lib/api/mode.rb', line 50

def pre_processor
  @pre_processor
end

#processorObject

Returns the value of attribute processor.



50
51
52
# File 'lib/api/mode.rb', line 50

def processor
  @processor
end

#raw_mode_nameObject

Read from glaeml



57
58
59
# File 'lib/api/mode.rb', line 57

def raw_mode_name
  @raw_mode_name
end

#supported_charsetsObject

Returns the value of attribute supported_charsets.



54
55
56
# File 'lib/api/mode.rb', line 54

def supported_charsets
  @supported_charsets
end

#versionObject

Returns the value of attribute version.



49
50
51
# File 'lib/api/mode.rb', line 49

def version
  @version
end

#warningsObject

Returns the value of attribute warnings.



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

def warnings
  @warnings
end

#worldObject

Returns the value of attribute world.



59
60
61
# File 'lib/api/mode.rb', line 59

def world
  @world
end

#writingObject

Returns the value of attribute writing.



49
50
51
# File 'lib/api/mode.rb', line 49

def writing
  @writing
end

Instance Method Details

#finalize(options = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/api/mode.rb', line 89

def finalize(options={})
        
  if options == @last_raw_options
    # Small optimization : don't refinalize if options are the same as before
    return
  end      
        
  @last_raw_options = options      
        
  # Hash: option_name => value_name
  trans_options = {}
  
  # Build default options
  @options.each { |oname, o|
    trans_options[oname] = o.default_value_name
  }   

  # Push user options
  options.each { |oname, valname|
    # Check if option exists
    opt = @options[oname]
    next if(!opt)
      
    val = opt.value_for_value_name(valname)
    next if val == nil
      
    trans_options[oname] = valname
  }
    
  trans_options_converted = {}

  # Do a conversion from names to values space
  trans_options.each{ |oname,valname| 
    trans_options_converted[oname] = @options[oname].value_for_value_name(valname)
  }

  # Add the option defined constants to the whole list for evaluation purposes
  @options.each { |oname, o|
    # For enums, add the values as constants for the evaluator
    if(o.type == Option::Type::ENUM)
      o.values.each{ |name, val|
        trans_options_converted[name] = val
      }
    end
  }   
  
  @latest_option_values = trans_options_converted
     
  @pre_processor.finalize(@latest_option_values)
  @post_processor.finalize(@latest_option_values)
  @processor.finalize(@latest_option_values)
    
  raw_mode.finalize options if raw_mode
  
  # Update the current espeak voice
  if @has_tts
    espeak_option       = @options['espeak_voice'].value_name_for_value(@latest_option_values['espeak_voice'])
    @current_tts_voice  = TTS.option_name_to_voice(espeak_option)
  end
  
  self
end

#inspectObject



85
86
87
# File 'lib/api/mode.rb', line 85

def inspect
  to_s
end

#raw_modeObject



152
153
154
155
156
157
# File 'lib/api/mode.rb', line 152

def raw_mode
  return @raw_mode if @raw_mode
  loaded_raw_mode = (@raw_mode_name && Glaemscribe::API::ResourceManager.loaded_modes[@raw_mode_name])    
  return nil if !loaded_raw_mode
  @raw_mode = loaded_raw_mode.deep_clone
end

#strict_transcribe(content, charset, debug_context) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/api/mode.rb', line 159

def strict_transcribe(content, charset, debug_context)
  charset = default_charset if !charset
  return false, "*** No charset usable for transcription. Failed!" if !charset
  
  if has_tts
    begin
      content = TTS.ipa(content, @current_tts_voice, (raw_mode != nil) )['ipa']
      debug_context.tts_output += content
    rescue StandardError => e
      return false, "TTS pre-transcription failed : #{e}."
    end
  end
  
  # Parser works line by line
  ret = content.lines.map{ |l| 
    restore_lf = false
    if l[-1] == "\n"
      l[-1] = "" 
      restore_lf = true
    end
    
    l = @pre_processor.apply(l)
    debug_context.preprocessor_output += l + "\n"
 
    l = @processor.apply(l, debug_context)
    debug_context.processor_output += l

    l = @post_processor.apply(l, charset)
    debug_context.postprocessor_output += l + "\n"

    l += "\n" if restore_lf
    l
  }.join
  return true, ret
end

#to_sObject



81
82
83
# File 'lib/api/mode.rb', line 81

def to_s
  " <Mode #{name}: Language '#{language}', Writing '#{writing}', Human Name '#{human_name}', Authors '#{authors}', Version '#{version}'> "
end

#transcribe(content, charset = nil) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/api/mode.rb', line 195

def transcribe(content, charset = nil)
  debug_context = ModeDebugContext.new
  if raw_mode
    chunks = content.split(/({{.*?}})/m)
    ret = ''
    res = true
    chunks.each{ |c|
      if c =~ /{{(.*?)}}/m
        succ, r = raw_mode.strict_transcribe($1, charset, debug_context)

        if !succ
          return false, r, debug_context # Propagate error
        end

        ret += r
      else
        succ, r = strict_transcribe(c,charset,debug_context)

        if !succ
          return false, r, debug_context # Propagate error 
        end

        ret += r
      end
    }
    return res, ret, debug_context
  else
    succ, r = strict_transcribe(content, charset, debug_context)
    return succ, r, debug_context
  end
end