Initial implementation #1

Merged
s444409 merged 2 commits from chatbot_initial_implementation into chatbot 2022-03-15 21:55:55 +01:00
Showing only changes of commit 70b370e64d - Show all commits

View File

@ -1,11 +1,14 @@
from ast import arg
from dataclasses import replace
import re import re
import random import random
ARG_LITERAL = '%' ARG_LITERAL = '%'
MAX_ARGS = 5
GOODBYES =["Do zobaczenia", "Elo"] GOODBYES =["Do zobaczenia", "Elo"]
QUIT = "wyjscie" QUIT = "wyjscie"
pairs = ( ('test (.*)', ('%1',)), ('test2 (.*)', ('%1 2',)) ) pairs = ( ('test (.*)', ('%1',)), ('(.*) test2 (.*)', ('%1 2 %2',)), ('test2 (.*)', ('%1 2',)) )
def main(): def main():
@ -24,7 +27,8 @@ def main():
continue continue
ans = pick_answer(pair) ans = pick_answer(pair)
print(substitute_arg(ans, search.groups())) print(replace_args(ans, search))
break;
print(random.choice(GOODBYES)) print(random.choice(GOODBYES))
@ -33,10 +37,24 @@ def should_quit(input):
return QUIT in input return QUIT in input
def substitute_arg(pattern, sub): def get_full_arg_str(arg_number):
# TODO: More than one argument support return ARG_LITERAL + str(arg_number)
arg = ARG_LITERAL + '1'
return pattern.replace(arg, sub[0])
def replace_args(pattern, match):
arg_number = 1
result = pattern
while arg_number <= MAX_ARGS:
if get_full_arg_str(arg_number) in result:
result = substitute_arg(result, match.group(arg_number), arg_number)
arg_number += 1
return result
def substitute_arg(pattern, sub, group_number):
arg = get_full_arg_str(group_number)
return pattern.replace(arg, sub)
def pick_answer(pair): def pick_answer(pair):