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

39 lines
1.4 KiB
Python

import json
import os
from typing import Any, Dict, Literal
class Config:
def __init__(self) -> None:
try:
config_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'config.json')
with open(config_path, 'r', encoding='utf-8') as config_file:
self.config_data: Dict[str, Any] = json.load(config_file)
except FileNotFoundError:
print("Config file not found.")
self.config_data = {}
except json.JSONDecodeError:
print("Invalid JSON.")
self.config_data = {}
def get_data_path(self) -> str:
data_path = self.config_data.get('data_path', '')
if not isinstance(data_path, str):
raise ValueError("Data path must be a string.")
return os.path.join(os.path.dirname(os.path.dirname(__file__)), data_path)
class NaturalLanguageProcessor:
def __init__(self, config: Config) -> None:
self.config = config
data_path = self.config.get_data_path()
with open(data_path, 'r', encoding='utf-8') as file:
self.intents: Dict[str, Any] = json.load(file)
def analyze(self, input_text: str) -> Dict[str, Literal['ask_name', 'unknown']]:
lower_text = input_text.lower()
for phrase in self.intents.get('name_query', []):
if phrase in lower_text:
return {"intent": "ask_name"}
return {"intent": "unknown"}