telegram-bot-systemy-dialogowe/Modules.py

90 lines
2.1 KiB
Python
Raw Normal View History

2021-04-26 15:13:39 +02:00
#Natural Language Understanding
class NLU:
#Text analysys
def __init__(self, acts, arguments):
self.acts = acts
self.arguments = arguments
def analyze(self, text):
#Turn text into frame
#return vector for further use
2021-04-26 15:56:03 +02:00
act = "(hello()&request(name))"
2021-04-26 15:13:39 +02:00
vector = [[0],[1,0]]
return vector
#Dialogue policy
class DP:
#Module decide what act takes next
def __init__(self, acts, arguments):
self.acts = acts
self.arguments = arguments
def tacticChoice(self, frame_list):
2021-04-26 15:56:03 +02:00
actVector = [0, 0]
return actVector
2021-04-26 15:13:39 +02:00
#Dialogue State Tracker
class DST:
#Contain informations about state of the dialogue and data taken from user
def __init__(self, acts, arguments):
self.acts = acts
self.arguments = arguments
2021-04-26 15:56:03 +02:00
self.frameList= []
2021-04-26 15:13:39 +02:00
#store new act into frame
def store(self, frame):
2021-04-26 15:56:03 +02:00
self.frameList.append(frame)
2021-04-26 15:13:39 +02:00
def transfer(self):
2021-04-26 15:56:03 +02:00
return self.frameList
2021-04-26 15:13:39 +02:00
#Natural Language Generator
class NLG:
def __init__(self, acts, arguments):
self.acts = acts
self.arguments = arguments
2021-04-26 15:56:03 +02:00
def vectorToText(self, actVector):
if(actVector == [0, 0]):
return "Witaj, nazywam się Mateusz."
else:
return "Przykro mi, nie zrozumiałem Cię"
2021-04-26 15:13:39 +02:00
class Run:
def __init__(self):
self.acts={
2021-04-26 15:56:03 +02:00
0: "hello",
2021-04-26 15:13:39 +02:00
1: "request",
}
self.arguments={
0: "name"
}
2021-04-26 15:30:26 +02:00
2021-04-26 15:13:39 +02:00
self.nlu = NLU(self.acts, self.arguments)
self.dp = DP(self.acts, self.arguments)
self.nlg = NLG(self.acts, self.arguments)
self.dst = DST(self.acts, self.arguments)
2021-04-26 15:56:03 +02:00
def inputProcessing(self, command):
2021-04-26 15:13:39 +02:00
act = self.nlu.analyze(command)
2021-04-26 15:30:26 +02:00
2021-04-26 15:13:39 +02:00
self.dst.store(act)
2021-04-26 15:30:26 +02:00
basic_act = self.dp.tacticChoice(self.dst.transfer())
return self.nlg.vectorToText(basic_act)
2021-04-26 15:13:39 +02:00
run = Run()
while(1):
message = input("Napisz coś: ")
2021-04-26 15:56:03 +02:00
print(run.inputProcessing(message))
2021-04-26 15:30:26 +02:00