diff --git a/elisa_pl.py b/elisa_pl.py new file mode 100644 index 0000000..db0873c --- /dev/null +++ b/elisa_pl.py @@ -0,0 +1,79 @@ +from ast import arg +from dataclasses import replace +import re +import random + +ARG_LITERAL = '%' +MAX_ARGS = 5 +GOODBYES = ["Do zobaczenia", "Elo"] +QUIT = "wyjscie" + +pairs = ( + ('test (.*)', ('%1',)), + ('(.*) test2 (.*)', ('%1 2 %2',)), + ('test2 (.*)', ('%1 2',)), + ('dobrze|świetnie|swietnie|super', ('To świetnie! Opowiedz mi dlaczego', 'To bardzo dobrze! Z jakiego powodu?')), + ('źle|zle|do bani|słabo|slabo', ('Przykro mi. Czhesz o tym porozmawiać?', 'To bardzo dobrze! Z jakiego powodu?')), + ('tak|nie', ('ok', ':)', 'mhm', 'okej')), + ('lubi[eę] (.*)', ('Ja też! :D Co Ci się w tym najbardziej podoba?', 'Powiedz mi więcej o %1')), + ('marz[eę] o (.*)', ('Nie przejmój się, kiedyś się uda!', 'Hmm... A dlaczego akurat o %1?')), + ('powiedz (.*)', ('%1', '%1, nie jestem botem :/')), + ('(.*)', ('Hmm... Interesujące... Opowiedz mi więcej o %1', 'Powiedz mi więcej o %1')), # '(.*)' to jest odpowiedź na "wszystko", musi być na końcu listy! + ) + + +def main(): + is_running = True + while is_running: + user_input = input().lower() + + if should_quit(user_input): + is_running = False + continue + + for pair in pairs: + search = re.search(pair[0].lower(), user_input) + + if not search: + continue + + ans = pick_answer(pair) + print(replace_args(ans, search)) + break; + + print(random.choice(GOODBYES)) + + +def should_quit(input): + return QUIT in input + + +def get_full_arg_str(arg_number): + return ARG_LITERAL + str(arg_number) + + +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): + assert len(pair) == 2 + idx = random.randint(0, len(pair[1]) - 1) + return pair[1][idx] + + +if __name__ == "__main__": + print("Hej jestem Elize! Jak się czujesz?") + main()