rules
This commit is contained in:
parent
40443cbfa6
commit
c95b8f8896
@ -1,7 +1,9 @@
|
|||||||
import re
|
import re
|
||||||
|
import jsgf
|
||||||
|
|
||||||
class Nlu:
|
class Nlu:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self.rules_grammar = jsgf.parse_grammar_file('rules.jsgf')
|
||||||
self.acts = {
|
self.acts = {
|
||||||
"request": {
|
"request": {
|
||||||
'triggers': ['jak', 'kiedy'],
|
'triggers': ['jak', 'kiedy'],
|
||||||
@ -10,10 +12,32 @@ class Nlu:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_slots(self, expansion, slots):
|
||||||
|
if expansion.tag != '':
|
||||||
|
slots.append((expansion.tag, expansion.current_match))
|
||||||
|
return
|
||||||
|
|
||||||
|
for child in expansion.children:
|
||||||
|
self.get_slots(child, slots)
|
||||||
|
|
||||||
|
if not expansion.children and isinstance(expansion, jsgf.NamedRuleRef):
|
||||||
|
self.get_slots(expansion.referenced_rule.expansion, slots)
|
||||||
|
|
||||||
|
def get_dialog_act(self, rule):
|
||||||
|
slots = []
|
||||||
|
self.get_slots(rule.expansion, slots)
|
||||||
|
return {'act': rule.grammar.name, 'slots': slots}
|
||||||
|
|
||||||
|
|
||||||
def tokenize(self, string):
|
def tokenize(self, string):
|
||||||
clean_string = self.get_str_cleaned(string)
|
clean_string = self.get_str_cleaned(string)
|
||||||
|
|
||||||
return clean_string.split()
|
matched = self.rules_grammar.find_matching_rules(clean_string)
|
||||||
|
|
||||||
|
if matched:
|
||||||
|
return self.get_dialog_act(matched[0])
|
||||||
|
else:
|
||||||
|
return {'act': 'null', 'slots': []}
|
||||||
|
|
||||||
|
|
||||||
def get_str_cleaned(self, str_dirty):
|
def get_str_cleaned(self, str_dirty):
|
||||||
@ -44,3 +68,5 @@ class Nlu:
|
|||||||
|
|
||||||
return (act, param)
|
return (act, param)
|
||||||
|
|
||||||
|
nlu = Nlu()
|
||||||
|
print(nlu.parse('chciałbym kupić bilet do Krakow'))
|
30
trailminator/rules.jsgf
Normal file
30
trailminator/rules.jsgf
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#JSGF V1.0 UTF-8 pl;
|
||||||
|
|
||||||
|
grammar rules;
|
||||||
|
|
||||||
|
|
||||||
|
public <rezerwuj> = chciałbym kupić bilet [na pociąg] [z <stacja_poczatkowa>] [(do <stacja_koncowa>)];
|
||||||
|
<stacja_poczatkowa> = <stacja> {stacion};
|
||||||
|
<stacja_koncowa> = <stacja> {stacion};
|
||||||
|
<stacja> = Poznan | Warszawa | Wroclaw | Krakow | Gdansk;
|
||||||
|
|
||||||
|
|
||||||
|
public <podaj_date> = [w] [najbliższy | najbliższą] {okres} <dzien> {day};
|
||||||
|
<dzien> = dzisiaj | jutro | poniedziałek | wtorek | środę | czwartek | piątek | sobotę | niedzielę;
|
||||||
|
|
||||||
|
|
||||||
|
public <liczba> = (0|1|2|3|4|5|6|7|8|9);
|
||||||
|
public <podaj_liczbe> = (<liczba>+){number};
|
||||||
|
|
||||||
|
|
||||||
|
public <discount> = [<liczba>] bilet[y] z ulgą <discount_type>{discount_type};
|
||||||
|
<discount_type> = studencką | seniorską | uczniowską;
|
||||||
|
|
||||||
|
|
||||||
|
public <seatType> = [poproszę] <seat_window>{seat_window} | <seat_middle>{seat_middle};
|
||||||
|
<seat_window> = [miejsce] pod oknem;
|
||||||
|
<seat_middle> = [miejsce] w środku | [miejsce] w przejściu;
|
||||||
|
|
||||||
|
|
||||||
|
public <wagonType> = [klasy | klasa] <class>{wagonType};
|
||||||
|
<class> = pierwsza | druga | 1 | 2 | I | II;
|
Loading…
Reference in New Issue
Block a user