sklep-internetowy-systemy-d.../chatbot/modules/generator.py

24 lines
755 B
Python
Raw Normal View History

2024-05-07 21:40:14 +02:00
import json
from typing import Dict
from .config import Config
import random
class ResponseGenerator:
def __init__(self, config: Config):
with config.responses_path.open('r', encoding='utf-8') as file:
2024-06-11 18:23:36 +02:00
self.responses: Dict[str, dict] = json.load(file)
2024-06-11 18:10:51 +02:00
def nlg(self, system_act):
intent = system_act.intent
slot = system_act.slots[0].name if system_act.slots else None
2024-06-11 18:23:36 +02:00
intent_responses = self.responses.get(intent, {})
slot_responses = intent_responses.get(slot)
2024-05-07 21:40:14 +02:00
2024-06-11 18:23:36 +02:00
if isinstance(slot_responses, list):
return random.choice(slot_responses)
elif isinstance(slot_responses, str):
return slot_responses
else:
return "Nieznane zapytanie."