PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/plugins/yhara.rb

https://github.com/Epictetus/termtter
Ruby | 144 lines | 108 code | 29 blank | 7 comment | 9 complexity | b3ebe340fa80b37bc76e46f6c79550d8 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. if RUBY_VERSION < "1.8.7"
  3. class Array
  4. def choice
  5. at(rand(size))
  6. end
  7. end
  8. end
  9. # based on new-harizon.rb
  10. module Yharian
  11. VOICES =
  12. %w(Agnes Albert Bad\ News Bahh Bells Boing Bruce Bubbles Cellos Deranged Fred Hysterical Junior Kathy Pipe\ Organ Princess Ralph Trinoids Vicki Victoria Whisper Zarvox)
  13. class Speaker
  14. attr_reader :name
  15. def initialize(name)
  16. @name = name
  17. end
  18. def talk(context)
  19. n = 7
  20. words = (0..rand(n)).map { %w[y hara].choice }.
  21. inject {|r, e| r + (rand < 0.97 ? ' ' : ', ') + e }
  22. eos = %w(? ? . . . . . . . . !).choice
  23. [Remark.new(self,words, eos)]
  24. end
  25. def voice(context = nil)
  26. @name
  27. end
  28. end
  29. class Alex < Speaker
  30. def initialize
  31. super 'Alex'
  32. end
  33. end
  34. class Vicki < Speaker
  35. def initialize
  36. super 'Vicki'
  37. end
  38. end
  39. class Yhara < Speaker
  40. def initialize
  41. super 'yhara'
  42. end
  43. def voice(context = nil)
  44. VOICES.choise
  45. end
  46. end
  47. class Jenifer < Speaker
  48. ARABIAN = %w[ايران نيست]
  49. def initialize
  50. super 'jenifer'
  51. end
  52. def talk(context)
  53. words = (0..rand(3)). map { ARABIAN.choice }.join(' ')
  54. [Remark.new(self,words, '')]
  55. end
  56. def voice(context = nil)
  57. 'Princess'
  58. end
  59. end
  60. class Remark
  61. attr_reader :speaker, :words, :eos, :pronounciation
  62. def initialize(speaker, words, eos, options = {})
  63. @speaker = speaker
  64. @words = words
  65. @eos = eos # end of text : "?" or "." or "!"
  66. @pronounciation = options[:pronounciation] || text
  67. end
  68. def text
  69. @words + @eos
  70. end
  71. def interrogative?
  72. @eos == '?'
  73. end
  74. def display
  75. puts "#{@speaker.name}: #{text}"
  76. end
  77. def say(context = nil)
  78. Kernel.say pronounciation, :voice => @speaker.voice(context)
  79. end
  80. def correct?(s)
  81. s.gsub(/[^yhar]/,'') == @words.gsub(/[^yhar]/,'')
  82. end
  83. end
  84. @@context = []
  85. @@speakers = [Alex.new, Vicki.new]
  86. def self.text
  87. if ( @@context.last && Yhara === @@context.last.speaker && rand < 0.25 ) || rand < 0.01
  88. speaker = Jenifer.new
  89. elsif @@context.last && @@context.last.words =~ /y hara/ and @@context.last.interrogative? and rand < 0.25
  90. speaker = Yhara.new
  91. else
  92. speaker = @@speakers[rand(2)]
  93. end
  94. remark = speaker.talk(@@context).first
  95. @@context.push remark
  96. remark.text
  97. end
  98. end
  99. module Termtter::Client
  100. register_command(
  101. :name => :yhara,
  102. :exec_proc => lambda{|arg|
  103. name = normalize_as_user_name(arg)
  104. text = "@#{name} #{Yharian::text}"
  105. Termtter::API.twitter.update(text)
  106. puts "=> #{text}"
  107. },
  108. :help => ["yhara (USER)", 'Post a new Yharian sentence']
  109. )
  110. end
  111. # yhara.rb
  112. # post a new yharian sentence
  113. # example:
  114. # > yhara
  115. # => hara y y hara.