Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
INTELLIGENT-CHATBOT-FOR-ARTIFICIAL-INTELLIGENCE/CHATBOT.PY
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
95 lines (63 sloc)
3.72 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import random | |
# this code function is to test the response system | |
def undisclosed(): | |
response = ["Could you please paraphrase that? ", | |
"...", | |
"sorry can you say that again.", | |
"i'm not sure i understand what you are saying", | |
"What does that mean?"][ | |
random.randrange(4)] | |
return response | |
# this code function is to get response | |
def input_responses(person_input): | |
message = re.split(r'\s+|[,;?!.-]\s*', person_input.lower()) | |
response = view_messages(message) | |
return response | |
def communication_possibility(person_message, observed_words, response=False, essential_words=[]): | |
communication_certitude = 0 | |
has_essential_words = True | |
# this checks how many words are in a predetermined message | |
for word in person_message: | |
if word in observed_words: | |
communication_certitude += 1 | |
# this code computes the percent of essential words in person message | |
ScalePercentage= float(communication_certitude) / float(len(observed_words)) | |
# this code checks for essential words in the string | |
for word in essential_words: | |
if word not in person_message: | |
has_essential_words = False | |
break | |
# this code function is to make it have essential words or give a response | |
if has_essential_words or response: | |
return int(ScalePercentage * 100) | |
else: | |
return 0 | |
def view_messages(text_message): | |
PossibilityList = {} | |
# | |
def pattern_response(chabot_response, list_of_words, response=False, essential_words=[]): | |
nonlocal PossibilityList | |
PossibilityList[chabot_response] = communication_possibility(text_message, list_of_words, response, essential_words) | |
#this code below gives an intelligent answer to a Person | |
pattern_response('Hello!', ['hello', 'hi', 'holla', 'whats up', 'holla'], response=True) | |
pattern_response('From your evaluated data you only have one exercise to do today', ['how', 'much'], response=True) | |
pattern_response('Watching tv all day is not good for the eyesight', ['all day', 'tv'], response=True) | |
pattern_response('Running is the best exercise for burning calories', ['burns', 'most calories'], response=True) | |
pattern_response('Like running you can do other exercise like weightlifting, swimming and boxing', ['recommend', 'any other'],response=True) | |
pattern_response('I do not have an age but my creator Seni is 30 years old', ['how', 'old', 'are', 'you', 'age'], response=True) | |
pattern_response('See you!', ['bye', 'goodbye', 'have a good day'], response=True) | |
pattern_response('hey bud sorry to hear that you feel that way, i am sure you are gonna get better soon', ['sad', 'unhappy'], response=True) | |
pattern_response('hey bud glad to hear that you feel that way, i am having a good day as well:)', ['happy', 'feeling good'],response=True) | |
pattern_response('I am good, and you?', ['how', 'are', 'you', 'doing'], essential_words=['how']) | |
pattern_response('yes, i require you try PureFitnessCenter! it is the best i know for exercise! ', ['gym'], response=True) | |
pattern_response('You\'re welcome!', ['thank', 'thanks'], response=True) | |
pattern_response('My name is 247psychobot', ['name'],response=True) | |
pattern_response('You are welcome', ['thank', 'you'], essential_words=['thank']) | |
PerfectMatch = max(PossibilityList, key=PossibilityList.get) | |
return undisclosed() if PossibilityList [PerfectMatch]< 1 else PerfectMatch | |
#this works except it puts user name in this format => {'name'} when running the code | |
userName = input('Greetings! my name is 247pyschobot, whats yours? : ' ) | |
f"Hello{userName}" | |
while True: | |
print('247psychobot: ' + input_responses(input(userName + ": "))) |