This commit is contained in:
s495724 2024-06-12 16:00:02 +02:00
parent 5f128e7e88
commit fded76596b

View File

@ -2,7 +2,32 @@ import re
from service.template_selector import select_template from service.template_selector import select_template
import random import random
# from service.templates import templates class NaturalLanguageGeneration:
def __init__(self, templates):
self.templates = templates
def generate(self, frame, system_action):
# Parsowanie frame
act, slots = parse_frame(frame)
# Wybierz szablon na podstawie system_action
template = select_template(system_action['act'], system_action['slots'])
if template is None:
print(f"Brak szablonu dla act: {system_action['act']} z slotami: {system_action['slots']}")
template = random.choice(self.templates["default/template"])
# Zamień sloty na wartości
slot_dict = {}
for slot in system_action['slots']:
if isinstance(slot['value'], list):
slot_dict[slot['name']] = ', '.join(slot['value'])
elif isinstance(slot['value'], dict):
slot_dict[slot['name']] = ', '.join([f"{k}: {v}" for k, v in slot['value'].items()])
else:
slot_dict[slot['name']] = slot['value']
response = template.format(**slot_dict)
return response
def parse_frame(frame): def parse_frame(frame):
if not hasattr(frame, 'act') or not hasattr(frame, 'slots'): if not hasattr(frame, 'act') or not hasattr(frame, 'slots'):
@ -13,14 +38,13 @@ def parse_frame(frame):
return act, slots return act, slots
class NaturalLanguageGeneration():
def __init__(self, templates):
self.templates = templates
def generate(self, frame): class Slot:
act, slots = parse_frame(frame) def __init__(self, name, value):
template = select_template(act, slots) self.name = name
if template == "default/template": self.value = value
template = random.choice(self.templates["default/template"])
slot_dict = {slot['name']: slot['value'] for slot in slots} class Frame:
return template.format(**slot_dict) def __init__(self, act, slots):
self.act = act
self.slots = slots