/app/helpers/eliza.rb

https://github.com/vishalmanohar/askjeeves · Ruby · 231 lines · 219 code · 12 blank · 0 comment · 14 complexity · 2602ab5b9907b3ff5252fc81e0a820bb MD5 · raw file

  1. require 'pat5'
  2. module Enumerable
  3. def some?
  4. self.each do |v|
  5. result = yield v
  6. return result if result
  7. end
  8. nil
  9. end
  10. end
  11. class Array
  12. def random_elt
  13. self[rand(self.length)]
  14. end
  15. end
  16. class Rule
  17. attr_reader :pattern, :responses
  18. def initialize(pattern, *responses)
  19. @pattern, @responses = pattern, responses
  20. end
  21. ELIZA_RULES =
  22. [
  23. Rule.new(Pattern.new(%w(*x hello *y)),
  24. "How do you do. Please state your problem"),
  25. Rule.new(Pattern.new(%w(*x I want *y)),
  26. "What would it mean if you got ?y?",
  27. "Why do you want ?y?",
  28. "Suppose you got ?y soon"),
  29. Rule.new(Pattern.new(%w(*x if *y)),
  30. "Do you really think it's likely that ?y? ",
  31. "Do you wish that ?y?",
  32. "What do you think about ?y?",
  33. "Really-- if ?y?"),
  34. Rule.new(Pattern.new(%w(*x no *y)),
  35. "Why not?",
  36. "You are being a bit negative",
  37. "Are you saying \"NO\" just to be negative?"),
  38. Rule.new(Pattern.new(%w(*x I was *y)),
  39. "Were you really?",
  40. "Perhaps I already knew you were ?y?",
  41. "Why do you tell me you were ?y now?"),
  42. Rule.new(Pattern.new(%w(*x I feel *y)),
  43. "Do you often feel ?y?"),
  44. Rule.new(Pattern.new(%w(*x I felt *y)),
  45. "What other feelings do you have?"),
  46. Rule.new(Pattern.new(%w(*x computer *y)),
  47. "Do computers worry you?",
  48. "What do you think about machines?",
  49. "Why do you mention computers",
  50. "What do you think machines have to do with your problem?"),
  51. Rule.new(Pattern.new(%w(*x name *y)),
  52. "I am not interested in names"),
  53. Rule.new(Pattern.new(%w(*x sorry *y)),
  54. "Please don't apologize",
  55. "Apologies are not necessary",
  56. "What feelings do you have when you apologize?"),
  57. Rule.new(Pattern.new(%w(*x I remember *y)),
  58. "Do you often think of ?y?",
  59. "Does thinking of ?y bring anything else to mind?",
  60. "What else do you remember?",
  61. "Why do you recall ?y right now?",
  62. "What in the present situation reminds you of ?y?",
  63. "What is the connection between me and ?y?"),
  64. Rule.new(Pattern.new(%w(*x do you remember *y)),
  65. "Did you think I would forget ?y?",
  66. "Why do you think I should recall ?y now?",
  67. "What about ?y?",
  68. "You mentioned ?y"),
  69. Rule.new(Pattern.new(%w(*x I dreamt *y)),
  70. "Really-- ?y?",
  71. "Have you ever fantasized ?y while you were awake?",
  72. "Have you dreamt ?y before?"),
  73. Rule.new(Pattern.new(%w(*x dream about *y)),
  74. "How do you feel about ?y in reality?"),
  75. Rule.new(Pattern.new(%w(*x dream *y)),
  76. "What does this dream suggest to you?",
  77. "Do you dream often?",
  78. "What persons appear in your dreams?",
  79. "Don't you believe that dream has to do with your problem? "),
  80. Rule.new(Pattern.new(%w(*x my mother *y)),
  81. "Who else in your family ?y?",
  82. "Tell me more about your family"),
  83. Rule.new(Pattern.new(%w(*x my father *y)),
  84. "Your father?",
  85. "Does he influence you strongly?",
  86. "What else comes to mind when you think of your father?"),
  87. Rule.new(Pattern.new(%w(*x I am glad *y)),
  88. "How have I helped you to be ?y?",
  89. "What makes you happy just now?",
  90. "Can you explain why you are suddenly ?y?"),
  91. Rule.new(Pattern.new(%w(*x I am sad *y)),
  92. "I am sorry to hear you are depressed",
  93. "I'm sure it's not pleasant to be sad"),
  94. Rule.new(Pattern.new(%w(*x are like *y)),
  95. "What resemblance do you see between ?x and ?y?"),
  96. Rule.new(Pattern.new(%w(*x is like *y)),
  97. "In what way is it that ?y is like ?y?",
  98. "What resemblance do you see?",
  99. "Could there really be come connection?",
  100. "How?"),
  101. Rule.new(Pattern.new(%w(*x alike *y)),
  102. "In what way?",
  103. "What similarities are there?"),
  104. Rule.new(Pattern.new(%w(*x same *y)),
  105. "What other connections do you see?"),
  106. Rule.new(Pattern.new(%w(*x was I *y)),
  107. "What if you were ?y?",
  108. "Do you think you were ?y?",
  109. "What would it mean if you were ?y?"),
  110. Rule.new(Pattern.new(%w(*x I am *y)),
  111. "In what way are you ?y?",
  112. "Do you want to be ?y?"),
  113. Rule.new(Pattern.new(%w(*x am I *y)),
  114. "Do you believe you are ?y?",
  115. "Would you want to be ?y?",
  116. "You wish I would tell you you are ?y",
  117. "What would it mean if you were ?y?"),
  118. Rule.new(Pattern.new(%w(*x am *y)),
  119. "Why do you say 'AM'? ",
  120. "I don't understand that"),
  121. Rule.new(Pattern.new(%w(*x are you *y)),
  122. "Why are you interested in whether I am ?y or not?",
  123. "Would you prefer if I weren't ?y? ",
  124. "Perhaps I am ?y in your fantasies?"),
  125. Rule.new(Pattern.new(%w(*x you are *y)),
  126. "What makes you think I am ?y?"),
  127. Rule.new(Pattern.new(%w(*x because *y)),
  128. "Is that the real reason?",
  129. "What other reasons might there be?",
  130. "Does that reason seem to explain anything else?"),
  131. Rule.new(Pattern.new(%w(*x were you *y)),
  132. "Perhaps I was ?y",
  133. "What do you think?",
  134. "What if I had been ?y"),
  135. Rule.new(Pattern.new(%w(*x I can't *y)),
  136. "Maybe you could ?y now",
  137. "What if you could ?y?"),
  138. Rule.new(Pattern.new(%w(*x I *y you *z)),
  139. "Perhaps in your fantasy we ?y each other"),
  140. Rule.new(Pattern.new(%w(*x why don't you *y)),
  141. "Should you ?y yourself?",
  142. "Do you believe I don't ?y? ",
  143. "Perhaps I will ?y in good time"),
  144. Rule.new(Pattern.new(%w(*x yes *y)),
  145. "You seem quite positive",
  146. "You are sure?",
  147. "I understand"),
  148. Rule.new(Pattern.new(%w(*x someone *y)),
  149. "Can you be more specific?"),
  150. Rule.new(Pattern.new(%w(*x everyone *y)),
  151. "Surely not everyone?",
  152. "Can you think of anyone in particular?",
  153. "Who for example?",
  154. "You are thinking of a special person"),
  155. Rule.new(Pattern.new(%w(*x always *y)),
  156. "Can you think of a specific example?",
  157. "When?",
  158. "What incident are you thinking of?",
  159. "Really-- always?"),
  160. Rule.new(Pattern.new(%w(*x what *y)),
  161. "Why do you ask?",
  162. "Does that question interest you?",
  163. "What is it you really want to know?",
  164. "What do you think?",
  165. "What comes to your mind when you ask that?"),
  166. Rule.new(Pattern.new(%w(*x perhaps *y)),
  167. "You do not seem quite certain"),
  168. Rule.new(Pattern.new(%w(*x are *y)),
  169. "Did you think they might not be ?y?",
  170. "Possibly they are ?y"),
  171. Rule.new(Pattern.new(%w(*x)),
  172. "Very interesting",
  173. "I am not sure I understand you fully",
  174. "What does that suggest to you?",
  175. "Please continue",
  176. "Go on",
  177. "Do you feel strongly about discussing such things?"),
  178. ]
  179. end
  180. module Eliza
  181. class << self
  182. def run(rules = Rule::ELIZA_RULES)
  183. while true
  184. print "eliza> "
  185. puts eliza_rule(gets.downcase.split, rules)
  186. end
  187. end
  188. def respond(input, rules = Rule::ELIZA_RULES)
  189. eliza_rule(input.downcase.split, rules)
  190. end
  191. def eliza_rule(input, rules)
  192. rules.some? do |rule|
  193. result = rule.pattern.match(input)
  194. if result
  195. switch_viewpoint(result).inject(rule.responses.random_elt) do |sum, repl|
  196. sum.gsub(repl[0], repl[1].join(" "))
  197. end
  198. end
  199. end
  200. end
  201. def switch_viewpoint(words)
  202. replacements = [%w(I you),
  203. %w(you I),
  204. %w(me you),
  205. %w(am are)]
  206. hash = {}
  207. words.each do |key, val|
  208. hash[key] = replacements.inject(val) do |sum, repl|
  209. sum.map { |val| val == repl[0] ? repl[1] : val}
  210. end
  211. end
  212. hash
  213. end
  214. end
  215. end
  216. if __FILE__ == $0
  217. Eliza.run
  218. end