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 UserAct import UserAct
from SystemActType import SystemActType
class DP:
@ -12,13 +13,15 @@ class DP:
def __init__(self):
pass
def chooseTactic(self, frameList=None):
if frameList[-1].getActType() is UserActType.INVALID:
systemAct = [1]
return systemAct
elif frameList[-1].getActType() is UserActType.BYE:
systemAct = [2]
return systemAct
else:
systemAct = [0]
return systemAct
def chooseTactic(self, frameList=None) -> SystemAct:
userAct = frameList[-1]
if userAct.getActType() == UserActType.WELCOME_MSG:
return SystemAct(SystemActType.WELCOME_MSG)
if userAct.getActType() == UserActType.REQUEST:
if "name" in userAct.getActParams():
return SystemAct(SystemActType.INFORM,['name'])
if userAct.getActType() == UserActType.BYE:
return SystemAct(SystemActType.BYE)
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:
"""
Moduł, który tworzy reprezentację tekstową aktu systemowego wybranego przez taktykę dialogu.
@ -8,10 +11,14 @@ class NLG:
def __init__(self):
pass
def toText(self, systemAct):
if systemAct == [1]:
return "Nie rozumiem o czym mówisz."
if systemAct == [2]:
def toText(self, systemAct: SystemAct) -> str:
if systemAct.getActType() == SystemActType.WELCOME_MSG:
return "Cześć"
if systemAct.getActType() == SystemActType.INFORM:
if "name" in systemAct.getActParams():
return "Nazywam się Janusz"
if systemAct.getActType() == SystemActType.BYE:
return "Do widzenia."
else:
return "Witaj, nazywam się Janusz."
if systemAct.getActType() == SystemActType.NOT_UNDERSTOOD:
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:
regex = re.compile(pattern, re.IGNORECASE)
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 ActFrame import ActFrame
class UserAct:
class UserAct(ActFrame):
def __init__(self, actType, actParams = None):
if actType == None:
raise Exception('actType cannot be None')
self.__actType = actType
if actType != None and type(actType) is not UserActType:
raise Exception('actParams has wrong type: expected type {}, got {}'.format(type(UserActType.WELCOME_MSG), type(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 "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
super(UserAct, self).__init__(actType, actParams)

View File

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