Spaces:
Sleeping
Sleeping
import re | |
import random | |
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 | |
for word in user_message: | |
if word in recognised_words: | |
message_certainty += 1 | |
percentage = float(message_certainty) / float(len(recognised_words)) | |
for word in required_words: | |
if word not in user_message: | |
has_required_words = False | |
break | |
if has_required_words or single_response: | |
return int(percentage * 100) | |
else: | |
return 0 | |
def unknown(): | |
response = ["Could you please re-phrase that? ", | |
"...", | |
"Sounds about right.", | |
"What does that mean?"][ | |
random.randrange(4)] | |
return response | |
def check_all_messages(message): | |
highest_prob_list = {} | |
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("بله", ["دارد", "نشانه", "علایم", "علائم", "بله"], required_words=["بله"]) | |
response("خیر", ["ندارد", "نشانهای", "علایمی", "خیر"], required_words=["خیر"]) | |
response("L99", ["تمام", "خروج", "پایان", "l99"], required_words=["پایان"]) | |
response("L01", ["اختلالات", "حرکتی", "Movement", "disorders","حرکت"], | |
required_words=["حرکت"]) | |
response("L02", ["تعمیم یافته", "صورت", "گردن", "اندامهای", "رقصاک", "فوقانی", "تحتانی", "تنه"], | |
required_words=["رقصاک"]) | |
response("L03", ["چشمی", "عصبی", "حرکت" | |
], required_words=["عصبی"]) | |
response("L04", ["بیماری", "عصبی", "متا", "بولیکی", "نورومتابولیکی" | |
], required_words=["عصبی"]) | |
response("L05", ["روانپزشکی", "روانپریشی", "روان", "پزشکی", "روانپزشکی" | |
], required_words=["روانپزشکی"]) | |
response("L06", ["هیپرهموسیستئینمی", "hyperhomocysteinemia","ژنتیکی" | |
], required_words=["ژنتیکی"]) | |
response("L07", ["درگیری", "طناب", "نخاعی", "spinal", "cord", "نخاعی" | |
], required_words=["نخاعی"]) | |
response("L08", ["کوبالامین", "داخل", "سلولی", "intracellular", "cobalamin","سلولی" | |
], required_words=["سلولی"]) | |
response("L09", ["لیپاز", "اسید", "لیزوزومال", "LAL-D", "کمبود" | |
], required_words=["کمبود"]) | |
response("L10", ["هیدرولیپوآمید", "دهیدروژناز", "کمبود" | |
], required_words=["کمبود"]) | |
response("L11", ["فسفاتمی","هایپوفسفاتمی", "هیپوفسفاتمی", "hypophosphatemia", "HP", "ژنتیکی" | |
], required_words=["ژنتیکی"]) | |
response("L12", ["آنسفالوپاتی", "هیپرآمونمیک", "غیر", "کبدی", "جراحی", "چاقی", "Nonhepatic", "hyperammonemic", "encephalopathy", "bariatric", "surgery", "NHE-BS" | |
], required_words=["کبدی"]) | |
response("L13", ["لرزش", "میتو", "کندری", "میتوکندری", "ژنتیکی" | |
], required_words=["ژنتیکی"]) | |
response("L14", ["اندام", "لرزش", "ترمور", "Tremor", "TRM" | |
], required_words=["عصبی"]) | |
best_match = max(highest_prob_list, key=highest_prob_list.get) | |
print(f'Best match = {best_match} | Score: {highest_prob_list[best_match]}') | |
return unknown() if highest_prob_list[best_match] < 1 else best_match | |
def replace_synonyms(sentence): | |
synonyms_dict = { | |
"بله": [ | |
"بله", "بلی", "بلخ", "یله", "پله", | |
"آره", "آری", "آرخ", "yes", "ya", "bale", "ari", | |
"are", "fgi", "hvi", "Hvd", "Hvd", "fgd", "hsj", | |
"isj", "nhvn", "هست", "است", "دارد", "یس", "nhavd", | |
"هست", "احتمال دارد", "امکان دارد", "میتواند", "شاید", | |
"احتمال مثبت", "باشد", "می دهم", "یس", "yes", "yup", | |
"چک کن", "مشکوک", "دارد" | |
], | |
"خیر": [ | |
"نه", "منفی", "odv", "kodv", "خیر", "no", "ni", "نخیر", | |
"نیست", "نبود", "نمیشود", "نمی شود", "نمی\u200cشود", "نیست", | |
"ندیدم", "دخ", "nist", "nabod", "na", "nah", "noch", "nuch", | |
"nooch", "manfi", "udv" | |
], | |
} | |
reverse_dict = {} | |
for key, synonyms in synonyms_dict.items(): | |
for synonym in synonyms: | |
reverse_dict[synonym] = key | |
regex_pattern = r'\b(' + '|'.join(map(re.escape, reverse_dict.keys())) + r')\b' | |
def replace(match): | |
return reverse_dict[match.group(0)] | |
result = re.sub(regex_pattern, replace, sentence) | |
print(result) | |
return result | |
# Used to get the response | |
def get_response(user_input): | |
user_input = replace_synonyms(user_input) | |
split_message = re.split(r'\s+|[,;?!.-]\s*', user_input.lower()) | |
response = check_all_messages(split_message) | |
return response |