Python‎ > ‎

Chatbot


We will be using Object Oriented features of Python to create our basic chatbot

We will create the following Classes:

1. Avatar Class
This is a generic class that will have basic attributes and functions/methods/operations

Attributes:
  • myName 

Methods:
  • say 
    • Input parameters
      • words
      • printFlag?
  • listen
    • Input parameters
      • prompt
    • Return parameters
      • response

  • spell  #additional methods to pad out our Avatar
    • Input:
      • word
    • Output
      • say/print the letters of the word





# pip install pywin32
# pip install pyttsx3    supports up to Python 3.9

import pyttsx3


class Avatar:

    def initVoice(self):
        '''
        Method: Initialise Text to Speech
        '''
        self.__engine = pyttsx3.init()

##        ''' Set Voice '''
        self.__voices = self.__engine.getProperty('voices')
        self.__vix = 0  # Male, 1 Female
        self.__voice = self.__voices[self.__vix].id
        self.__engine.setProperty('voice'self.__voice)

        ''' Set Rate '''
        self.__engine.setProperty('rate'200)

        ''' Set Volume '''
        self.__engine.setProperty('volume'1.0)

    def __init__(selfname="Mary"):
        self.__name = name
        self.initVoice()

    def say(selfwords):
        self.say(words, True)

    def say(selfwordsprintFlag=True):
        if printFlag:
            print(words)
        self.__engine.say(words, self.getName())
        self.__engine.runAndWait()

    def setName(selfname):
        self.__name = name

    def getName(self):
        return self.__name

    def introduce(self):
        '''
        Avatar introduces themselves
        '''
        self.say(f"Hello. My name is {self.getName()}")

    def spell(selfword):
        for letter in word:
            self.say(letter)

    def listen(selfprompt="I am listening, please speak:"):
        words = input(prompt)
        return words

# Test harness that will only work as a standalone
def main():
    test = Avatar()
    test.introduce()
    test.say("Hello")
    test.spell("Hello")
    test.say(test.listen())

if __name__ == "__main__":
    main()









Comments