telegram-bot-systemy-dialogowe/Modules.py

113 lines
2.9 KiB
Python
Raw Normal View History

2021-05-16 23:13:40 +02:00
import os
import jsgf
2021-04-26 15:13:39 +02:00
#Natural Language Understanding
class NLU:
2021-05-16 23:13:40 +02:00
def __init__(self):
self.grammars = [jsgf.parse_grammar_file(f'JSGFs/{file_name}') for file_name in os.listdir('JSGFs')]
def get_dialog_act(self, sentence):
2021-05-17 14:45:14 +02:00
acts = []
for sentence in sentence.split(','):
for grammar in self.grammars:
match = grammar.find_matching_rules(sentence)
if match:
acts.append(grammar.name)
return acts
2021-05-16 23:13:40 +02:00
def match_slots(self, expansion, slots):
if expansion.tag != '':
slots.append((expansion.tag, expansion.current_match))
return slots
for child in expansion.children:
self.get_slots(child, slots)
if not expansion.children and isinstance(expansion, jsgf.NamedRuleRef):
self.get_slots(expansion.referenced_rule.expansion, slots)
def get_slots(self, utterance):
slots = []
for grammar in self.grammars:
matched = grammar.find_matching_rules(utterance)
if matched:
return self.match_slots(matched[0], slots)
return []
2021-04-26 15:13:39 +02:00
#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-05-16 23:13:40 +02:00
self.nlu = NLU()
2021-04-26 15:13:39 +02:00
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
2021-05-16 23:13:40 +02:00
# run = Run()
# while(1):
# message = input("Napisz coś: ")
# print(run.inputProcessing(message))
2021-04-26 15:30:26 +02:00