Changeset 3534
- Timestamp:
- 12/24/07 18:33:20 (5 years ago)
- Files:
-
- 1 modified
-
lang/ruby/misc/yhara/new-harizon.rb (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/ruby/misc/yhara/new-harizon.rb
r3533 r3534 9 9 VOICES = %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) 10 10 11 module 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 20 end 21 11 22 class Speaker 12 23 attr_reader :name 24 13 25 def initialize(name = self.class.name) 14 26 @name = name 15 27 end 16 28 17 def voice(context = [])29 def voice(context = nil) 18 30 @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)] 19 37 end 20 38 … … 49 67 class Yhara < Speaker 50 68 def initialize 51 @name = "yhara"69 super 'yhara' 52 70 end 53 71 … … 58 76 59 77 class Jenifer < Speaker 78 ARABIAN = %w[ايران نيست] 79 60 80 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)] 62 87 end 63 88 … … 65 90 'Princess' 66 91 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 102 end 103 104 class Remark 105 attr_reader :speaker, :words, :eos, :pronounciation 106 107 def initialize(speaker, words, eos, options = {}) 72 108 @speaker = speaker 73 109 @words = words 74 @eos = eos # end of text : "?" or "." 110 @eos = eos # end of text : "?" or "." or "!" 111 @pronounciation = options[:pronounciation] || text 75 112 end 76 113 … … 79 116 end 80 117 81 def question?118 def interrogative? 82 119 @eos == '?' 83 120 end … … 88 125 89 126 def say(context = nil) 90 voice = @speaker.voice(context) 91 `say -v #{voice} #{text}` 127 Kernel.say pronounciation, :voice => @speaker.voice(context) 92 128 end 93 129 … … 97 133 end 98 134 135 include Say 136 137 require 'optparse' 138 config = {} 139 OptionParser.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) 145 end 146 147 lesson = 0 99 148 speakers = [Alex.new, Vicki.new] 100 lesson = 0101 149 102 150 loop do … … 104 152 context = [] 105 153 106 puts "LESSON #{lesson}" 107 `say lesson #{lesson}` 108 `say Repeat after me.` 154 putsnsay "LESSON #{lesson}" 155 say 'Repeat after me.' 109 156 sleep 1 110 157 111 158 (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.25117 118 159 # Jenifer appears 119 if ( context && context.last && context.last.speaker.class == Yhara && rand < 0.25 ) || rand < 0.01160 if ( context.last && Yhara === context.last.speaker && rand < 0.25 ) || rand < 0.01 120 161 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 146 173 end 147 174 148 175 sleep 1 149 150 176 puts 151 unless ARGV.include? '--no-exercise' 152 puts "EXERCISE" 153 `say exercise` 177 178 unless config[:no_exercise] 179 putsnsay "EXERCISE" 180 154 181 (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.') 168 194 else 169 `say y`195 say 'hara.' 170 196 end 171 197 else 172 `say hara`198 sleep 1.5 173 199 end 174 200 end
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)