n0va_sa

OOP Dr.House Chat bot

May 23rd, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. #============= OOP Dr.House Chat bot =============#
  2. import re as searcher
  3. def findMaxIndex(array_):
  4.     maxNum = 0
  5.     index = 0
  6.     for i in range(len(array_)):
  7.         if array_[i] > maxNum:
  8.             maxNum = array_[i]
  9.             index = i
  10.     return index
  11. class ChatBot:
  12.     def __init__(self, question):
  13.         self.question = question
  14.         file = open("ans.txt",'r')
  15.         self.memory = file.read()
  16.         self.rightAnswer =""
  17.     def searchContent(self, mem, keyword):
  18.         result = searcher.search(keyword, mem)
  19.         if result is not None:
  20.             return True
  21.         return False
  22.     def searchForAnswerInMemory(self):
  23.         questionsArray = self.question.split(" ")
  24.         rightCaseCount = []
  25.         memSplit = self.memory.split(".")
  26.         for mem in memSplit:
  27.             add = 0
  28.             for part in questionsArray:
  29.                 if self.searchContent(mem, part) == True:
  30.                     add += 1
  31.             rightCaseCount.append(add)
  32.  
  33.         return rightCaseCount
  34.     def mainChat(self):
  35.         catcher = self.searchForAnswerInMemory()
  36.         maxIndex = findMaxIndex(catcher)
  37.         maxValue = max(catcher)
  38.         print(catcher) #DeBUGGING
  39.         if maxValue <= 2:
  40.             print("Sry I have nothing for you !")
  41.             answer = input("give me the answer -> ")
  42.             file = open("ans.txt", 'a')
  43.             file.write(answer)
  44.         else:
  45.             memSplit = self.memory.split(".")
  46.             print(memSplit[maxIndex])
  47.  
  48. #================= Main ==============#
  49. ques = "WELCOME"
  50. while True:
  51.     ques = input("Question ->")
  52.     if ques == "quit":
  53.         break
  54.     x = ChatBot(ques)
  55.     x.mainChat()
Add Comment
Please, Sign In to add comment