File size: 984 Bytes
88e91df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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]