+1 (315) 557-6473 

Chatbot Creation using Python Assignment Solution

Chatbot AI for the used cars

I. Build a Dictionary out of 5 common, simple questions and answers - for example, a chat bot for Slick Al's Used Cars! ( make sure your keys are simple ):

- (key): Guarantee?

(value): Sure - it's in the fine print of our contract

- (key): Financing?

(value): Louie the loan shark will be happy to work with you

- …

II. Create a List of generic responses to apply if the question (key) requested is not found in the above dictionary

- (element1): We have the best selection & prices, come on down

- (element2): Happy to talk to you more in person about your question.

III. Create a Class:

- In its own file create a class that models a Log of your chat. The attributes will be:

  • chat number
  • question
  • answer

IV. Create Functions:

- def createKnownResponses()

  • initialize & return your Dictionary
  • - def createUnknownResponses()

  • initialize & return your List
  • - def process(d, l):

  • Takes a dictionary and list as parameters
  • Create a list to hold unanswered questions ( note this will be declared before the loop )
  • Contains a Loop which gets a request from the user. If the request (key) is found in the Dictionary, return the value. If the request is not found, append this request to your list of unanswered questions. Next return a random element from the list.
  • Keep a count of how many iterations your run thru the list
  • Create a list ‘logHistory’ which will maintain a list of Log instances
  • For each iteration ( question asked / response provided ), create an instance of a

    Log object & add this to your logHistory along with the count

  • Provide some utility to exit the program (i.e. type ‘exit’ to quit)
    • When the user signals exit, first call your runReport() method (below)
    • To print the ‘transcript’. Next, call your questionsToLearn() method (make sure to pass in your unansweredQuestions list) so you’ll have a list of questions to provide answers for in the future. Finally, exit the program.

      - def runReport(logHistory):

      • Takes a list of Log instances as a parameter
      • Iterates thru the list printing a history (‘transcript’) of the chat conversation

Example ‘transcript’:

Chat number[1] Question: 'test question 1' Answer: {['test answer 1']}

Chat number[2] Question: 'is the moon made of green cheese' Answer: {random answer 3}

Chat number[3] Question: 'test question 2' Answer: {['test answer 2']} Chat number[4] Question: 'is lasagna yummy' Answer: {random answer 3}

- def questionsToLearn(unansweredQuestionList):

  • Takes a list of unanswered questions as a parameter
  • Write the list to a file
    • So you could potentially understand which questions to add to your dictionary were you to enhance your bot.
    • - You’re free to create more functions, but the requirement is to at least create these 5

V. Create a main() method

      - Call your createKnownResponses()and save the value returned as a variable in your main()

      - Call your createUnknownResponses()and save the value returned as a variable in your main()

      - Call your process(d, l)method, passing your dictionary and list as arguments.

      • Note, process() will call runReport() and questions to learn() when the user signals exit ( call the methods 1st then exit ).
      • - Call the main using the standard if main()


from random import choice def createKnownResponses(): questions = {"sex" : "Subscribe to HBO.", "children": "Subscribe Disney+ and Nickelodeon.", "new": "Included in all the basic packages.", "package": "Basic: $30, all network channels\n"+ "Family: $50, all the Basic channels plus Cartoon Network, SyFy\n"+ "Movie: $80, all the channels from the Family pack plus Showtime, and TMC.", "order": "OK. We will be happy to install just select a date.", "bundle": "We can include phone, and internet for only a small amount more."} return questions def createUnknownResponses(): generic = ["Ask us about the packages.", "We offer bundles with phone and internet as well.", "100 channels, and nothing on? You are not looking hard enough.", "We habla Espanol.", "Call in to our office for personal service.", "Thanks for visiting our web site.", "When TikTok runs out of content."] return generic def process(d, l): questions = [] unanswered = [] q = 1 while True: # Get the question question = input("> (quit): ") if question == "quit": break # Get each word in the question and remove punctuation and s (so packages and package are the same) # from the end of the word for word in question.lower().split(): word = word.rstrip(",.?s") if word in d: answer = d[word] break else: # We didn't find an answer, so give a generic response answer = choice(l) unanswered.append(Question(q, question, answer)) # Display the answer chosen (either random or from the fixed responses) print(answer) questions.append(Question(q, question, answer)) q += 1 # Display all the questions asked runReport(questions) # Output to a file all the ones with generic response questionsToLearn(unanswered) def runReport(questions): # Loop through each question for question in questions: print(question) def questionsToLearn(questions): with open("unanswered.txt", "w") as f: # Loop through each question for question in questions: # Print to a file rather than the screen print(question, file=f) class Question: def __init__(self, id, question, answer): self.id = id self.question = question self.answer = answer def __str__(self): return f"Chat number[{self.id}] Question:{self.question} Answer:{self.answer}" def main(): print("Welcome to TV Guide Chatbot") questions = createKnownResponses() generic = createUnknownResponses() process(questions, generic) if __name__ == "__main__": main()