47 lines
1.0 KiB
Python
47 lines
1.0 KiB
Python
import re
|
|
|
|
class Nlu:
|
|
def __init__(self):
|
|
self.acts = {
|
|
"request": {
|
|
'triggers': ['jak', 'kiedy'],
|
|
'parameters': ['imie']
|
|
}
|
|
}
|
|
|
|
|
|
def tokenize(self, string):
|
|
clean_string = self.get_str_cleaned(string)
|
|
|
|
return clean_string.split()
|
|
|
|
|
|
def get_str_cleaned(self, 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
|
|
|
|
def parse(self, message):
|
|
tokens = self.tokenize(message)
|
|
act = None
|
|
param = []
|
|
|
|
for k, v in self.acts.items():
|
|
if any(t in v['triggers'] for t in tokens):
|
|
act = k
|
|
|
|
for t in tokens:
|
|
if t in v['parameters']:
|
|
param.append(t)
|
|
|
|
# TODO: Refactor
|
|
|
|
|
|
return (act, param)
|
|
|