Spaces:
Sleeping
Sleeping
import re | |
import config.long_responses as long | |
from config.valid_text import is_valid_input | |
def message_probability( | |
user_message, recognised_words, | |
single_response=False, required_words=None | |
): | |
if required_words is None: | |
required_words = [] | |
message_certainty = 0 | |
has_required_words = True | |
# Counts how many words are present in each predefined message | |
for word in user_message: | |
if word in recognised_words: | |
message_certainty += 1 | |
# Calculates the percent of recognised words in a user message | |
percentage = float(message_certainty) / float(len(recognised_words)) | |
# Checks that the required words are in the string | |
for word in required_words: | |
if word not in user_message: | |
has_required_words = False | |
break | |
# Must either have the required words, or be a single response | |
if has_required_words or single_response: | |
return int(percentage * 100) | |
else: | |
return 0 | |
def check_all_messages(message): | |
highest_prob_list = {} | |
# Simplifies response creation / adds it to the dict | |
def response(bot_response, list_of_words, single_response=False, required_words=[]): | |
nonlocal highest_prob_list | |
highest_prob_list[bot_response] = message_probability(message, list_of_words, single_response, required_words) | |
# Responses ------------------------------------------------------------------------------------------------------- | |
response("بله", ["بله", "بلی", "بلخ", "یله", "پله", "gfi", | |
"آره", "آری", "آرخ", "yes", "ya", | |
"bale", "ari", "are", "fgi", "hvi", | |
"Hvd", "Hvd", "fgd", "hsj", "isj", | |
"nhvn", "هست", "است", "دارد", | |
"یس",], single_response=True) | |
response("خیر", [ | |
"نه", "منفی", "odv", "kodv", "خیر", "no", | |
"ni", "نخیر", "نیست", "نبود", "نمیشود", | |
"نمی شود", "نمی\u200cشود", "نیست", | |
"ندیدم", "دخ", "nist", "nabod", "na", | |
"nah", "noch", "nuch", "nooch", "manfi", | |
], single_response=True) | |
# Longer responses | |
response(long.R_L01, ["اختلالات", "حرکتی", "Movement", "disorders","حرکت"], | |
required_words=["حرکت"]) | |
response(long.R_L02, ["تعمیم یافته", "صورت", "گردن", "اندامهای", "رقصاک", "فوقانی", "تحتانی", "تنه"], | |
required_words=["رقصاک"]) | |
response(long.R_L03, ["چشمی", "عصبی", "حرکت" | |
], required_words=["عصبی"]) | |
response(long.R_L04, ["بیماری", "عصبی", "متا", "بولیکی", "نورومتابولیکی" | |
], required_words=["عصبی"]) | |
response(long.R_L05, ["روانپزشکی", "روانپریشی", "روان", "پزشکی", "روانپزشکی" | |
], required_words=["روانپزشکی"]) | |
response(long.R_L06, ["هیپرهموسیستئینمی", "hyperhomocysteinemia","ژنتیکی" | |
], required_words=["ژنتیکی"]) | |
response(long.R_L07, ["درگیری", "طناب", "نخاعی", "spinal", "cord", "نخاعی" | |
], required_words=["نخاعی"]) | |
response(long.R_L08, ["کوبالامین", "داخل", "سلولی", "intracellular", "cobalamin","سلولی" | |
], required_words=["سلولی"]) | |
response(long.R_L09, ["لیپاز", "اسید", "لیزوزومال", "LAL-D", "کمبود" | |
], required_words=["کمبود"]) | |
response(long.R_L10, ["هیدرولیپوآمید", "دهیدروژناز", "کمبود" | |
], required_words=["کمبود"]) | |
response(long.R_L11, ["فسفاتمی","هایپوفسفاتمی", "هیپوفسفاتمی", "hypophosphatemia", "HP", "ژنتیکی" | |
], required_words=["ژنتیکی"]) | |
response(long.R_L12, ["آنسفالوپاتی", "هیپرآمونمیک", "غیر", "کبدی", "جراحی", "چاقی", "Nonhepatic", "hyperammonemic", "encephalopathy", "bariatric", "surgery", "NHE-BS" | |
], required_words=["کبدی"]) | |
response(long.R_L13, ["لرزش", "میتو", "کندری", "میتوکندری", "ژنتیکی" | |
], required_words=["ژنتیکی"]) | |
response(long.R_L13, ["اندام", "لرزش", "ترمور", "Tremor", "TRM", "عصبی" | |
], required_words=["عصبی"]) | |
# | |
# | |
# "اختلالات حرکتی": "L01", | |
# " رقصاک تعمیم یافته در صورت ، گردن ، اندامهای فوقانی/تحتانی ، تنه ": "L02", | |
# "اختلالات حرکات چشم و علائم عصبی": "L03", | |
# "بیماریهای عصبی": "L04", | |
# "هوموستئینمی شدید": "L06", | |
# "تظاهرات روانی": "L05", | |
# "درگیری نخاع": "L07", | |
# "متابولیسم کبدی داخل سلولی": "L08", | |
# "کمبود لیپاز اسید لیزوزومی": "L09", | |
# "کمبود دی هیدرو لیپوامید دی هیدروژناز": "L10", | |
# | |
# | |
# "فسفاتمی": "L11", | |
# "عوارض" ,"آنسفالوپاتی", "غیرکبدی" , "جراحی", "باریاتریک": "L12", | |
# "لرزش اندام فوقانی": "L13", | |
# "بیماریهای میتوکندری": "L14", | |
# | |
# | |
# | |
# "کمبود", "هیدرولیپوآمید", "دهیدروژناز" | |
# "هایپوفسفاتمی", "هیپوفسفاتمی", "hypophosphatemia", "HP" | |
# "آنسفالوپاتی", "هیپرآمونمیک", "غیر", "کبدی", "جراحی", "چاقی", "Nonhepatic", "hyperammonemic", "encephalopathy", "bariatric", "surgery", "NHE-BS | |
# "لرزش", "ترمور", "Tremor", "لرزش", "TRM" | |
best_match = max(highest_prob_list, key=highest_prob_list.get) | |
# print(highest_prob_list) | |
# print(f'Best match = {best_match} | Score: {highest_prob_list[best_match]}') | |
return long.unknown() if highest_prob_list[best_match] < 1 else best_match | |
# Used to get the response | |
def get_response(user_input): | |
split_message = re.split(r'\s+|[,;?!.-]\s*', user_input.lower()) | |
response = check_all_messages(split_message) | |
return response | |
# | |
# # Testing the response system | |
# while True: | |
# print('Bot: ' + get_response(input('You: '))) |