Changeset 3534

Show
Ignore:
Timestamp:
12/24/07 18:33:20 (5 years ago)
Author:
nanki
Message:

lang/ruby/misc/yhara/new-harizon.rb: refactored, use optparse.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/misc/yhara/new-harizon.rb

    r3533 r3534  
    99VOICES = %w(Alex Bruce Fred Junior Ralph Agnes Kathy Princess Vicki Victoria Albert Bad\ News Bahh Bells Boing Bubbles Cellos Deranged Good\ News Hysterical Pipe\ Organ Trinoids Whisper Zarvox) 
    1010 
     11module Say 
     12  def putsnsay(arg) 
     13    puts arg 
     14    say arg 
     15  end 
     16 
     17  def say(arg, options = {}) 
     18    `say #{options[:voice] ? "-v #{options[:voice]}" : ''} #{arg}` 
     19  end 
     20end 
     21 
    1122class Speaker 
    1223  attr_reader :name 
     24 
    1325  def initialize(name = self.class.name) 
    1426    @name = name 
    1527  end 
    1628 
    17   def voice(context = []) 
     29  def voice(context = nil) 
    1830    @name 
     31  end 
     32 
     33  def talk(context) 
     34    words = (0..rand(7)).map { %w[y hara].choice }.join(' ') 
     35    eos = %w(? ? . . . . . . . . !).choice 
     36    [Remark.new(self, words, eos)] 
    1937  end 
    2038 
     
    4967class Yhara < Speaker 
    5068  def initialize 
    51     @name = "yhara" 
     69    super 'yhara' 
    5270  end 
    5371 
     
    5876 
    5977class Jenifer < Speaker 
     78  ARABIAN = %w[ايران نيست] 
     79 
    6080  def initialize 
    61     @name = "jenifer" 
     81    super 'jenifer' 
     82  end 
     83 
     84  def talk(context) 
     85    words, pron = (0..rand(3)).map { ARABIAN.choice }.map {|word| [word, pronounce_arabically(word)] }.transpose.map {|e| e.join(' ') } 
     86    [Remark.new(self, words, '', :pronounciation => pron)] 
    6287  end 
    6388 
     
    6590    'Princess' 
    6691  end 
    67 end 
    68  
    69 class Text 
    70   attr_reader :speaker,:words,:eos 
    71   def initialize(speaker,words,eos) 
     92 
     93  private 
     94  def pronounce_arabically(word) 
     95    case word 
     96    when ARABIAN[0] 
     97      'al-lughatu' 
     98    when ARABIAN[1] 
     99      'l-arabiyah' 
     100    end 
     101  end 
     102end 
     103 
     104class Remark 
     105  attr_reader :speaker, :words, :eos, :pronounciation 
     106 
     107  def initialize(speaker, words, eos, options = {}) 
    72108    @speaker = speaker 
    73109    @words = words 
    74     @eos = eos  # end of text : "?" or "." 
     110    @eos = eos # end of text : "?" or "." or "!" 
     111    @pronounciation = options[:pronounciation] || text 
    75112  end 
    76113 
     
    79116  end 
    80117 
    81   def question? 
     118  def interrogative? 
    82119    @eos == '?' 
    83120  end 
     
    88125 
    89126  def say(context = nil) 
    90     voice = @speaker.voice(context) 
    91     `say -v #{voice} #{text}` 
     127    Kernel.say pronounciation, :voice => @speaker.voice(context) 
    92128  end 
    93129 
     
    97133end 
    98134 
     135include Say 
     136 
     137require 'optparse' 
     138config = {} 
     139OptionParser.new do |opts| 
     140  opts.separator "" 
     141  opts.separator "Optional:" 
     142  opts.on('-E', '--no-exercise'   , 'Skip Exercise.'             ) { config[:no_exercise ] = true } 
     143  opts.on('-I', '--no-interactive', 'Skip Interactive Dictation.') { config[:no_dictation] = true } 
     144  opts.parse(ARGV) 
     145end 
     146 
     147lesson = 0 
    99148speakers = [Alex.new, Vicki.new] 
    100 lesson = 0 
    101149 
    102150loop do 
     
    104152  context = [] 
    105153 
    106   puts "LESSON #{lesson}" 
    107   `say lesson #{lesson}` 
    108   `say Repeat after me.` 
     154  putsnsay "LESSON #{lesson}" 
     155  say 'Repeat after me.' 
    109156  sleep 1 
    110157 
    111158  (5 + rand(10)).times do 
    112     words = (0..rand(7)).map { %w[y hara].choice }.join(' ') 
    113     eos = rand < 0.2 ? '?' : '.' 
    114  
    115     speaker = speakers[rand(2)] 
    116     speaker = Yhara.new if context && context.last && context.last.words =~ /y hara/ and context.last.question? and rand < 0.25 
    117  
    118159    # Jenifer appears 
    119     if ( context && context.last && context.last.speaker.class == Yhara && rand < 0.25 ) ||  rand < 0.01  
     160    if ( context.last && Yhara === context.last.speaker && rand < 0.25 ) ||  rand < 0.01 
    120161      speaker = Jenifer.new 
    121       arabian = %w[ايران  نيست] 
    122       words = (0..rand(3)).map { arabian.choice }.join(' ') 
    123  
    124       text = Text.new(speaker,words,eos) 
    125       text.display  
    126  
    127       words = words.split(/\s+/).map { |word| 
    128         if   word == arabian[0] 
    129           'al-lughatu' 
    130         else word == arabian[1] 
    131           'l-arabiyah' 
    132         end 
    133       } .join(' ') 
    134  
    135       text = Text.new(speaker,words,eos) 
    136       text.say(context) 
    137       next 
    138     end 
    139  
    140     text = Text.new(speaker,words,eos) 
    141  
    142     text.display  
    143     text.say(context) 
    144  
    145     context.push text 
     162    elsif context.last && context.last.words =~ /y hara/ and context.last.interrogative? and rand < 0.25 
     163      speaker = Yhara.new 
     164    else 
     165      speaker = speakers[rand(2)] 
     166    end 
     167 
     168    speaker.talk(context).each do |remark| 
     169      remark.display 
     170      remark.say(context) 
     171      context.push remark 
     172    end 
    146173  end 
    147174 
    148175  sleep 1 
    149  
    150176  puts 
    151   unless ARGV.include? '--no-exercise' 
    152     puts "EXERCISE" 
    153     `say exercise` 
     177 
     178  unless config[:no_exercise] 
     179    putsnsay "EXERCISE" 
     180 
    154181    (1..5).each do |num| 
    155       i = rand(context.size) 
    156       text = context[i] 
    157       context.delete_at i 
    158  
    159       `say #{num}`  
    160       puts "#{num}. #{text.words.gsub(/[yhar]/, '_')} #{text.eos}" 
    161       print "   " 
    162       text.say 
    163  
    164       answer = readline.chomp 
    165       if text.correct?(answer) 
    166         if num == 5 
    167           `say y!` 
     182      remark = context.choice 
     183      context.delete remark 
     184 
     185      say num 
     186      puts "#{num}. #{remark.words.gsub(/[yhar]/,'_')} #{remark.eos}" 
     187      remark.say 
     188 
     189      unless config[:no_dictation] 
     190        print "   " 
     191        answer = readline.chomp 
     192        if remark.correct?(answer) 
     193          say(num == 5 ? 'y!' : 'y.') 
    168194        else 
    169           `say y` 
     195          say 'hara.' 
    170196        end 
    171197      else 
    172         `say hara` 
     198        sleep 1.5 
    173199      end 
    174200    end