File size: 2,056 Bytes
267d8fd
 
3094b45
49d699c
267d8fd
3326ef5
267d8fd
3326ef5
 
 
 
 
 
 
 
 
 
 
 
 
 
267d8fd
 
3326ef5
49d699c
3326ef5
 
49d699c
3094b45
49d699c
3094b45
3326ef5
49d699c
 
3326ef5
267d8fd
 
3326ef5
 
 
 
 
 
ea9c667
 
 
3326ef5
 
 
 
 
 
 
 
 
 
 
 
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import spacy
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from spacy.cli import download


class DeepPragmaEngine:
    """

    Auxiliary lexical/syntactic analyzer.



    NOTE: these heuristics are NOT the primary classifier for sarcasm/hate —

    that responsibility belongs to GroqClient.classify(), since irony relies

    on pragmatic context that word-level features can't reliably capture.

    This class only surfaces supporting signals (sentiment, a naive

    syntactic "attack" pattern) to log or display alongside the LLM's

    decision, not to gate it.

    """

    HATE_TERMS = ["stupid", "useless", "disease", "parasite"]
    IRONY_MARKERS = ["always", "oh", "sure"]

    def __init__(self):
        try:
            nltk.data.find("sentiment/vader_lexicon.zip")
        except LookupError:
            nltk.download("vader_lexicon")

        model_name = "en_core_web_sm"
        try:
            self.nlp = spacy.load(model_name)
        except OSError:
            print(f"Model {model_name} not found. Downloading...")
            download(model_name)
            self.nlp = spacy.load(model_name)

        self.sia = SentimentIntensityAnalyzer()

    def get_signals(self, text: str) -> dict:
        """Compute auxiliary lexical/syntactic signals (informational only)."""
        doc = self.nlp(text)
        sentiment = self.sia.polarity_scores(text)

        attack_pattern = False
        for token in doc:
            if token.dep_ == "nsubj" and token.head.lemma_ == "be":
                for child in token.head.children:
                    if child.dep_ in ("acomp", "attr") and child.text.lower() in self.HATE_TERMS:
                        attack_pattern = True

        irony_hint = (sentiment["pos"] > 0.3) and any(
            marker in text.lower() for marker in self.IRONY_MARKERS
        )

        return {
            "sentiment": sentiment,
            "attack_pattern": attack_pattern,
            "irony_hint": irony_hint,
        }