improvement

This commit is contained in:
Piotr Meller 2021-04-26 15:13:52 +02:00
parent d075382862
commit 653693d4f9
8 changed files with 89 additions and 47 deletions

34
ActFrame.py Normal file
View File

@ -0,0 +1,34 @@
from abc import ABC, abstractmethod
class ActFrame(ABC):
def __init__(self, actType, actParams = None):
if actType == None:
raise Exception('actType cannot be None')
self.__actType = actType
if actParams != None:
if type(actParams) is not list:
raise Exception(
'actParams has wrong type: expected type \'list\', got \'{}\''.format(type(actParams)))
self.__actParams = actParams
def __repr__(self):
return str(type(self))
def __str__(self):
return "actType:{} actParams:{}".format(self.__actType,self.__actParams)
def setActParams(self, actParams):
if type(actParams) is not list:
raise Exception(
'actParams has wrong type: expected type \'list\', got \'{}\''.format(type(actParams)))
self.__actParams = actParams
def getActParams(self):
return self.__actParams
def getActType(self):
return self.__actType

View File

@ -1,5 +1,6 @@
from SystemAct import SystemAct
from UserActType import UserActType from UserActType import UserActType
from UserAct import UserAct from SystemActType import SystemActType
class DP: class DP:
@ -12,13 +13,15 @@ class DP:
def __init__(self): def __init__(self):
pass pass
def chooseTactic(self, frameList=None): def chooseTactic(self, frameList=None) -> SystemAct:
if frameList[-1].getActType() is UserActType.INVALID: userAct = frameList[-1]
systemAct = [1] if userAct.getActType() == UserActType.WELCOME_MSG:
return systemAct return SystemAct(SystemActType.WELCOME_MSG)
elif frameList[-1].getActType() is UserActType.BYE: if userAct.getActType() == UserActType.REQUEST:
systemAct = [2] if "name" in userAct.getActParams():
return systemAct return SystemAct(SystemActType.INFORM,['name'])
else: if userAct.getActType() == UserActType.BYE:
systemAct = [0] return SystemAct(SystemActType.BYE)
return systemAct if userAct.getActType() == UserActType.INVALID:
return SystemAct(SystemActType.NOT_UNDERSTOOD)
raise Exception("UserAct:{} not recognized".format(userAct))

View File

@ -1,3 +1,6 @@
from SystemAct import SystemAct
from SystemActType import SystemActType
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.
@ -8,10 +11,14 @@ class NLG:
def __init__(self): def __init__(self):
pass pass
def toText(self, systemAct): def toText(self, systemAct: SystemAct) -> str:
if systemAct == [1]: if systemAct.getActType() == SystemActType.WELCOME_MSG:
return "Nie rozumiem o czym mówisz." return "Cześć"
if systemAct == [2]: if systemAct.getActType() == SystemActType.INFORM:
if "name" in systemAct.getActParams():
return "Nazywam się Janusz"
if systemAct.getActType() == SystemActType.BYE:
return "Do widzenia." return "Do widzenia."
else: if systemAct.getActType() == SystemActType.NOT_UNDERSTOOD:
return "Witaj, nazywam się Janusz." return "Nie rozumiem o czym mówisz."
raise Exception("SystemAct:{} not recognized".format(systemAct))

View File

@ -29,7 +29,7 @@ class NLU:
) )
] ]
def parseUserInput(self, text): def parseUserInput(self, text: str) -> UserAct:
for pattern, actType, actParams in self.__actParsePatternList: for pattern, actType, actParams in self.__actParsePatternList:
regex = re.compile(pattern, re.IGNORECASE) regex = re.compile(pattern, re.IGNORECASE)
match = regex.match(text) match = regex.match(text)

12
SystemAct.py Normal file
View File

@ -0,0 +1,12 @@
from SystemActType import SystemActType
from ActFrame import ActFrame
class SystemAct(ActFrame):
def __init__(self, actType, actParams = None):
if actType != None and type(actType) is not SystemActType:
raise Exception('actParams has wrong type: expected type {}, got {}'.format(type(SystemActType.WELCOME_MSG), type(actType)))
super(SystemAct, self).__init__(actType, actParams)
def isDialogFinished(self):
return self.getActType() == SystemActType.BYE

9
SystemActType.py Normal file
View File

@ -0,0 +1,9 @@
from enum import Enum, unique
@unique
class SystemActType(Enum):
WELCOME_MSG = 0
INFORM = 1
BYE = 2
NOT_UNDERSTOOD = -1

View File

@ -1,32 +1,9 @@
from UserActType import UserActType from UserActType import UserActType
from ActFrame import ActFrame
class UserAct(ActFrame):
class UserAct:
def __init__(self, actType, actParams = None): def __init__(self, actType, actParams = None):
if actType == None: if actType != None and type(actType) is not UserActType:
raise Exception('actType cannot be None') raise Exception('actParams has wrong type: expected type {}, got {}'.format(type(UserActType.WELCOME_MSG), type(actType)))
self.__actType = actType
if actParams != None: super(UserAct, self).__init__(actType, actParams)
if type(actParams) is not list:
raise Exception(
'actParams has wrong type: expected type \'list\', got \'{}\''.format(type(actParams)))
self.__actParams = actParams
def __repr__(self):
return "UserAct()"
def __str__(self):
return "actType:{} actParams:{}".format(self.__actType,self.__actParams)
def setActParams(self, actParams):
if type(actParams) is not list:
raise Exception(
'actParams has wrong type: expected type \'list\', got \'{}\''.format(type(actParams)))
self.__actParams = actParams
def getActParams(self):
return self.__actParams
def getActType(self):
return self.__actType

View File

@ -19,5 +19,5 @@ if __name__ == "__main__":
text = nlg.toText(system_act) text = nlg.toText(system_act)
print(text) print(text)
if text == "Do widzenia.": if system_act.isDialogFinished():
break break