Eliza like chatterbot in Python

Basically, the brain takes in a list of two lists that defines it’s
personality : [patternResponsepairs,defefaultReplies].
The pattern response pairs contains a list of patterns and
their corresponding responses.
A PATTERN is a Python list containing any number of the following elements in any order:
WORD: This is a string containing alphabetic characters only. It must match an
element in a SENTENCE exactly as written.
0: This matches any number of WORDs in a SENTENCE.
1: This matches exactly one WORD in a SENTENCE
[0, identifier]: This matches zero or more WORDs in a SENTENCE, and binds them to the identifier.
[1, identifier]: This matches exactly one WORD in a SENTENCE, and binds it to the identifier.

A default response is responses that the bot will spit out when it can’t find a match.

#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#GNU General Public License for more details.
#For a copy of the license see .
# A simple eliza bot in python
import re
import random
class Agent:
    def __init__(self,brain):
        self.patternResponses = []
        self.defaultResponses = []
        self.Usersname =''
        self.Usernametag ='Username'
        self.defRespIndex =0
        self.pronounMap = {
                           #generic pronouns
                           'my':'your', 'me':'you',
                           # "you've":"i've","they've":"we've",
                           #some names, these wll get applied if the name
                           #isnt the current user's name
                           'fred':'he', 'jack':'he',
    def change_mind(self,brain):
        Change mind on the fly.
        To change the Agent's brain
        assert (len(brain) == 2)
        self.patternResponses = brain[0]
        self.defaultResponses = brain[1]
        print "Brain initialized."
    def tell(self,input):
        sentence = input.lower().strip().replace('(','').replace(')','').split(' ')
        bl = []
        matchedPat = []
        for pat in self.patternResponses:
            bl = self.pattern_match(pat[0],sentence,[])
            if bl == ['FAIL']:
            matchedPat = pat
        if bl == ['FAIL']:
            #choose from a default response
            # or just append a question mark on the
            # user's sentence.
            print '... No pattern Response pair selected.'
            print '... Choosing a default response.'
            #Used to be randon using random.choice
            #but then i noticed that the requirements say that
            #the same default response may not be used again
            #before every other one has been used.
            self.defRespIndex = self.defRespIndex +1
            if  self.defRespIndex >= len(self.defaultResponses):
                #reset the counter to 0
                self.defRespIndex = 0 ;
                #Possible improvement: shuffle the default response array, so that
                # the responses dont come in the same sequence again.
                #last on got chosen, make the user input into
                #a question
                return ' '.join(sentence) + ' ?'
                return ' '.join(self.defaultResponses[self.defRespIndex])
            print '... Pattern Response pair selected:', pat
            print '... BL before call to change_pronouns: ',bl
            blAfter = self.change_pronoun(bl)
            print '... BL after call to change_pronouns: ',blAfter
            rep = self.build_reply(pat[1],blAfter)
            return ' '.join(rep)
    def make_regexp(self,pattern):
        Convert a pattern list into an equivalent
        regular expression
        r = ['^']  #for exact matches we prepend ^ and $ to the regexp.
        usedBackrefs = []
        for tok in pattern:
            if isinstance(tok,str):
                r.append('(' + tok +')')
            elif isinstance(tok, int):
                # 0 or 1
                if tok == 0 :
                    r.append('[a-zA-Z ]*?') # match 0+ word(chars + space),
                elif tok == 1:
                    r.append('[a-zA-Z]+') # match 1 word
                    raise Exception, "Ints can only be 1 or 0, not:", tok
            elif isinstance(tok, list):
                #bindinglist pattern list
                if tok[0] == 0 :
                    #if we havent used this backref yet, then add ?P<name>
                    #and add it to the used backrefs list.
                    if not tok[1] in usedBackrefs:
                        r.append('(?P<'+tok[1] +'>[a-zA-Z ]*?)')
                        #if we have come across his back ref, then add?P=name
                elif tok[0] == 1:
                    if not tok[1] in usedBackrefs:
                        r.append('(?P<'+tok[1] +'>[A-Za-z]+)')
                        #if we have come across his back ref, then add?P=name
                    raise Exception, "Valid first elems of BL pattern = 1 or 0, not:", tok
        #for exact matches we prepend ^ and $ to the regexp.
        #also, we disregard surrounding whitespace.
        ret =  '(s)*'.join(r) + '$(s)*'
        return re.compile(ret)
    def pattern_match(self,pattern,sentence,bl):
        return the populated binding list if the pattern matches,
        otherwise return the binding list with the element 'FAIL'
        pat = self.make_regexp(pattern)
        s = ' '.join(sentence)
        matches =  re.search(pat,s)
        if (matches == None): # if we dont have a match
            #then return w/o modifying the bindinglist.
            return ['FAIL']
        bindingList = list(bl)
        grps = matches.groups()
        for k in pat.groupindex:
            for x in grps[pat.groupindex[k]- 1].split(' '):
                if x.strip() !='':
        return bindingList
    def change_pronoun(self,bl):
        reverses pronouns in a binding list.
        #if the pronoun dic contains a elemen, then
        #replace it with its corresponding value.
        bindingList = list(bl)
        for i in range(len(bindingList)):
            if isinstance( bindingList[i], list):
                for j in range(len(bindingList[i])):
                    if j > 0:
                        if self.pronounMap.has_key(bindingList[i][j].lower()):
                            bindingList[i][j] = self.pronounMap[bindingList[i][j]]
        return bindingList
    def build_reply(self,respPattern,bindingList):
        build_reply ([[1, 'subject'], 'loves', [0, 'object']],
                    [['subject', 'jane'], ['object', 'ice', 'cream']])
        will return  ['jane', 'loves', 'ice', 'cream'].
        #convert the bindlingList to a dictionary so thats its easy to
        #get the binding label
        bl = {}
        reply = []
        for elem in bindingList:
            #do the username chack firsdt, the id Username is
            #only used in relation to the username .This is
            #so that we can kinda remember the user's name
            if (elem[0] == self.Usernametag):
                # if we already have user name set, then
                # Hi, username , what happend to oldusername
                if self.Usersname !='':
                    tmp =self.Usersname
                    self.Usersname = elem[1]
                    return [elem[1],'eh?','what','ever','happened','to',tmp]
                    self.Usersname = elem[1]
            if bl.has_key(elem[0]) == elem[1:]:
                raise Exception, 'Binding list has name collision for: ', elem[0]
                if isinstance(elem,list) and len(elem) > 1:
                    bl[elem[0]] = elem[1:]
                #    print elem, 'binding element ',elem,' has no binded things'
        for elem in respPattern:
            if isinstance(elem,list) and len(elem) > 1:
                x = elem[1]
                if bl.has_key(x):
                    for y in bl[x]:
                #    print 'not found in bl:', x
            else: #anything else, just append to the list.
        return reply
def talk(brain):
    assert((isinstance(brain,list)) and (len(brain) ==2 ))
    b = Agent(brain)
    while 1:
            inp = raw_input("> ").lower()
        except EOFError:
            print 'Toodle do,n you can type (quit) if you want be more pleasant.'
        if len(inp.strip()) > 0 :
            if inp.strip().lower() == 'quit':
                print 'To exit the program, type (quit) [In parentheses]'
            if inp.strip().lower() == '(quit)':
                print 'Bye'
                print b.tell(inp)
def start():
    defReplys = [
    patternResps = [
    [['you','said',[0,'we']],['I may have said ',[0,'we'],'What about it?']],
    [[[1, 'subject'], 'loves', [0, 'object']],['Laudable','why','does',[1,'subject'],'love',[0,'object'],'?']],
    [['because'],['Lack of reason is not a reason']],
    [['how', 'is','your',[0,'object']],['my',[0,'object'],'is','holding','up','well','.do','you','have','a',[0,'object'],'?']],
    [[[0,'we'],'i','have','a',[0,'thing']],['how','goes', 'it','with','your',[0,'thing'],'?']],
    [[[0,'we'],'i','have',[0,'thing']],['how','is', 'your',[0,'thing'],'?']],
    [[0,'you',[1,'b'],'me'],['What','gave','you','the','impression','I',[1,'b'],'you ?']],
    #anything that ends with exclamation
    [[0,'!'],['Keep calm']],
    # if its a question that hasnt gotten matched uptil now
    # do this. Note: This needs to be last to ensure its the last test
    [[[0, 'question'], '?'],['What','do','you','mean', [0,'question'],'?']],
    ## Run the tests required in the Project specification
if __name__=="__main__":
srijakEliza like chatterbot in Python

Comments 1

Leave a Reply

Your email address will not be published. Required fields are marked *