Merge branch 'WIP'
This commit is contained in:
commit
5984eb96e6
@ -1,9 +1,9 @@
|
|||||||
import jsgf
|
#import jsgf
|
||||||
|
|
||||||
from Modules.NLG_module import NLG
|
from Modules.NLG_module import NLG
|
||||||
from Modules.DP_module import DP
|
from Modules.DP_module import DP
|
||||||
from Modules.DST_module import Rules_DST
|
from Modules.DST_module import Rules_DST
|
||||||
from Modules.Book_NLU_module import Book_NLU
|
# from Modules.Book_NLU_module import Book_NLU
|
||||||
from Modules.ML_NLU_module import ML_NLU
|
from Modules.ML_NLU_module import ML_NLU
|
||||||
|
|
||||||
import random
|
import random
|
||||||
@ -23,13 +23,13 @@ class Janet:
|
|||||||
self.nlg = NLG()
|
self.nlg = NLG()
|
||||||
self.dp = DP()
|
self.dp = DP()
|
||||||
self.dst = Rules_DST()
|
self.dst = Rules_DST()
|
||||||
self.nlu = Book_NLU(jsgf.parse_grammar_file('book.jsgf'))
|
#self.nlu = Book_NLU(jsgf.parse_grammar_file('book.jsgf'))
|
||||||
self.nlu_v2 = ML_NLU()
|
self.nlu_v2 = ML_NLU()
|
||||||
|
|
||||||
def process(self, command):
|
def process(self, command):
|
||||||
act = self.nlu_v2.test_nlu(command)
|
act = self.nlu_v2.test_nlu(command)
|
||||||
return self.dp.predict(self.dst.update(act))
|
system_acts = self.dp.predict(self.dst.update_user(act))
|
||||||
#return self.nlg.change_to_text(dest_act)
|
return self.nlg.change_to_text(system_acts)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -1,7 +1,4 @@
|
|||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
import copy
|
|
||||||
import json
|
|
||||||
from copy import deepcopy
|
|
||||||
|
|
||||||
class DP():
|
class DP():
|
||||||
"""
|
"""
|
||||||
@ -15,16 +12,16 @@ class DP():
|
|||||||
|
|
||||||
def predict(self, state):
|
def predict(self, state):
|
||||||
self.results = []
|
self.results = []
|
||||||
system_action = defaultdict(list)
|
system_action = defaultdict(dict)
|
||||||
user_action = defaultdict(list)
|
user_action = defaultdict(list)
|
||||||
|
system_acts = []
|
||||||
for intent, domain, slot, value in state['user_action']:
|
while len(state['user_action']) > 0:
|
||||||
|
intent, domain, slot, value = state['user_action'].pop(0)
|
||||||
user_action[(domain, intent)].append((slot, value))
|
user_action[(domain, intent)].append((slot, value))
|
||||||
|
|
||||||
for user_act in user_action:
|
for user_act in user_action:
|
||||||
self.update_system_action(user_act, user_action, state, system_action)
|
system_acts.append(self.update_system_action(user_act, user_action, state, system_action))
|
||||||
|
|
||||||
system_acts = [[intent, domain, slot, value] for (domain, intent), slots in system_action.items() for slot, value in slots]
|
|
||||||
state['system_action'] = system_acts
|
state['system_action'] = system_acts
|
||||||
return system_acts
|
return system_acts
|
||||||
|
|
||||||
@ -50,14 +47,14 @@ class DP():
|
|||||||
elif intent == 'appointment/create_appointment':
|
elif intent == 'appointment/create_appointment':
|
||||||
for x in constraints:
|
for x in constraints:
|
||||||
if(x[0] == 'doctor'):
|
if(x[0] == 'doctor'):
|
||||||
system_action[(domain, 'book_appointent')].append([x[0], x[1]])
|
system_action[(domain, 'book_appointent')][x[0]] = x[1]
|
||||||
for y in constraints:
|
for y in constraints:
|
||||||
if(y[0] == 'datetime'):
|
if(y[0] == 'datetime'):
|
||||||
system_action[(domain, 'book_appointent')].append([y[0], y[1]])
|
system_action[(domain, 'book_appointent')][y[0]] = y[1]
|
||||||
elif intent == 'appointment/set_date':
|
elif intent == 'appointment/set_date':
|
||||||
for y in constraints:
|
for y in constraints:
|
||||||
if(y[0] == 'datetime'):
|
if(y[0] == 'datetime'):
|
||||||
system_action[(domain, 'set_appointment_date')].append([y[0], y[1]])
|
system_action[(domain, 'set_appointment_date')][y[0]] = y[1]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -69,30 +66,30 @@ class DP():
|
|||||||
if intent == 'request_information/available_dates':
|
if intent == 'request_information/available_dates':
|
||||||
for x in constraints_v2:
|
for x in constraints_v2:
|
||||||
if(x[0] == 'doctor'):
|
if(x[0] == 'doctor'):
|
||||||
system_action[(domain, 'date_info')].append([x[0], x[1]])
|
system_action[(domain, 'date_info')][x[0]] = x[1]
|
||||||
for y in constraints_v2:
|
for y in constraints_v2:
|
||||||
if(y[0] == 'datetime'):
|
if(y[0] == 'datetime'):
|
||||||
system_action[(domain, 'date_info')].append([y[0], y[1]])
|
system_action[(domain, 'date_info')][y[0]] = y[1]
|
||||||
elif intent == 'request_information/doctors':
|
elif intent == 'request_information/doctors':
|
||||||
system_action[(domain, 'Doctors_list')] = []
|
system_action[("inform", 'Doctors_list')] = {"doctors": "dr. Kolano - okulista, dr. Kowalski - internista, dr. Kaszak - ginekolog"}
|
||||||
elif intent == 'request_information/location':
|
elif intent == 'request_information/location':
|
||||||
system_action[(domain, 'Location')] = []
|
system_action[("inform", 'Location')] = {"street": "Marszałkowska", "number": "45", "city": "Poznań"}
|
||||||
elif intent == 'request_information/cost':
|
elif intent == 'request_information/cost':
|
||||||
system_action[(domain, 'Cost')] = []
|
system_action[("inform", 'Cost')] = {"cost": "100 zł"}
|
||||||
elif intent == 'request_information/opening_hours':
|
elif intent == 'request_information/opening_hours':
|
||||||
system_action[(domain, 'Opening_Hours')] = []
|
system_action[("inform", 'Opening_Hours')] = {"hours": "'8:00 - 16:00"}
|
||||||
elif len(constraints) == 0:
|
elif len(constraints) == 0:
|
||||||
system_action[(domain, 'NoOffer')] = []
|
system_action[("inform", 'NoOffer')] = {'0': '0'}
|
||||||
|
|
||||||
#################################################################################################
|
#################################################################################################
|
||||||
# Reguła 3
|
# Reguła 3
|
||||||
if domain == 'end_conversation':
|
if domain == 'end_conversation':
|
||||||
system_action[(domain, 'End_Conversation')].append(['0', '0'])
|
system_action[(domain, 'End_Conversation')] = {'0': '0'}
|
||||||
|
|
||||||
#################################################################################################
|
#################################################################################################
|
||||||
# Reguła 4
|
# Reguła 4
|
||||||
if domain == 'greeting':
|
if domain == 'greeting':
|
||||||
system_action[(domain, 'Greeting')].append(['0', '0'])
|
system_action[(domain, 'Greeting')] = {'0': '0'}
|
||||||
|
|
||||||
#################################################################################################
|
#################################################################################################
|
||||||
# Reguła 5
|
# Reguła 5
|
||||||
@ -101,21 +98,21 @@ class DP():
|
|||||||
if intent == 'prescription/request':
|
if intent == 'prescription/request':
|
||||||
for x in constraints_v2:
|
for x in constraints_v2:
|
||||||
if(x[0] == 'prescription/type'):
|
if(x[0] == 'prescription/type'):
|
||||||
system_action[(domain, 'create_prescription')].append([x[0], x[1]])
|
system_action[(domain, 'create_prescription')][x[0]] = x[1]
|
||||||
for y in constraints_v2:
|
for y in constraints_v2:
|
||||||
if(y[0] == 'prescription/medicine'):
|
if(y[0] == 'prescription/medicine'):
|
||||||
system_action[(domain, 'create_prescription')].append([y[0], y[1]])
|
system_action[(domain, 'create_prescription')][y[0]] = y[1]
|
||||||
elif intent == 'prescription/collect':
|
elif intent == 'prescription/collect':
|
||||||
for x in constraints_v2:
|
for x in constraints_v2:
|
||||||
if(x[0] == 'prescription/type'):
|
if(x[0] == 'prescription/type'):
|
||||||
system_action[(domain, 'collect_prescription')].append([x[0], x[1]])
|
system_action[(domain, 'collect_prescription')][x[0]] = x[1]
|
||||||
elif intent == 'prescription/type':
|
elif intent == 'prescription/type':
|
||||||
for x in constraints_v2:
|
for x in constraints_v2:
|
||||||
if(x[0] == 'prescription/type_info'):
|
if(x[0] == 'prescription/type_info'):
|
||||||
system_action[(domain, 'prescription type')].append([x[0], x[1]])
|
system_action[(domain, 'prescription type')][x[0]] = x[1]
|
||||||
if len(constraints_v2) == 0:
|
if len(constraints_v2) == 0:
|
||||||
system_action[(domain, 'NoOffer')] = []
|
system_action[(domain, 'NoOffer')] = []
|
||||||
|
|
||||||
#return system_action
|
return system_action
|
||||||
|
|
||||||
## Brakuje: Rejestracja, Login, Hasło, affirm, deny
|
## Brakuje: Rejestracja, Login, Hasło, affirm, deny
|
@ -11,7 +11,7 @@ class Rules_DST(): #Dialogue State Tracker
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.state = json.load(open('default_state.json'))
|
self.state = json.load(open('default_state.json'))
|
||||||
|
|
||||||
def update(self, user_acts=None):
|
def update_user(self, user_acts=None):
|
||||||
for intent, domain, slot, value in user_acts:
|
for intent, domain, slot, value in user_acts:
|
||||||
self.state["user_action"].append([intent, domain, slot, value])
|
self.state["user_action"].append([intent, domain, slot, value])
|
||||||
if domain in ['password', 'name', 'email', 'enter_email', 'enter_name']:
|
if domain in ['password', 'name', 'email', 'enter_email', 'enter_name']:
|
||||||
@ -27,7 +27,7 @@ class Rules_DST(): #Dialogue State Tracker
|
|||||||
domain_dic = self.state['belief_state'][domain]
|
domain_dic = self.state['belief_state'][domain]
|
||||||
if slot in domain_dic:
|
if slot in domain_dic:
|
||||||
self.state['belief_state'][domain][slot] = value
|
self.state['belief_state'][domain][slot] = value
|
||||||
|
|
||||||
if 'prescription' in intent:
|
if 'prescription' in intent:
|
||||||
if (slot == 'prescription'):
|
if (slot == 'prescription'):
|
||||||
continue
|
continue
|
||||||
@ -51,7 +51,9 @@ class Rules_DST(): #Dialogue State Tracker
|
|||||||
self.state['request_state'][domain] = {}
|
self.state['request_state'][domain] = {}
|
||||||
if slot not in self.state['request_state'][domain]:
|
if slot not in self.state['request_state'][domain]:
|
||||||
self.state['request_state'][domain][slot] = value
|
self.state['request_state'][domain][slot] = value
|
||||||
|
|
||||||
print(self.state)
|
elif intent == 'end_conversation':
|
||||||
print("\n")
|
self.state = json.load(open('default_state.json'))
|
||||||
return self.state
|
# print(self.state)
|
||||||
|
# print("\n")
|
||||||
|
return self.state
|
@ -1,5 +1,3 @@
|
|||||||
import jsgf
|
|
||||||
from tabulate import tabulate
|
|
||||||
from flair.data import Sentence, Token
|
from flair.data import Sentence, Token
|
||||||
from flair.datasets import SentenceDataset
|
from flair.datasets import SentenceDataset
|
||||||
from flair.models import SequenceTagger
|
from flair.models import SequenceTagger
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
import random
|
||||||
|
import uuid
|
||||||
|
|
||||||
class NLG:
|
class NLG:
|
||||||
"""
|
"""
|
||||||
Moduł, który tworzy reprezentację tekstową aktu systemowego wybranego przez taktykę dialogu.
|
Moduł, który tworzy reprezentację tekstową aktu systemowego wybranego przez taktykę dialogu.
|
||||||
@ -9,10 +12,62 @@ class NLG:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def change_to_text(self, act_vector):
|
def change_to_text(self, system_acts):
|
||||||
"""
|
"""
|
||||||
Funkcja zamieniająca akt systemu na tekst rozumiany przez użytkownika.
|
Funkcja zamieniająca akt systemu na tekst rozumiany przez użytkownika.
|
||||||
"""
|
"""
|
||||||
if(act_vector == [0, 0]):
|
if(len(system_acts) == 0):
|
||||||
return "Cześć, mam na imię Janet"
|
return "Nie mam już nic do powiedzenia :("
|
||||||
return "Nie rozumiem"
|
|
||||||
|
act = system_acts.pop(0)
|
||||||
|
if(len(act) == 0):
|
||||||
|
return "Nie mam już nic do powiedzenia :("
|
||||||
|
|
||||||
|
for action in act:
|
||||||
|
domain, intent = action
|
||||||
|
if(domain == "greeting"):
|
||||||
|
return random.choice(["Cześć, mam na imię Janet", "Hej, jestem Janet. W czym mogę pomóc?", "Dzień dobry, nazywam się Janet"])
|
||||||
|
|
||||||
|
if(domain == "end_conversation"):
|
||||||
|
return random.choice(["Dziękujemy za skorzystanie z naszych usług!", "Do widzenia!", "Do zobaczenia!"])
|
||||||
|
|
||||||
|
if(domain == "appointment"):
|
||||||
|
if (intent == "NoOffer"):
|
||||||
|
return "Proszę sprecyzować zapis na wizytę (lekarz, godzina)"
|
||||||
|
if (intent == "book_appointent"):
|
||||||
|
answer = "Zarezerwowano wizytę"
|
||||||
|
for key in act[action]:
|
||||||
|
value = act[action][key]
|
||||||
|
if(key == "doctor"):
|
||||||
|
answer += f" do lekarza {value}"
|
||||||
|
if(key== "datetime"):
|
||||||
|
answer += f" umówiona na: {value}"
|
||||||
|
return answer
|
||||||
|
if (intent == "set_appointment_date"):
|
||||||
|
value = act[action]["datetime"]
|
||||||
|
return f"Ustawiono datę wizyty na {value}"
|
||||||
|
|
||||||
|
if(domain == "inform"):
|
||||||
|
if (intent == "Doctors_list"):
|
||||||
|
value = act[action]["doctors"]
|
||||||
|
return f"Lista doktorów w naszej placówce: \n{value}"
|
||||||
|
if (intent == "Location"):
|
||||||
|
city = act[action]["city"]
|
||||||
|
street = act[action]["street"]
|
||||||
|
number = act[action]["number"]
|
||||||
|
return f"Placówka znajduje się w {city}, {street} {number}"
|
||||||
|
if (intent == "Opening_Hours"):
|
||||||
|
value = act[action]["hours"]
|
||||||
|
return f"Placówka jest otwarta w godzinach {value}"
|
||||||
|
if (intent == "NoOffer"):
|
||||||
|
return f"Nie posiadam takiej wiedzy, żeby odpowiedzieć na to pytanie :("
|
||||||
|
|
||||||
|
if(domain == "prescription"):
|
||||||
|
if(intent == "create_prescription" or intent == "collect_prescription"):
|
||||||
|
answer = f"Odebrano receptę {uuid.uuid4().hex}"
|
||||||
|
return answer
|
||||||
|
if(intent == "prescription type"):
|
||||||
|
return "Wszystkie recepty w naszej placówce są elektroniczne"
|
||||||
|
if (intent == "NoOffer"):
|
||||||
|
return f"Nie rozumiem :("
|
||||||
|
|
||||||
|
@ -16,10 +16,6 @@
|
|||||||
"username": "",
|
"username": "",
|
||||||
"password": ""
|
"password": ""
|
||||||
},
|
},
|
||||||
"prescription": {
|
|
||||||
"prescription/type": "",
|
|
||||||
"prescription/medicine": ""
|
|
||||||
},
|
|
||||||
"request_information":{
|
"request_information":{
|
||||||
"doctor": "",
|
"doctor": "",
|
||||||
"datetime": "",
|
"datetime": "",
|
||||||
|
Loading…
Reference in New Issue
Block a user