File: //opt/textanalyse/tools/dictionary_tool.py
# tools/dictionary_tool.py
import os
import re
from typing import Set, List
from pathlib import Path
from config import DICTIONARY_FILE_PATH # Use path from config
from utils.text_helpers import split_into_words
# Helper function to load words (kept concise)
def _load_dictionary_words(file_path: Path) -> Set[str]:
"""Loads the official word list from a file into a lowercase set."""
words: Set[str] = set()
if not file_path.is_file():
print(f"Error: Dictionary file not found at {file_path}")
return words
try:
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
word = line.strip()
if word:
# Store words in lowercase for case-insensitive matching
words.add(word.lower())
print(f"Loaded {len(words)} words from dictionary: {file_path}")
except Exception as e:
print(f"Error loading dictionary '{file_path}': {e}")
return words
# Load words once when the module is imported
OFFICIAL_WORDS_SET = _load_dictionary_words(DICTIONARY_FILE_PATH)
# Define the tool function for the agent
def is_word_official(word: str) -> bool:
"""
Checks if a word (case-insensitive) is in the official word list.
Args:
word (str): The word to check.
Returns:
bool: True if the word is in the official list, False otherwise.
"""
if not isinstance(word, str) or not word:
return False
# Check against the lowercase set
return word.lower() in OFFICIAL_WORDS_SET
# Neuer Wrapper für den Dictionary-Agenten
def check_word_in_dictionary(text: str) -> List[str]:
"""
Checks a text against the official dictionary and returns a list
of words that are NOT in the official list.
Args:
text (str): Text to check against the dictionary.
Returns:
List[str]: List of words not found in the official dictionary.
Returns an empty list if all words are official or if text is empty.
"""
if not text or not isinstance(text, str):
return []
# Split the text into words and check each one
words = split_into_words(text)
unofficial_words = []
for word in words:
if word and not is_word_official(word):
unofficial_words.append(word)
return unofficial_words
# Optional: Define other dictionary-related tools here if needed
# e.g., def get_official_word_suggestions(misspelled_word: str) -> list[str]:...