class Dictation::ControlCenter

Public Class Methods

launch() click to toggle source
# File lib/dictation/control_center.rb, line 4
def launch
  if OperatingSystem.supported?
    command = ARGV[0]
    options = Commander.order(ARGV)
    case command
    when 'new'
      execute_new(options)
    when 'add'
      execute_add(options)
    when 'dictate'
      execute_dictate(options)
    when 'verify'
      execute_verify(options)
    end
  else
    puts 'Sorry, your Operating System is not supported.  Only Mac OSX has built-in TTS (Text-to-Speech).'
  end
end

Private Class Methods

execute_add(options) click to toggle source
# File lib/dictation/control_center.rb, line 31
def execute_add(options)
  if options[:language]
    dictionary = get_dictionary(options[:language])
    if dictionary
      words = Typewriter.new.collect
      Serializer.serialize(words, dictionary)
    else
      puts 'Can not find any dictionary file.'
    end
  else
    puts 'Please specify the target language (-l).'
  end
end
execute_dictate(options) click to toggle source
# File lib/dictation/control_center.rb, line 45
def execute_dictate(options)
  execute_speak(options, :dictates)
end
execute_new(options) click to toggle source
# File lib/dictation/control_center.rb, line 23
def execute_new(options)
  if options[:dictate] && options[:verify]
    File.open("dict_#{options[:dictate]}_#{options[:verify]}.txt", "w") {}
  else
    puts 'Please give both dictate (-d) and verify (-v) languages.'
  end
end
execute_speak(options, action) click to toggle source
# File lib/dictation/control_center.rb, line 103
def execute_speak(options, action)
  if options.has_key?(:language)
    words = get_words(options)
    dictionary = get_dictionary(options[:language])
    if dictionary
      teacher = set_teacher(dictionary)
      teacher.send(action, words)
    else
      puts 'Please check if the given language is correct (two-letters country code) or whether the dictionary file exists.'
    end
  else
    puts 'Please specify target learning language by two-letters country code (-l)'
  end
end
execute_verify(options) click to toggle source
# File lib/dictation/control_center.rb, line 49
def execute_verify(options)
  execute_speak(options, :verifies)
end
get_dictionary(language) click to toggle source
# File lib/dictation/control_center.rb, line 53
def get_dictionary(language)
  dictionary = nil
  regex = /dict_#{language}_[a-z]{2}\.txt/
  Dir.glob("*").each do |file|
    (dictionary = file) && break if file.match(regex)
  end
  dictionary
end
get_languages(dictionary) click to toggle source
# File lib/dictation/control_center.rb, line 62
def get_languages(dictionary)
  regex = /dict_([a-z]{2})_([a-z]{2})\.txt/
  md = dictionary.match(regex)
  [md[1], md[2]]
end
get_words(options) click to toggle source
# File lib/dictation/control_center.rb, line 68
def get_words(options)
  words = []
  dictionary = nil
  language = options[:language]
  word_begin = options[:word_begin]
  word_end = options[:word_end]
  line_start = options[:line_start]
  line_finish = options[:line_finish]

  if language
    dictionary = get_dictionary(language)
  end

  if dictionary && (word_begin || line_start)
    if word_end
      words = Serializer.deserialize_a_portion_by_given_word(dictionary, word_begin, word_end)
    elsif line_finish
      words = Serializer.deserialize_a_portion_by_given_line(dictionary, line_start, line_finish)
    else
      words = (word_begin ? Serializer.deserialize_a_portion_by_given_word(dictionary, word_begin) : Serializer.deserialize_a_portion_by_given_line(dictionary, line_start))
    end
  else
    words = Serializer.deserialize(dictionary)
  end

  words
end
set_teacher(dictionary) click to toggle source
# File lib/dictation/control_center.rb, line 96
def set_teacher(dictionary)
  languages = get_languages(dictionary)
  tts_dictate = TTS.new(languages[0].intern)
  tts_verify = TTS.new(languages[1].intern)
  Teacher.new(tts_dictate, tts_verify)
end