2024-04-16 12:28:58 +02:00
from typing import Any
2024-04-23 15:28:20 +02:00
import jsgf
2024-05-08 17:56:59 +02:00
from unidecode import unidecode
import string
2024-04-23 15:28:20 +02:00
import argparse
2024-04-16 12:28:58 +02:00
class Model ( ) :
def __init__ ( self ) :
pass
def __call__ ( self , prompt ) - > Any :
nlu = NLU ( )
dst = DST ( )
dp = DP ( )
nlg = NLG ( )
2024-04-23 15:28:20 +02:00
msg = prompt . lower ( )
2024-04-16 12:28:58 +02:00
rama_nlu = nlu ( msg )
2024-04-23 15:28:20 +02:00
print ( rama_nlu )
2024-04-16 12:28:58 +02:00
rama_dst = dst ( rama_nlu )
rama_dp = dp ( rama_dst )
text = nlg ( rama_dp )
return text
class NLU ( ) :
def __init__ ( self ) :
2024-05-05 16:54:03 +02:00
self . book_grammar = jsgf . parse_grammar_file ( ' book.jsgf ' )
2024-04-16 12:28:58 +02:00
2024-04-23 15:28:20 +02:00
def get_dialog_act ( self , rule ) :
slots = [ ]
self . get_slots ( rule . expansion , slots )
2024-05-08 17:56:59 +02:00
return { ' act ' : rule . name , ' slots ' : slots }
2024-04-23 15:28:20 +02:00
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 )
2024-04-16 12:28:58 +02:00
def __call__ ( self , prompt ) - > Any :
2024-05-08 17:56:59 +02:00
book_grammar = jsgf . parse_grammar_file ( ' book.jsgf ' )
prompt = unidecode ( prompt )
translator = str . maketrans ( ' ' , ' ' , string . punctuation )
prompt = prompt . translate ( translator )
matched = book_grammar . find_matching_rules ( prompt )
2024-04-23 15:28:20 +02:00
if matched :
return self . get_dialog_act ( matched [ 0 ] )
else :
return { ' act ' : ' null ' , ' slots ' : [ ] }
2024-04-16 12:28:58 +02:00
class DST ( ) :
def __init__ ( self ) :
self . msgs = [ ]
def __call__ ( self , msg ) - > Any :
self . msgs . append ( msg )
return msg
class DP ( ) :
def __init__ ( self ) :
pass
def __call__ ( self , msg ) - > Any :
if " imie " in msg :
return " imieMSG "
else :
return None
class NLG ( ) :
def __init__ ( self ) :
pass
def __call__ ( self , msg ) - > Any :
if msg == " imieMSG " :
return " Mam na imie JARVIS "
else :
return " Nie rozumiem "
if __name__ == " __main__ " :
model = Model ( )
2024-05-08 17:56:59 +02:00
# parser = argparse.ArgumentParser()
# parser.add_argument("--msg")
# args = parser.parse_args()
2024-05-14 11:36:24 +02:00
# print(model(prompt="chcialbym zarezerwowac stolik na jutro na dziesiata dla trzech osob"))
# print(model(prompt="Cześć"))
# print(model(prompt="Hej, jakim botem jesteś?"))
# print(model(prompt="Hej, w czym mi możesz pomóc?"))
# print(model(prompt="Siema, w czym możesz mi pomóc?"))
# print(model(prompt="Witam"))
# print(model(prompt="Witam system"))
# print(model(prompt="Hej, czym się zajmujesz?"))
# print(model(prompt="Czesc, jestem agentem dialogowym przyjmujacym zamowienia w restauracji. Moge doradzic ci w wyborze odpowiedniej pozycji z menu. W czym moge ci pomoc?"))
# print(model(prompt="oki"))
# print(model(prompt="Potwierdzam!"))
# print(model(prompt="Tak!"))
# print(model(prompt="Tak to wszystko!"))
2024-05-14 11:59:25 +02:00
print ( model ( prompt = " Interesuja mnie dania kuchni woskiej oraz meksykanskiej " ) )
print ( model ( prompt = " Poproszę ryba " ) )
print ( model ( prompt = " Poproszę tatara " ) )
print ( model ( prompt = " Poproszę 2 porcje " ) )
print ( model ( prompt = " Zaplacę kartą przy odbiorze " ) )
print ( model ( prompt = " Dobrze, nie mogę sie już doczekać " ) )
2024-05-14 11:56:17 +02:00
print ( model ( prompt = " uniwersytetu poznanskiego 4 61-614 poznan " ) )
2024-05-14 11:59:25 +02:00
print ( model ( prompt = " dziekuje to wszystko " ) )