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
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):
if not hasattr(frame, 'act') or not hasattr(frame, 'slots'):
@ -13,14 +38,13 @@ def parse_frame(frame):
return act, slots
class NaturalLanguageGeneration():
def __init__(self, templates):
self.templates = templates
def generate(self, frame):
act, slots = parse_frame(frame)
template = select_template(act, slots)
if template == "default/template":
template = random.choice(self.templates["default/template"])
slot_dict = {slot['name']: slot['value'] for slot in slots}
return template.format(**slot_dict)
class Slot:
def __init__(self, name, value):
self.name = name
self.value = value
class Frame:
def __init__(self, act, slots):
self.act = act
self.slots = slots