NLU
This commit is contained in:
parent
049bd7f07e
commit
5d9165411a
46
NaturalLanguageUnderstanding.py
Normal file
46
NaturalLanguageUnderstanding.py
Normal file
@ -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)
|
32
UserAct.py
Normal file
32
UserAct.py
Normal file
@ -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
|
9
UserActType.py
Normal file
9
UserActType.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
from enum import Enum, unique
|
||||||
|
|
||||||
|
|
||||||
|
@unique
|
||||||
|
class UserActType(Enum):
|
||||||
|
WELCOME_MSG = 0
|
||||||
|
REQUEST = 1
|
||||||
|
BYE = 2
|
||||||
|
INVALID = -1
|
Loading…
Reference in New Issue
Block a user