File size: 6,298 Bytes
77801a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0c7208
77801a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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: ')))