From 7f57db16063137bdc4903193de763aea00b9b2b4 Mon Sep 17 00:00:00 2001 From: Iwona Christop Date: Tue, 19 Apr 2022 19:09:41 +0200 Subject: [PATCH] Add NLU --- src/components/NLU.py | 48 +++++++++++++++++++++--------------------- src/dialogue_system.py | 4 +++- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/components/NLU.py b/src/components/NLU.py index de7cb49..f28aa54 100644 --- a/src/components/NLU.py +++ b/src/components/NLU.py @@ -1,27 +1,27 @@ -# Iwona +from jsgf import PublicRule, Grammar +import re + class NLU: - # add slots + def get_str_cleaned(str_dirty): + punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\\\]^_`{|}~' + new_str = str_dirty.lower() + new_str = re.sub(' +', ' ', new_str) + for char in punctuation: + new_str = new_str.replace(char,'') + return new_str - userSpeechActs = { - 'hello': None, - 'null': None, - 'inform': None, - 'ack': None, - } + def getDialogAct(rule): + slots = [] + return {'act': rule.grammar.name, 'slots': slots} - def getUserActs(self, userInput): - resultUserActs = [] - - # split user input - userInput = userInput.lower().split() - - # find key words - if "cześć" in userInput: - resultUserActs.append(self.userSpeechActs[0]) - if "imię" in userInput: - resultUserActs.append(self.userSpeechActs[0]) - - - # returns user speech act - return # wyobrażam sobie to jako słownik list krotek UwU {inform:[('name','edyta'), ('age','18')], reqmore:[date,time]} - #słownik słowników???? {inform:{'name':'edyta', 'age':18}, reqmore:{'date','time'}} albo słownik słowników/zbiorów bardziej pasuje \ No newline at end of file + def nlu(utterance): + hello = Grammar('hello') + hello.add_rule(PublicRule('witaj', 'cześć jak masz na imię')) + + utterance = NLU.get_str_cleaned(utterance) + + matched = hello.find_matching_rules(utterance) + if matched: + return NLU.getDialogAct(matched[0]) + else: + return {'act': 'null', 'slots': []} \ No newline at end of file diff --git a/src/dialogue_system.py b/src/dialogue_system.py index 05cb10a..6ce7295 100644 --- a/src/dialogue_system.py +++ b/src/dialogue_system.py @@ -1,5 +1,7 @@ +from components.NLU import NLU + def generate_response(input): - result = "pass" + result = NLU.nlu(input) return result inputText = 'Cześć, jak masz na imię?'