err-elizabot

a classic electronic shrink for the chatbot err
git clone https://git.ce9e.org/err-elizabot.git

commit
1aeea8afef9eabf7e462964b6ae05408766ff58e
parent
236100055d87ef1130b23b9bd3d131337435ae8e
Author
Guillaume BINET <gbin@gootz.net>
Date
2013-01-05 18:37
port to py3 + unittests

Diffstat

M eliza.py 518 ++++++++++++++++++++++++++++++-------------------------------
M elizaBot.py 29 ++++++++++++-----------------
A tests/test_eliza.py 19 +++++++++++++++++++

3 files changed, 289 insertions, 277 deletions


diff --git a/eliza.py b/eliza.py

@@ -7,72 +7,70 @@
    7     7 #  last revised: 28 February 2005

    8     8 #----------------------------------------------------------------------

    9     9 

   10    -1 import string

   11    10 import re

   12    11 import random

   13    12 

   -1    13 

   14    14 class Eliza:

   15    -1   def __init__(self):

   16    -1     self.keys = map(lambda x:re.compile(x[0], re.IGNORECASE),gPats)

   17    -1     self.values = map(lambda x:x[1],gPats)

   18    -1 

   19    -1   #----------------------------------------------------------------------

   20    -1   # translate: take a string, replace any words found in dict.keys()

   21    -1   #  with the corresponding dict.values()

   22    -1   #----------------------------------------------------------------------

   23    -1   def translate(self,str,dict):

   24    -1     words = string.split(string.lower(str))

   25    -1     keys = dict.keys()

   26    -1     for i in range(0,len(words)):

   27    -1       if words[i] in keys:

   28    -1         words[i] = dict[words[i]]

   29    -1     return string.join(words)

   30    -1 

   31    -1   #----------------------------------------------------------------------

   32    -1   #  respond: take a string, a set of regexps, and a corresponding

   33    -1   #    set of response lists; find a match, and return a randomly

   34    -1   #    chosen response from the corresponding list.

   35    -1   #----------------------------------------------------------------------

   36    -1   def respond(self,str):

   37    -1     # find a match among keys

   38    -1     for i in range(0,len(self.keys)):

   39    -1       match = self.keys[i].match(str)

   40    -1       if match:

   41    -1         # found a match ... stuff with corresponding value

   42    -1         # chosen randomly from among the available options

   43    -1         resp = random.choice(self.values[i])

   44    -1         # we've got a response... stuff in reflected text where indicated

   45    -1         pos = string.find(resp,'%')

   46    -1         while pos > -1:

   47    -1           num = string.atoi(resp[pos+1:pos+2])

   48    -1           resp = resp[:pos] + \

   49    -1             self.translate(match.group(num),gReflections) + \

   50    -1             resp[pos+2:]

   51    -1           pos = string.find(resp,'%')

   52    -1         # fix munged punctuation at the end

   53    -1         if resp[-2:] == '?.': resp = resp[:-2] + '.'

   54    -1         if resp[-2:] == '??': resp = resp[:-2] + '?'

   55    -1         return resp

   -1    15     def __init__(self):

   -1    16         self.keys = [re.compile(x[0], re.IGNORECASE) for x in gPats]

   -1    17         self.values = [x[1] for x in gPats]

   -1    18 

   -1    19     #----------------------------------------------------------------------

   -1    20     # translate: take a string, replace any words found in dict.keys()

   -1    21     #  with the corresponding dict.values()

   -1    22     #----------------------------------------------------------------------

   -1    23     def translate(self, str, dict):

   -1    24         words = str.lower().split()

   -1    25         keys = dict.keys()

   -1    26         for i in range(0, len(words)):

   -1    27             if words[i] in keys:

   -1    28                 words[i] = dict[words[i]]

   -1    29         return ''.join(words)

   -1    30 

   -1    31     #----------------------------------------------------------------------

   -1    32     #  respond: take a string, a set of regexps, and a corresponding

   -1    33     #    set of response lists; find a match, and return a randomly

   -1    34     #    chosen response from the corresponding list.

   -1    35     #----------------------------------------------------------------------

   -1    36     def respond(self, str):

   -1    37         # find a match among keys

   -1    38         for i in range(0, len(self.keys)):

   -1    39             match = self.keys[i].match(str)

   -1    40             if match:

   -1    41                 # found a match ... stuff with corresponding value

   -1    42                 # chosen randomly from among the available options

   -1    43                 resp = random.choice(self.values[i])

   -1    44                 # we've got a response... stuff in reflected text where indicated

   -1    45                 pos = resp.find('%')

   -1    46                 while pos > -1:

   -1    47                     num = int(resp[pos + 1:pos + 2])

   -1    48                     resp = resp[:pos] + self.translate(match.group(num), gReflections) + resp[pos + 2:]

   -1    49                     pos = resp.find('%')

   -1    50                     # fix munged punctuation at the end

   -1    51                 if resp[-2:] == '?.': resp = resp[:-2] + '.'

   -1    52                 if resp[-2:] == '??': resp = resp[:-2] + '?'

   -1    53                 return resp

   56    54 

   57    55 #----------------------------------------------------------------------

   58    56 # gReflections, a translation table used to convert things you say

   59    57 #    into things the computer says back, e.g. "I am" --> "you are"

   60    58 #----------------------------------------------------------------------

   61    59 gReflections = {

   62    -1   "am"   : "are",

   63    -1   "was"  : "were",

   64    -1   "i"    : "you",

   65    -1   "i'd"  : "you would",

   66    -1   "i've"  : "you have",

   67    -1   "i'll"  : "you will",

   68    -1   "my"  : "your",

   69    -1   "are"  : "am",

   70    -1   "you've": "I have",

   71    -1   "you'll": "I will",

   72    -1   "your"  : "my",

   73    -1   "yours"  : "mine",

   74    -1   "you"  : "me",

   75    -1   "me"  : "you"

   -1    60     "am": "are",

   -1    61     "was": "were",

   -1    62     "i": "you",

   -1    63     "i'd": "you would",

   -1    64     "i've": "you have",

   -1    65     "i'll": "you will",

   -1    66     "my": "your",

   -1    67     "are": "am",

   -1    68     "you've": "I have",

   -1    69     "you'll": "I will",

   -1    70     "your": "my",

   -1    71     "yours": "mine",

   -1    72     "you": "me",

   -1    73     "me": "you"

   76    74 }

   77    75 

   78    76 #----------------------------------------------------------------------

