class Glaemscribe::API::Mode

Attributes

authors[RW]
current_tts_voice[R]
default_charset[RW]
errors[RW]
has_tts[RW]
human_name[RW]
invention[RW]
language[RW]
latest_option_values[R]
name[RW]
options[RW]
post_processor[RW]
pre_processor[RW]
processor[RW]
raw_mode_name[RW]
supported_charsets[RW]
version[RW]
warnings[RW]
world[RW]
writing[RW]

Public Class Methods

new(name) click to toggle source
# File lib/api/mode.rb, line 51
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

Public Instance Methods

finalize(options={}) click to toggle source
# File lib/api/mode.rb, line 74
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
inspect() click to toggle source
# File lib/api/mode.rb, line 70
def inspect
  to_s
end
raw_mode() click to toggle source
# File lib/api/mode.rb, line 137
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 = nil) click to toggle source
# File lib/api/mode.rb, line 144
def strict_transcribe(content, charset = nil)
  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']
    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)
    l = @processor.apply(l)
    l = @post_processor.apply(l, charset)
    l += "\n" if restore_lf
    l
  }.join
  return true, ret
end
to_s() click to toggle source
# File lib/api/mode.rb, line 66
def to_s
  " <Mode #{name}: Language '#{language}', Writing '#{writing}', Human Name '#{human_name}', Authors '#{authors}', Version '#{version}'> "
end
transcribe(content, charset = nil) click to toggle source
# File lib/api/mode.rb, line 172
def transcribe(content, charset = nil)
  if raw_mode
    chunks = content.split(/({{.*?}})/m)
    ret = ''
    res = true
    chunks.each{ |c|
      if c =~ /{{(.*?)}}/m
        succ, r = raw_mode.strict_transcribe($1,charset)

        if !succ
          return false, r # Propagate error
        end

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

        if !succ
          return false, r # Propagate error
        end

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