Spaces:
Sleeping
Sleeping
Update src/engine.py
Browse files- src/engine.py +38 -22
src/engine.py
CHANGED
|
@@ -3,40 +3,56 @@ import nltk
|
|
| 3 |
from nltk.sentiment.vader import SentimentIntensityAnalyzer
|
| 4 |
from spacy.cli import download
|
| 5 |
|
|
|
|
| 6 |
class DeepPragmaEngine:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def __init__(self):
|
| 8 |
-
# Configuración de NLTK
|
| 9 |
try:
|
| 10 |
-
nltk.data.find(
|
| 11 |
except LookupError:
|
| 12 |
-
nltk.download(
|
| 13 |
-
|
| 14 |
-
# Configuración de SpaCy con autodescarga
|
| 15 |
model_name = "en_core_web_sm"
|
| 16 |
try:
|
| 17 |
self.nlp = spacy.load(model_name)
|
| 18 |
except OSError:
|
| 19 |
-
print(f"
|
| 20 |
download(model_name)
|
| 21 |
self.nlp = spacy.load(model_name)
|
| 22 |
-
|
| 23 |
self.sia = SentimentIntensityAnalyzer()
|
| 24 |
-
self.terminos_odio = ["stupid", "useless", "disease", "parasite"]
|
| 25 |
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
for token in doc:
|
| 32 |
if token.dep_ == "nsubj" and token.head.lemma_ == "be":
|
| 33 |
for child in token.head.children:
|
| 34 |
-
if child.dep_ in
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from nltk.sentiment.vader import SentimentIntensityAnalyzer
|
| 4 |
from spacy.cli import download
|
| 5 |
|
| 6 |
+
|
| 7 |
class DeepPragmaEngine:
|
| 8 |
+
"""
|
| 9 |
+
Auxiliary lexical/syntactic analyzer.
|
| 10 |
+
|
| 11 |
+
NOTE: these heuristics are NOT the primary classifier for sarcasm/hate —
|
| 12 |
+
that responsibility belongs to GroqClient.classify(), since irony relies
|
| 13 |
+
on pragmatic context that word-level features can't reliably capture.
|
| 14 |
+
This class only surfaces supporting signals (sentiment, a naive
|
| 15 |
+
syntactic "attack" pattern) to log or display alongside the LLM's
|
| 16 |
+
decision, not to gate it.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
HATE_TERMS = ["stupid", "useless", "disease", "parasite"]
|
| 20 |
+
IRONY_MARKERS = ["always", "oh", "sure"]
|
| 21 |
+
|
| 22 |
def __init__(self):
|
|
|
|
| 23 |
try:
|
| 24 |
+
nltk.data.find("sentiment/vader_lexicon.zip")
|
| 25 |
except LookupError:
|
| 26 |
+
nltk.download("vader_lexicon")
|
| 27 |
+
|
|
|
|
| 28 |
model_name = "en_core_web_sm"
|
| 29 |
try:
|
| 30 |
self.nlp = spacy.load(model_name)
|
| 31 |
except OSError:
|
| 32 |
+
print(f"Model {model_name} not found. Downloading...")
|
| 33 |
download(model_name)
|
| 34 |
self.nlp = spacy.load(model_name)
|
| 35 |
+
|
| 36 |
self.sia = SentimentIntensityAnalyzer()
|
|
|
|
| 37 |
|
| 38 |
+
def get_signals(self, text: str) -> dict:
|
| 39 |
+
"""Compute auxiliary lexical/syntactic signals (informational only)."""
|
| 40 |
+
doc = self.nlp(text)
|
| 41 |
+
sentiment = self.sia.polarity_scores(text)
|
| 42 |
+
|
| 43 |
+
attack_pattern = False
|
| 44 |
for token in doc:
|
| 45 |
if token.dep_ == "nsubj" and token.head.lemma_ == "be":
|
| 46 |
for child in token.head.children:
|
| 47 |
+
if child.dep_ in ("acomp", "attr") and child.text.lower() in self.HATE_TERMS:
|
| 48 |
+
attack_pattern = True
|
| 49 |
+
|
| 50 |
+
irony_hint = (sentiment["pos"] > 0.3) and any(
|
| 51 |
+
marker in text.lower() for marker in self.IRONY_MARKERS
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
return {
|
| 55 |
+
"sentiment": sentiment,
|
| 56 |
+
"attack_pattern": attack_pattern,
|
| 57 |
+
"irony_hint": irony_hint,
|
| 58 |
+
}
|