@@ -82,208 +80,207 @@ gReflections = {
   82    80 #  %1, %2, etc.  

   83    81 #----------------------------------------------------------------------

   84    82 gPats = [

   85    -1   [r'I need (.*)',

   86    -1   [  "Why do you need %1?",

   87    -1     "Would it really help you to get %1?",

   88    -1     "Are you sure you need %1?"]],

   89    -1   

   90    -1   [r'Why don\'?t you ([^\?]*)\??',

   91    -1   [  "Do you really think I don't %1?",

   92    -1     "Perhaps eventually I will %1.",

   93    -1     "Do you really want me to %1?"]],

   94    -1   

   95    -1   [r'Why can\'?t I ([^\?]*)\??',

   96    -1   [  "Do you think you should be able to %1?",

   97    -1     "If you could %1, what would you do?",

   98    -1     "I don't know -- why can't you %1?",

   99    -1     "Have you really tried?"]],

  100    -1   

  101    -1   [r'I can\'?t (.*)',

  102    -1   [  "How do you know you can't %1?",

  103    -1     "Perhaps you could %1 if you tried.",

  104    -1     "What would it take for you to %1?"]],

  105    -1   

  106    -1   [r'I am (.*)',

  107    -1   [  "Did you come to me because you are %1?",

  108    -1     "How long have you been %1?",

  109    -1     "How do you feel about being %1?"]],

  110    -1   

  111    -1   [r'I\'?m (.*)',

  112    -1   [  "How does being %1 make you feel?",

  113    -1     "Do you enjoy being %1?",

  114    -1     "Why do you tell me you're %1?",

  115    -1     "Why do you think you're %1?"]],

  116    -1   

  117    -1   [r'Are you ([^\?]*)\??',

  118    -1   [  "Why does it matter whether I am %1?",

  119    -1     "Would you prefer it if I were not %1?",

  120    -1     "Perhaps you believe I am %1.",

  121    -1     "I may be %1 -- what do you think?"]],

  122    -1   

  123    -1   [r'What (.*)',

  124    -1   [  "Why do you ask?",

  125    -1     "How would an answer to that help you?",

  126    -1     "What do you think?"]],

  127    -1   

  128    -1   [r'How (.*)',

  129    -1   [  "How do you suppose?",

  130    -1     "Perhaps you can answer your own question.",

  131    -1     "What is it you're really asking?"]],

  132    -1   

  133    -1   [r'Because (.*)',

  134    -1   [  "Is that the real reason?",

  135    -1     "What other reasons come to mind?",

  136    -1     "Does that reason apply to anything else?",

  137    -1     "If %1, what else must be true?"]],

  138    -1   

  139    -1   [r'(.*) sorry (.*)',

  140    -1   [  "There are many times when no apology is needed.",

  141    -1     "What feelings do you have when you apologize?"]],

  142    -1   

  143    -1   [r'Hello(.*)',

  144    -1   [  "Hello... I'm glad you could drop by today.",

  145    -1     "Hi there... how are you today?",

  146    -1     "Hello, how are you feeling today?"]],

  147    -1   

  148    -1   [r'I think (.*)',

  149    -1   [  "Do you doubt %1?",

  150    -1     "Do you really think so?",

  151    -1     "But you're not sure %1?"]],

  152    -1   

  153    -1   [r'(.*) friend (.*)',

  154    -1   [  "Tell me more about your friends.",

  155    -1     "When you think of a friend, what comes to mind?",

  156    -1     "Why don't you tell me about a childhood friend?"]],

  157    -1   

  158    -1   [r'Yes',

  159    -1   [  "You seem quite sure.",

  160    -1     "OK, but can you elaborate a bit?"]],

  161    -1   

  162    -1   [r'(.*) computer(.*)',

  163    -1   [  "Are you really talking about me?",

  164    -1     "Does it seem strange to talk to a computer?",

  165    -1     "How do computers make you feel?",

  166    -1     "Do you feel threatened by computers?"]],

  167    -1   

  168    -1   [r'Is it (.*)',

  169    -1   [  "Do you think it is %1?",

  170    -1     "Perhaps it's %1 -- what do you think?",

  171    -1     "If it were %1, what would you do?",

  172    -1     "It could well be that %1."]],

  173    -1   

  174    -1   [r'It is (.*)',

  175    -1   [  "You seem very certain.",

  176    -1     "If I told you that it probably isn't %1, what would you feel?"]],

  177    -1   

  178    -1   [r'Can you ([^\?]*)\??',

  179    -1   [  "What makes you think I can't %1?",

  180    -1     "If I could %1, then what?",

  181    -1     "Why do you ask if I can %1?"]],

  182    -1   

  183    -1   [r'Can I ([^\?]*)\??',

  184    -1   [  "Perhaps you don't want to %1.",

  185    -1     "Do you want to be able to %1?",

  186    -1     "If you could %1, would you?"]],

  187    -1   

  188    -1   [r'You are (.*)',

  189    -1   [  "Why do you think I am %1?",

  190    -1     "Does it please you to think that I'm %1?",

  191    -1     "Perhaps you would like me to be %1.",

  192    -1     "Perhaps you're really talking about yourself?"]],

  193    -1   

  194    -1   [r'You\'?re (.*)',

  195    -1   [  "Why do you say I am %1?",

  196    -1     "Why do you think I am %1?",

  197    -1     "Are we talking about you, or me?"]],

  198    -1   

  199    -1   [r'I don\'?t (.*)',

  200    -1   [  "Don't you really %1?",

  201    -1     "Why don't you %1?",

  202    -1     "Do you want to %1?"]],

  203    -1   

  204    -1   [r'I feel (.*)',

  205    -1   [  "Good, tell me more about these feelings.",

  206    -1     "Do you often feel %1?",

  207    -1     "When do you usually feel %1?",

  208    -1     "When you feel %1, what do you do?"]],

  209    -1   

  210    -1   [r'I have (.*)',

  211    -1   [  "Why do you tell me that you've %1?",

  212    -1     "Have you really %1?",

  213    -1     "Now that you have %1, what will you do next?"]],

  214    -1   

  215    -1   [r'I would (.*)',

  216    -1   [  "Could you explain why you would %1?",

  217    -1     "Why would you %1?",

  218    -1     "Who else knows that you would %1?"]],

  219    -1   

  220    -1   [r'Is there (.*)',

  221    -1   [  "Do you think there is %1?",

  222    -1     "It's likely that there is %1.",

  223    -1     "Would you like there to be %1?"]],

  224    -1   

  225    -1   [r'My (.*)',

  226    -1   [  "I see, your %1.",

  227    -1     "Why do you say that your %1?",

  228    -1     "When your %1, how do you feel?"]],

  229    -1   

  230    -1   [r'You (.*)',

  231    -1   [  "We should be discussing you, not me.",

  232    -1     "Why do you say that about me?",

  233    -1     "Why do you care whether I %1?"]],

  234    -1     

  235    -1   [r'Why (.*)',

  236    -1   [  "Why don't you tell me the reason why %1?",

  237    -1     "Why do you think %1?" ]],

  238    -1     

  239    -1   [r'I want (.*)',

  240    -1   [  "What would it mean to you if you got %1?",

  241    -1     "Why do you want %1?",

  242    -1     "What would you do if you got %1?",

  243    -1     "If you got %1, then what would you do?"]],

  244    -1   

  245    -1   [r'(.*) mother(.*)',

  246    -1   [  "Tell me more about your mother.",

  247    -1     "What was your relationship with your mother like?",

  248    -1     "How do you feel about your mother?",

  249    -1     "How does this relate to your feelings today?",

  250    -1     "Good family relations are important."]],

  251    -1   

  252    -1   [r'(.*) father(.*)',

  253    -1   [  "Tell me more about your father.",

  254    -1     "How did your father make you feel?",

  255    -1     "How do you feel about your father?",

  256    -1     "Does your relationship with your father relate to your feelings today?",

  257    -1     "Do you have trouble showing affection with your family?"]],

  258    -1 

  259    -1   [r'(.*) child(.*)',

  260    -1   [  "Did you have close friends as a child?",

  261    -1     "What is your favorite childhood memory?",

  262    -1     "Do you remember any dreams or nightmares from childhood?",

  263    -1     "Did the other children sometimes tease you?",

  264    -1     "How do you think your childhood experiences relate to your feelings today?"]],

  265    -1     

  266    -1   [r'(.*)\?',

  267    -1   [  "Why do you ask that?",

  268    -1     "Please consider whether you can answer your own question.",

  269    -1     "Perhaps the answer lies within yourself?",

  270    -1     "Why don't you tell me?"]],

  271    -1   

  272    -1   [r'quit',

  273    -1   [  "Thank you for talking with me.",

  274    -1     "Good-bye.",

  275    -1     "Thank you, that will be $150.  Have a good day!"]],

  276    -1   

  277    -1   [r'(.*)',

  278    -1   [  "Please tell me more.",

  279    -1     "Let's change focus a bit... Tell me about your family.",

  280    -1     "Can you elaborate on that?",

  281    -1     "Why do you say that %1?",

  282    -1     "I see.",

  283    -1     "Very interesting.",

  284    -1     "%1.",

  285    -1     "I see.  And what does that tell you?",

  286    -1     "How does that make you feel?",

  287    -1     "How do you feel when you say that?"]]

  288    -1   ]

   -1    83     [r'I need (.*)',

   -1    84      ["Why do you need %1?",

   -1    85       "Would it really help you to get %1?",

   -1    86       "Are you sure you need %1?"]],

   -1    87 

   -1    88     [r'Why don\'?t you ([^\?]*)\??',

   -1    89      ["Do you really think I don't %1?",

   -1    90       "Perhaps eventually I will %1.",

   -1    91       "Do you really want me to %1?"]],

   -1    92 

   -1    93     [r'Why can\'?t I ([^\?]*)\??',

   -1    94      ["Do you think you should be able to %1?",

   -1    95       "If you could %1, what would you do?",

   -1    96       "I don't know -- why can't you %1?",

   -1    97       "Have you really tried?"]],

   -1    98 

   -1    99     [r'I can\'?t (.*)',

   -1   100      ["How do you know you can't %1?",

   -1   101       "Perhaps you could %1 if you tried.",

   -1   102       "What would it take for you to %1?"]],

   -1   103 

   -1   104     [r'I am (.*)',

   -1   105      ["Did you come to me because you are %1?",

   -1   106       "How long have you been %1?",

   -1   107       "How do you feel about being %1?"]],

   -1   108 

   -1   109     [r'I\'?m (.*)',

   -1   110      ["How does being %1 make you feel?",

   -1   111       "Do you enjoy being %1?",

   -1   112       "Why do you tell me you're %1?",

   -1   113       "Why do you think you're %1?"]],

   -1   114 

   -1   115     [r'Are you ([^\?]*)\??',

   -1   116      ["Why does it matter whether I am %1?",

   -1   117       "Would you prefer it if I were not %1?",

   -1   118       "Perhaps you believe I am %1.",

   -1   119       "I may be %1 -- what do you think?"]],

   -1   120 

   -1   121     [r'What (.*)',

   -1   122      ["Why do you ask?",

   -1   123       "How would an answer to that help you?",

   -1   124       "What do you think?"]],

   -1   125 

   -1   126     [r'How (.*)',

   -1   127      ["How do you suppose?",

   -1   128       "Perhaps you can answer your own question.",

   -1   129       "What is it you're really asking?"]],

   -1   130 

   -1   131     [r'Because (.*)',

   -1   132      ["Is that the real reason?",

   -1   133       "What other reasons come to mind?",

   -1   134       "Does that reason apply to anything else?",

   -1   135       "If %1, what else must be true?"]],

   -1   136 

   -1   137     [r'(.*) sorry (.*)',

   -1   138      ["There are many times when no apology is needed.",

   -1   139       "What feelings do you have when you apologize?"]],

   -1   140 

   -1   141     [r'Hello(.*)',

   -1   142      ["Hello... I'm glad you could drop by today.",

   -1   143       "Hi there... how are you today?",

   -1   144       "Hello, how are you feeling today?"]],

   -1   145 

   -1   146     [r'I think (.*)',

   -1   147      ["Do you doubt %1?",

   -1   148       "Do you really think so?",

   -1   149       "But you're not sure %1?"]],

   -1   150 

   -1   151     [r'(.*) friend (.*)',

   -1   152      ["Tell me more about your friends.",

   -1   153       "When you think of a friend, what comes to mind?",

   -1   154       "Why don't you tell me about a childhood friend?"]],

   -1   155 

   -1   156     [r'Yes',

   -1   157      ["You seem quite sure.",

   -1   158       "OK, but can you elaborate a bit?"]],

   -1   159 

   -1   160     [r'(.*) computer(.*)',

   -1   161      ["Are you really talking about me?",

   -1   162       "Does it seem strange to talk to a computer?",

   -1   163       "How do computers make you feel?",

   -1   164       "Do you feel threatened by computers?"]],

   -1   165 

   -1   166     [r'Is it (.*)',

   -1   167      ["Do you think it is %1?",

   -1   168       "Perhaps it's %1 -- what do you think?",

   -1   169       "If it were %1, what would you do?",

   -1   170       "It could well be that %1."]],

   -1   171 

   -1   172     [r'It is (.*)',

   -1   173      ["You seem very certain.",

   -1   174       "If I told you that it probably isn't %1, what would you feel?"]],

   -1   175 

   -1   176     [r'Can you ([^\?]*)\??',

   -1   177      ["What makes you think I can't %1?",

   -1   178       "If I could %1, then what?",

   -1   179       "Why do you ask if I can %1?"]],

   -1   180 

   -1   181     [r'Can I ([^\?]*)\??',

   -1   182      ["Perhaps you don't want to %1.",

   -1   183       "Do you want to be able to %1?",

   -1   184       "If you could %1, would you?"]],

   -1   185 

   -1   186     [r'You are (.*)',

   -1   187      ["Why do you think I am %1?",

   -1   188       "Does it please you to think that I'm %1?",

   -1   189       "Perhaps you would like me to be %1.",

   -1   190       "Perhaps you're really talking about yourself?"]],

   -1   191 

   -1   192     [r'You\'?re (.*)',

   -1   193      ["Why do you say I am %1?",

   -1   194       "Why do you think I am %1?",

   -1   195       "Are we talking about you, or me?"]],

   -1   196 

   -1   197     [r'I don\'?t (.*)',

   -1   198      ["Don't you really %1?",

   -1   199       "Why don't you %1?",

   -1   200       "Do you want to %1?"]],

   -1   201 

   -1   202     [r'I feel (.*)',

   -1   203      ["Good, tell me more about these feelings.",

   -1   204       "Do you often feel %1?",

   -1   205       "When do you usually feel %1?",

   -1   206       "When you feel %1, what do you do?"]],

   -1   207 

   -1   208     [r'I have (.*)',

   -1   209      ["Why do you tell me that you've %1?",

   -1   210       "Have you really %1?",

   -1   211       "Now that you have %1, what will you do next?"]],

   -1   212 

   -1   213     [r'I would (.*)',

   -1   214      ["Could you explain why you would %1?",

   -1   215       "Why would you %1?",

   -1   216       "Who else knows that you would %1?"]],

   -1   217 

   -1   218     [r'Is there (.*)',

   -1   219      ["Do you think there is %1?",

   -1   220       "It's likely that there is %1.",

   -1   221       "Would you like there to be %1?"]],

   -1   222 

   -1   223     [r'My (.*)',

   -1   224      ["I see, your %1.",

   -1   225       "Why do you say that your %1?",

   -1   226       "When your %1, how do you feel?"]],

   -1   227 

   -1   228     [r'You (.*)',

   -1   229      ["We should be discussing you, not me.",

   -1   230       "Why do you say that about me?",

   -1   231       "Why do you care whether I %1?"]],

   -1   232 

   -1   233     [r'Why (.*)',

   -1   234      ["Why don't you tell me the reason why %1?",

   -1   235       "Why do you think %1?"]],

   -1   236 

   -1   237     [r'I want (.*)',

   -1   238      ["What would it mean to you if you got %1?",

   -1   239       "Why do you want %1?",

   -1   240       "What would you do if you got %1?",

   -1   241       "If you got %1, then what would you do?"]],

   -1   242 

   -1   243     [r'(.*) mother(.*)',

   -1   244      ["Tell me more about your mother.",

   -1   245       "What was your relationship with your mother like?",

   -1   246       "How do you feel about your mother?",

   -1   247       "How does this relate to your feelings today?",

   -1   248       "Good family relations are important."]],

   -1   249 

   -1   250     [r'(.*) father(.*)',

   -1   251      ["Tell me more about your father.",

   -1   252       "How did your father make you feel?",

   -1   253       "How do you feel about your father?",

   -1   254       "Does your relationship with your father relate to your feelings today?",

   -1   255       "Do you have trouble showing affection with your family?"]],

   -1   256 

   -1   257     [r'(.*) child(.*)',

   -1   258      ["Did you have close friends as a child?",

   -1   259       "What is your favorite childhood memory?",

   -1   260       "Do you remember any dreams or nightmares from childhood?",

   -1   261       "Did the other children sometimes tease you?",

   -1   262       "How do you think your childhood experiences relate to your feelings today?"]],

   -1   263 

   -1   264     [r'(.*)\?',

   -1   265      ["Why do you ask that?",

   -1   266       "Please consider whether you can answer your own question.",

   -1   267       "Perhaps the answer lies within yourself?",

   -1   268       "Why don't you tell me?"]],

   -1   269 

   -1   270     [r'quit',

   -1   271      ["Thank you for talking with me.",

   -1   272       "Good-bye.",

   -1   273       "Thank you, that will be $150.  Have a good day!"]],

  289   274 

   -1   275     [r'(.*)',

   -1   276      ["Please tell me more.",

   -1   277       "Let's change focus a bit... Tell me about your family.",

   -1   278       "Can you elaborate on that?",

   -1   279       "Why do you say that %1?",

   -1   280       "I see.",

   -1   281       "Very interesting.",

   -1   282       "%1.",

   -1   283       "I see.  And what does that tell you?",

   -1   284       "How does that make you feel?",

   -1   285       "How do you feel when you say that?"]]

   -1   286 
   -1   286 
\ No newline at end of file

diff --git a/elizaBot.py b/elizaBot.py

@@ -2,37 +2,32 @@ import json
    2     2 from random import choice
    3     3 from lxml import objectify
    4     4 from eliza import Eliza
    5    -1 from urllib2 import urlopen,quote
   -1     5 from errbot import botcmd, BotPlugin, PY2
    6     6 
    7    -1 # Backward compatibility
    8    -1 from errbot.version import VERSION
    9    -1 from errbot.utils import version2array
   10    -1 if version2array(VERSION) >= [1,6,0]:
   11    -1     from errbot import botcmd, BotPlugin
   -1     7 if PY2:
   -1     8     from urllib2 import urlopen, quote
   12     9 else:
   13    -1     from errbot.botplugin import BotPlugin
   14    -1     from errbot.jabberbot import botcmd
   15    -1 
   16    -1 
   -1    10     from urllib.request import urlopen, quote
   17    11 
   18    12 class ElizaBot(BotPlugin):
   19    13     eliza_daemon = Eliza()
   -1    14 
   20    15     @botcmd
   21    -1     def eliza(self, mess, args):
   -1    16     def eliza(self, _, args):
   22    17         """ El'cheapo shrink for you """
   23    18         args = args.strip()
   24    19         return self.eliza_daemon.respond(args)
   25    20 
   26    21     @botcmd
   27    -1     def askus(self, mess, args):
   -1    22     def askus(self, _, __):
   28    23         """ Give us a fun topic to talk about
   29    24             thx to http://chatoms.com/
   30    25         """
   31    26         content = urlopen('http://chatoms.com/chatom.json?Normal=1&Fun=2&Philosophy=3&Out+There=4&Love=5&Personal=7').read()
   32    -1         return json.loads(content)['text']
   -1    27         return json.loads(content.decode())['text']
   33    28 
   34    29     @botcmd
   35    -1     def complete(self, mess, args):
   -1    30     def complete(self, _, args):
   36    31         """ Complete the given sentence
   37    32             thx to the awesome google completion
   38    33         """
@@ -40,10 +35,9 @@ class ElizaBot(BotPlugin):
   40    35         if not args:
   41    36             return 'Complete what ?'
   42    37 
   43    -1         content = urlopen('http://google.com/complete/search?q=%s&output=toolbar'%quote(args)).read()
   -1    38         content = urlopen('http://google.com/complete/search?q=%s&output=toolbar' % quote(args)).read()
   44    39         xml = objectify.fromstring(content)
   45    40         possibilities = xml.xpath("//toplevel/CompleteSuggestion/suggestion/@data")
   46    41         if possibilities:
   47    42             return choice(possibilities)
   48    -1         return 'Hmmm... no answer for that'
   49    -1 
   -1    43         return 'Hmmm... no answer for that
   -1    43 
\ No newline at end of file

diff --git a/tests/test_eliza.py b/tests/test_eliza.py

@@ -0,0 +1,18 @@
   -1     1 # coding=utf-8
   -1     2 from errbot.backends.test import FullStackTest, pushMessage, popMessage
   -1     3 
   -1     4 
   -1     5 class TestCommands(FullStackTest):
   -1     6     @classmethod
   -1     7     def setUpClass(cls, extra=None):
   -1     8         super(TestCommands, cls).setUpClass(__file__)
   -1     9 
   -1    10     def test_eliza(self):
   -1    11         pushMessage('!eliza Hello')
   -1    12         self.assertRegex(popMessage(), r"Hi|Hello")
   -1    13 
   -1    14     def test_askus(self):
   -1    15         self.assertCommandFound('!askus')
   -1    16 
   -1    17     def test_complete(self):
   -1    18         self.assertCommand('!complete paris is', 'paris is'
   -1    18 
\ No newline at end of file