Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #============= OOP Dr.House Chat bot =============#
- import re as searcher
- def findMaxIndex(array_):
- maxNum = 0
- index = 0
- for i in range(len(array_)):
- if array_[i] > maxNum:
- maxNum = array_[i]
- index = i
- return index
- class ChatBot:
- def __init__(self, question):
- self.question = question
- file = open("ans.txt",'r')
- self.memory = file.read()
- self.rightAnswer =""
- def searchContent(self, mem, keyword):
- result = searcher.search(keyword, mem)
- if result is not None:
- return True
- return False
- def searchForAnswerInMemory(self):
- questionsArray = self.question.split(" ")
- rightCaseCount = []
- memSplit = self.memory.split(".")
- for mem in memSplit:
- add = 0
- for part in questionsArray:
- if self.searchContent(mem, part) == True:
- add += 1
- rightCaseCount.append(add)
- return rightCaseCount
- def mainChat(self):
- catcher = self.searchForAnswerInMemory()
- maxIndex = findMaxIndex(catcher)
- maxValue = max(catcher)
- print(catcher) #DeBUGGING
- if maxValue <= 2:
- print("Sry I have nothing for you !")
- answer = input("give me the answer -> ")
- file = open("ans.txt", 'a')
- file.write(answer)
- else:
- memSplit = self.memory.split(".")
- print(memSplit[maxIndex])
- #================= Main ==============#
- ques = "WELCOME"
- while True:
- ques = input("Question ->")
- if ques == "quit":
- break
- x = ChatBot(ques)
- x.mainChat()
Add Comment
Please, Sign In to add comment