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

21 lines
587 B
Python
Raw Normal View History

2024-05-05 21:42:45 +02:00
import json
2024-05-07 21:40:14 +02:00
from typing import Dict, List, TypedDict
from .config import Config
2024-05-05 21:42:45 +02:00
2024-05-07 21:40:14 +02:00
class Intents(TypedDict):
name_query: List[str]
2024-05-05 21:42:45 +02:00
class NaturalLanguageProcessor:
2024-05-07 21:40:14 +02:00
def __init__(self, config: Config):
with config.data_path.open('r', encoding='utf-8') as file:
self.intents: Intents = json.load(file)
2024-05-05 21:42:45 +02:00
2024-05-07 21:40:14 +02:00
def analyze(self, input_text: str) -> str:
2024-05-05 21:42:45 +02:00
lower_text = input_text.lower()
2024-05-07 21:40:14 +02:00
for intent, phrases in self.intents.items():
if any(phrase in lower_text for phrase in phrases):
return intent
return "unknown"