Compare commits

...

5 Commits

Author SHA1 Message Date
f69e664e04 Fix 2021-06-15 10:05:49 +02:00
508508a416 Delete meeeting fix 2021-06-15 09:43:04 +02:00
bf1c7b95fd Meeting list hotfix 2021-06-15 09:34:09 +02:00
af98dabec7 Meeting list functionality connected to DB 2021-06-15 09:25:48 +02:00
6e4e1271c4 Basic connection of DB 2021-06-14 17:27:27 +02:00
6 changed files with 129 additions and 70 deletions

View File

@ -43,6 +43,10 @@ class calender_db:
with open(self.db_file_path, 'r+') as f:
self.db = json.load(f)
def clear_db(self):
self.db = {}
self.save_db()
def save_db(self):
with open(self.db_file_path, 'w+') as f:
json.dump(self.db, f)
@ -72,19 +76,10 @@ class calender_db:
self.db[format_date(date_time)] = meetings
self.save_db()
def update_meeting(self, meeting_dict):
date_time = get_date(
meeting_dict['date'].lower(), meeting_dict['time'].lower())
if format_date(date_time) in self.db.keys():
meetings = self.db[format_date(date_time)]
for key, meeting in enumerate(meetings):
if format_time(meeting['time']) == format_time(meeting_dict['time'].lower()):
meetings.remove(meeting)
meetings.append(meeting_dict)
self.db[format_date(date_time)] = meetings
self.save_db()
def update_meeting(self, old_meeting_date, old_meeting_time, new_meeting_dict):
self.delete_meeting(old_meeting_date, old_meeting_time)
self.create_meeting(new_meeting_dict)
def find_meeting(self, date, time):
if date in self.db.keys():
@ -180,10 +175,15 @@ class calender_db:
# Tests
# db = calender_db()
# db.create_meeting({"date": "16.06.2021", "time": "15:00",
# "description": "chuj"})
# db.clear_db()
# db.create_meeting({"date": "16.06.2021", "time": "15:00", "description": "ciastko"})
# db.create_meeting({"date": "14.06.2021", "time": "13:00-18:00"})
# db.delete_meeting("16.06.2021", "15:00")
# print(db.find_meeting("16.06.2021", "13:00-14:00"))
# print(db.get_meetings(["16.06.2021", "14.06.2021"]))
# print(db.is_collision("16.06.2021", "12:30-13"))
#db.create_meeting({"date": "16.06.2021", "time": "12:00-13:00", "description": "costam"})
#print(db.get_meetings(["16.06.2021", "14.06.2021"]))
#db.update_meeting("16.06.2021", "12:00-13:00", {"date": "14.06.2021", "time": "11:00-12:00"})
#print(db.get_meetings(["16.06.2021", "14.06.2021"]))
#db.delete_meeting("16.06.2021", "15:00")
#print(db.find_meeting("16.06.2021", "13:00-14:00"))
#print(db.get_meetings(["16.06.2021", "14.06.2021"]))
#print(db.is_collision("16.06.2021", "12:30-13"))
#db.clear_db()

View File

