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

23 lines
637 B
Python
Raw Normal View History

2024-05-05 21:42:45 +02:00
import json
2024-05-07 23:34:34 +02:00
import re
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):
2024-05-07 23:34:34 +02:00
ask_name: 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:
2024-05-07 23:34:34 +02:00
self.intents: Dict[str, List[str]] = 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-07 23:34:34 +02:00
input_text = input_text.lower()
for intent, patterns in self.intents.items():
for pattern in patterns:
if re.search(pattern, input_text):
return intent
2024-05-07 21:40:14 +02:00
return "unknown"