import json import os class DogwhistleRetriever: """ Lightweight retrieval over a curated knowledge base of documented coded/dogwhistle hate speech patterns. Keyword-based for now; can be upgraded to embeddings once the knowledge base grows. """ def __init__(self, kb_path: str = None): kb_path = kb_path or os.path.join( os.path.dirname(__file__), "knowledge_base.json" ) with open(kb_path, "r", encoding="utf-8") as f: self.entries = json.load(f) def retrieve(self, text: str, max_results: int = 3) -> list: """Return KB entries whose surface_pattern overlaps with the text.""" text_lower = text.lower() matches = [] for entry in self.entries: pattern_words = entry["surface_pattern"].lower().split() if any(word in text_lower for word in pattern_words if len(word) > 3): matches.append(entry) return matches[:max_results]