@ -11,8 +11,9 @@ class DP:
Wyjście: Akt systemu (rama)
"""
def __init__(self, dst):
def __init__(self, dst, db):
self.DST = dst
self.DB = db
self.meeting_to_update = False
def chooseTactic(self) -> SystemAct:
@ -71,6 +72,7 @@ class DP:
self.DST.system_update(system_act)
return system_act
else:
# TODO sprawdzanie czy spotkanie nie koliduje
system_act = SystemAct(SystemActType.CONFIRM_DOMAIN, slots)
self.DST.system_update(system_act)
return system_act
@ -78,6 +80,7 @@ class DP:
if last_user_act == UserActType.CONFIRM:
system_act = SystemAct(SystemActType.AFFIRM, ['create_meeting'])
# implementacja wpisywanie spotkania do bazy
self.DB.create_meeting(slots)
self.DST.clear()
return system_act
elif last_user_act == UserActType.NEGATE:
@ -201,7 +204,10 @@ class DP:
return system_act
else:
# implementacja wyszukiwania odpowiedniego spotkania w bazie
system_act = SystemAct(SystemActType.CONFIRM_DOMAIN, ['meeting_to_cancel'])
slots_to_delete = self.DB.find_meeting(slots['date'], slots['time'])
self.DST.update_slots(slots_to_delete)
#system_act = SystemAct(SystemActType.CONFIRM_DOMAIN, ['meeting_to_cancel'])
system_act = SystemAct(SystemActType.CONFIRM_DOMAIN, slots_to_delete)
self.DST.system_update(system_act)
return system_act
elif last_system_act.getActType() == SystemActType.REQUEST:
@ -215,13 +221,17 @@ class DP:
return system_act
else:
# implementacja wyszukiwania odpowiedniego spotkania w bazie
system_act = SystemAct(SystemActType.CONFIRM_DOMAIN, ['meeting_to_cancel'])
slots_to_delete = self.DB.find_meeting(slots['date'], slots['time'])
self.DST.update_slots(slots_to_delete)
# system_act = SystemAct(SystemActType.CONFIRM_DOMAIN, ['meeting_to_cancel'])
system_act = SystemAct(SystemActType.CONFIRM_DOMAIN, slots_to_delete)
self.DST.system_update(system_act)
return system_act
elif last_system_act.getActType() == SystemActType.CONFIRM_DOMAIN:
if last_user_act == UserActType.CONFIRM:
system_act = SystemAct(SystemActType.AFFIRM, ['cancel_meeting'])
# implementacja usuwania spotkania z bazy
self.DB.delete_meeting(slots['date'], slots['time'])
self.DST.clear()
return system_act
elif last_user_act == UserActType.NEGATE:
@ -231,18 +241,42 @@ class DP:
return SystemAct(SystemActType.NOT_UNDERSTOOD, [])
# stan prośby o listę spotkań
elif dialogue_state == UserActType.MEETING_LIST:
if last_user_act == UserActType.NEGATE:
self.DST.clear()
return SystemAct(SystemActType.REQMORE, ['meeting_list'])
else:
if 'date' in slots:
system_act = SystemAct(SystemActType.MEETING_LIST, slots)
self.DST.clear()
return system_act
else:
if not last_system_act:
if 'date' not in slots:
system_act = SystemAct(SystemActType.REQUEST, ['date'])
self.DST.system_update(system_act)
return system_act
else:
# implementacja wyszukiwania spotkań w bazie
meetings_slots = self.DB.get_meetings([slots['date']])
system_act = SystemAct(SystemActType.MEETING_LIST, meetings_slots)
self.DST.system_update(system_act)
return system_act
elif last_system_act.getActType() == SystemActType.REQUEST:
if 'date' not in slots:
system_act = SystemAct(SystemActType.REQUEST, ['date'])
self.DST.system_update(system_act)
return system_act
else:
# implementacja wyszukiwania spotkań w bazie
meetings_slots = self.DB.get_meetings([slots['date']])
system_act = SystemAct(SystemActType.MEETING_LIST, meetings_slots)
self.DST.system_update(system_act)
return system_act
else:
return SystemAct(SystemActType.NOT_UNDERSTOOD, [])
# if last_user_act == UserActType.NEGATE:
# self.DST.clear()
# return SystemAct(SystemActType.REQMORE, ['meeting_list'])
# else:
# if 'date' in slots:
# system_act = SystemAct(SystemActType.MEETING_LIST, slots)
# self.DST.clear()
# return system_act
# else:
# system_act = SystemAct(SystemActType.REQUEST, ['date'])
# self.DST.system_update(system_act)
# return system_act
# stan prośby o czas wolny
elif dialogue_state == UserActType.FREE_TIME:
if last_user_act == UserActType.NEGATE:

View File

@ -45,6 +45,9 @@ class DST:
def clear_slots(self):
self.slots = {}
def update_slots(self, slots):
self.slots = slots
def get_dialogue_state(self):
return self.state, self.last_user_act, self.last_system_act

View File

@ -13,7 +13,7 @@ class NLG:
self.DST = dst
def generateResponse(self, systemAct: SystemAct) -> str:
dialogue_state, last_user_act, _ = self.DST.get_dialogue_state()
dialogue_state, _, last_system_act = self.DST.get_dialogue_state()
slots = self.DST.get_dialogue_slots()
if dialogue_state == UserActType.CREATE_MEETING:
if systemAct.getActType() == SystemActType.REQUEST:
@ -33,16 +33,24 @@ class NLG:
place = slots['place']
part_list = slots['participants']
part = ""
for p in part_list:
part += p
part += ", "
part = part[:-2]
if part_list is None:
part = None
else:
for p in part_list:
part += p
part += ", "
part = part[:-2]
desc = slots['description']
return f'Czy mam dodać te spotkanie do kalendarza?\n' \
f'Dzień: {date}\nCzas: {time}\nMiejsce: {place}\nUczestnicy: {part}\nOpis: {desc}'
# TODO: nie sprawdzone - trudno wejść do tego stanu
elif dialogue_state == UserActType.UPDATE_MEETING:
# TODO zaślepka do wywalenia
response = "Przepraszam ale obecnie nie obsługuje jeszcze tej funkcji :("
self.DST.clear()
return response
# -------------------------------------
if systemAct.getActType() == SystemActType.REQUEST:
if "date" in systemAct.getActParams():
return "W jakim dniu miało się odbyć to spotkanie?"
@ -55,10 +63,13 @@ class NLG:
place = slots['place']
part_list = slots['participants']
part = ""
for p in part_list:
part += p
part += ", "
part = part[:-2]
if part_list is None:
part = None
else:
for p in part_list:
part += p
part += ", "
part = part[:-2]
desc = slots['description']
return f'Spotkanie:\n' \
f'Dzień: {date}\nCzas: {time}\nMiejsce: {place}\nUczestnicy: {part}\nOpis: {desc}'
@ -69,44 +80,54 @@ class NLG:
return "W jakim dniu miało się odbyć to spotkanie?"
if "time" in systemAct.getActParams():
return "W jakim czasie miało się odbyć to spotkanie?"
# TODO dopracować po dodaniu DB
if systemAct.getActType() == SystemActType.CONFIRM_DOMAIN:
date = slots['date']
time = slots['time']
# place = slots['place']
# part_list = slots['participants']
# part = ""
# for p in part_list:
# part += p
# part += ", "
# part = part[:-2]
# desc = slots['description']
return f'Spotkanie:\n' \
f'Dzień: {date}\nCzas: {time}'
place = slots['place']
part_list = slots['participants']
part = ""
if part_list is None:
part = None
else:
for p in part_list:
part += p
part += ", "
part = part[:-2]
desc = slots['description']
return f'Odwołać te spotkanie?:\n' \
f'Dzień: {date}\nCzas: {time}\nMiejsce: {place}\nUczestnicy: {part}\nOpis: {desc}'
elif dialogue_state == UserActType.MEETING_LIST:
if systemAct.getActType() == SystemActType.REQUEST:
if "date" in systemAct.getActParams():
return "Z jakiego okresu chcesz przejrzeć spotkania?"
# TODO: dopracować po dodaniu DB
if systemAct.getActType() == SystemActType.MEETING_LIST:
response = ""
for s in slots:
for s in last_system_act.getActParams():
date = s['date']
time = s['time']
place = s['place']
part_list = s['participants']
part = ""
for p in part_list:
part += p
part += ", "
part = part[:-2]
if part_list is None:
part = None
else:
for p in part_list:
part += p
part += ", "
part = part[:-2]
desc = s['description']
response += f'Spotkanie:\nDzień: {date}\nCzas: {time}\nMiejsce: {place}\nUczestnicy: {part}\nOpis: {desc}\n'
response += f'\nSpotkanie:\nDzień: {date}\nCzas: {time}\nMiejsce: {place}\nUczestnicy: {part}\nOpis: {desc}\n'
response += "--------------------"
self.DST.clear()
return response
elif dialogue_state == UserActType.FREE_TIME:
# TODO zaślepka do wywalenia
response = "Przepraszam ale obecnie nie obsługuje jeszcze tej funkcji :("
self.DST.clear()
return response
#-------------------------------------
if systemAct.getActType() == SystemActType.REQUEST:
if "date" in systemAct.getActParams():
return "W jakim okresie chcesz znaleźć wolny czas?"

View File

@ -124,6 +124,7 @@ def get_datetime_from_day(day, hours, minutes):
def get_date(date, time):
print("test\ndate: ", date, "\ntime: ", time)
# parsowanie czasu
if re.match(r'^[0-2][0-9]$', time):
hours = int(time)

26
main.py
View File

@ -3,36 +3,36 @@ from NaturalLanguageUnderstanding import NLU
from NaturalLanguageGeneration import NLG
from DialogueStateTracker import DST
from DialoguePolicy import DP
from DBManager import calender_db
if __name__ == "__main__":
db = calender_db()
nlu = NLU()
dst = DST()
dp = DP(dst)
dp = DP(dst, db)
nlg = NLG(dst)
while(1):
user_input = input("Wpisz tekst: ")
user_input = input("\nWpisz tekst: ")
user_frame = nlu.parse_user_input(user_input)
print('------ rozpoznany user frame ------')
print(user_frame)
# print('\n------ rozpoznany user frame ------')
# print(user_frame)
dst.user_update(user_frame)
state, last_user_act, last_system_act = dst.get_dialogue_state()
slots = dst.get_dialogue_slots()
system_act = dp.chooseTactic()
print('------ stan ------')
print(state, last_user_act, last_system_act)
print('------ przechowywane sloty ------')
print(slots)
print('------ wybrana akcja systemu ------')
# print('\n------ stan ------')
# print(state, last_user_act, last_system_act)
# print('\n------ przechowywane sloty ------')
# print(slots)
print('\n------ wybrana akcja systemu ------')
print(system_act)
system_response = nlg.generateResponse(system_act)
print('------ wygenerowana odpowiedź systemu ------')
print('\n------ wygenerowana odpowiedź systemu ------')
print(system_response)
print('-----------------------------------')
print('-----------------------------------')
if system_act.getActType() == SystemActType.BYE:
break