Spaces:
Running
on
Zero
Running
on
Zero
# motif_tagging.py | |
import re | |
MOTIF_PATTERNS = { | |
"physical_threat": [ | |
r"\b(i am going to|i'll) (hurt|kill|break|end|ruin|destroy) you\b", | |
r"\bsay goodbye to (you|those)? (kneecaps|teeth|face)\b", | |
r"\b(i'll|i will) put you in a (grave|hole|rose garden)\b", | |
r"\bsleep with one eye open( you'll see what happens)?\b", | |
r"\b(i'll|i will) make you disappear\b", | |
r"\b(i'll|i will|ill) put you in a rose garden\b" | |
], | |
"extreme_control": [ | |
r"\b(i decide|i control) who you (see|talk to|text|spend time with)\b", | |
r"\b(you('re| are) not allowed to)\b", | |
r"\byou (don't|do not) get to (leave|say no|argue)\b", | |
r"\bi own you\b", | |
], | |
"suicidal_threat": [ | |
r"\b(i'll|i will) kill myself\b", | |
r"\bi (don’t|do not) want to live if you leave\b", | |
r"\b(i’ll|i will) die without you\b", | |
r"\byou(’ll|'ll) regret it when i(’m|'m) gone\b", | |
], | |
} | |
def detect_motifs(text): | |
tags = [] | |
matched_phrases = [] | |
text_lower = text.lower() | |
for label, patterns in MOTIF_PATTERNS.items(): | |
for pattern in patterns: | |
if re.search(pattern, text_lower): | |
tags.append(label) | |
matched_phrases.append((label, pattern)) | |
break | |
return tags, matched_phrases |