diff --git a/ActFrame.py b/ActFrame.py new file mode 100644 index 0000000..14e0ab5 --- /dev/null +++ b/ActFrame.py @@ -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 diff --git a/DialoguePolicy.py b/DialoguePolicy.py index 5f539a5..da76f96 100644 --- a/DialoguePolicy.py +++ b/DialoguePolicy.py @@ -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)) diff --git a/NaturalLAnguageGeneration.py b/NaturalLAnguageGeneration.py index e60f8a5..6e3e1fa 100644 --- a/NaturalLAnguageGeneration.py +++ b/NaturalLAnguageGeneration.py @@ -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)) diff --git a/NaturalLanguageUnderstanding.py b/NaturalLanguageUnderstanding.py index 1863824..f84992a 100644 --- a/NaturalLanguageUnderstanding.py +++ b/NaturalLanguageUnderstanding.py @@ -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) diff --git a/SystemAct.py b/SystemAct.py new file mode 100644 index 0000000..eb7d150 --- /dev/null +++ b/SystemAct.py @@ -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 diff --git a/SystemActType.py b/SystemActType.py new file mode 100644 index 0000000..338090d --- /dev/null +++ b/SystemActType.py @@ -0,0 +1,9 @@ +from enum import Enum, unique + + +@unique +class SystemActType(Enum): + WELCOME_MSG = 0 + INFORM = 1 + BYE = 2 + NOT_UNDERSTOOD = -1 diff --git a/UserAct.py b/UserAct.py index 722b1bf..e644dd4 100644 --- a/UserAct.py +++ b/UserAct.py @@ -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) \ No newline at end of file diff --git a/main.py b/main.py index 09d7074..9391992 100644 --- a/main.py +++ b/main.py @@ -19,5 +19,5 @@ if __name__ == "__main__": text = nlg.toText(system_act) print(text) - if text == "Do widzenia.": + if system_act.isDialogFinished(): break