diff --git a/src/service/natural_language_generation.py b/src/service/natural_language_generation.py index 8ab6875..42935bf 100644 --- a/src/service/natural_language_generation.py +++ b/src/service/natural_language_generation.py @@ -1,8 +1,33 @@ import re from service.template_selector import select_template import random + +class NaturalLanguageGeneration: + def __init__(self, templates): + self.templates = templates -# from service.templates import 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) \ No newline at end of file +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 \ No newline at end of file