diff --git a/NaturalLanguageUnderstanding.py b/NaturalLanguageUnderstanding.py new file mode 100644 index 0000000..f54e21a --- /dev/null +++ b/NaturalLanguageUnderstanding.py @@ -0,0 +1,46 @@ +from UserActType import UserActType +from UserAct import UserAct +import re + + +class NLU: + + def __init__(self): + self.__actParsePatternList = [ + ( + r"(.*)(cze(ś|s)(ć|c)|witaj|dzie(ń|n) dobry)(.*)", + UserActType.WELCOME_MSG, + [] + ), + ( + r"(.*)(Jak (masz na imi(ę|e))|(si(ę|e) nazywasz))(.*)", + UserActType.REQUEST, + ["name"] + ), + ( + r"(.*)((ż|z)egnaj)|(do widzenia)|(na razie)(.*)", + UserActType.BYE, + [] + ) + ] + + def parseUserInput(self, text): + for pattern, actType, actParams in self.__actParsePatternList: + regex = re.compile(pattern, re.IGNORECASE) + match = regex.match(text) + if match: + return UserAct(actType, actParams) + return UserAct(UserActType.INVALID) + + +if __name__ == "__main__": + + nlu = NLU() + + a = nlu.parseUserInput("czesc") + b = nlu.parseUserInput("jak masz na imie") + c = nlu.parseUserInput("zegnaj") + + print(a) + print(b) + print(c) diff --git a/UserAct.py b/UserAct.py new file mode 100644 index 0000000..722b1bf --- /dev/null +++ b/UserAct.py @@ -0,0 +1,32 @@ +from UserActType import UserActType + + +class UserAct: + 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 "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 diff --git a/UserActType.py b/UserActType.py new file mode 100644 index 0000000..d532331 --- /dev/null +++ b/UserActType.py @@ -0,0 +1,9 @@ +from enum import Enum, unique + + +@unique +class UserActType(Enum): + WELCOME_MSG = 0 + REQUEST = 1 + BYE = 2 + INVALID = -1