-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (40 loc) · 1.9 KB
/
main.py
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
# load my own library
from wn_integration import *
from utils import *
# Library used to build the chatbot
from programy.clients.embed.configfile import EmbeddedConfigFileBot
def main():
chatbot = EmbeddedConfigFileBot("y-bot/config.yaml")
# use a user for testing
client_context = chatbot.create_client_context("testuser")
print( "\033[94m> Guido: \033[0m{}".format(chatbot.process_question(client_context, "Hi")))
while True:
# get the message from the user and let the chatbot digests it
message = input("\033[95m> You: \033[0m")
# split the message and possibily learn new patterns
split_and_learn_sentences(chatbot, client_context, message)
# get response from the chatbot
response = chatbot.process_question(client_context, message)
if response:
# in any case print the response
print("\033[94m> Guido: \033[0m{}".format(response))
print()
# message to exit
if response == CategoriesOfInterest.quit_pattern:
break
# it splits the message according to the splitter of the chatbot
# and it check if each chunck can be learned as new pattern
def split_and_learn_sentences(chatbot, client_context, message):
pre_processed_sentence = client_context.bot.pre_process_text(client_context, message, srai=False)
# split the sentence into smaller sentences according to the punctuation
senteces_list = client_context.bot.sentence_splitter.split(pre_processed_sentence)
for sentence in senteces_list:
response = chatbot.process_question(client_context, sentence)
# if response is not None
if response:
# if no pattern matches it may be applied a learning
if not_recognise(response):
learn_pattern(chatbot, client_context, sentence)
# run main
if __name__ == "__main__":
main()