Update goodneutraloreviltext
Browse filesimport transformers
from collections import Counter
# Load the sentiment pipeline once (do this outside the function for speed)
pipeline = transformers.pipeline(
"sentiment-analysis",
model="distilbert-base-uncased-finetuned-sst-2-english",
return_all_scores=False
)
# Your moral vocabulary
good_words = {
"love", "kindness", "peace", "joy", "happiness",
"help", "care", "respect", "honor", "justice",
"compassion", "forgiveness", "friendship", "empathy", "generosity"
}
evil_words = {
"hate", "cruelty", "war", "pain", "suffering",
"harm", "disrespect", "dishonor", "injustice",
"violence", "torture", "oppression", "betrayal", "greed", "malice"
}
def judge_text_v2(text: str) -> str:
"""
Returns 'Good', 'Evil', or 'Neutral' based on sentiment + moral keywords.
"""
# 1. Whole-sentence sentiment (-1 to +1)
result = pipeline(text)[0]
sentiment_score = result['score'] if result['label'] == 'POSITIVE' else -result['score']
# 2. Count explicit moral words (exact word match, case-insensitive)
lower_text = text.lower()
good_bonus = sum(1 for w in good_words if w in lower_text)
evil_bonus = sum(1 for w in evil_words if w in lower_text)
# 3. Final score
final_score = sentiment_score + 1.2 * good_bonus - 1.2 * evil_bonus
# 4. Judgment
if final_score >= 0.4:
return "Good"
elif final_score <= -0.4:
return "Evil"
else:
return "Neutral"
# ===========================
# Test it!
# ===========================
test_sentences = [
"I love you so much",
"I hate everyone and want to watch the world burn",
"This is just a normal day",
"We should spread kindness and forgiveness to all people",
"War brings nothing but pain, suffering, and injustice",
"I don't care",
"Justice and peace will prevail",
"Cruelty and greed destroy everything good"
]
for s in test_sentences:
print(f'"{s}" → {judge_text_v2(s)}')
- goodneutraloreviltext +63 -46
|
@@ -1,47 +1,64 @@
|
|
| 1 |
-
# Import the transformers and nltk libraries
|
| 2 |
import transformers
|
| 3 |
-
import
|
| 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 |
-
if
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import transformers
|
| 2 |
+
from collections import Counter
|
| 3 |
+
|
| 4 |
+
# Load the sentiment pipeline once (do this outside the function for speed)
|
| 5 |
+
pipeline = transformers.pipeline(
|
| 6 |
+
"sentiment-analysis",
|
| 7 |
+
model="distilbert-base-uncased-finetuned-sst-2-english",
|
| 8 |
+
return_all_scores=False
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
# Your moral vocabulary
|
| 12 |
+
good_words = {
|
| 13 |
+
"love", "kindness", "peace", "joy", "happiness",
|
| 14 |
+
"help", "care", "respect", "honor", "justice",
|
| 15 |
+
"compassion", "forgiveness", "friendship", "empathy", "generosity"
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
evil_words = {
|
| 19 |
+
"hate", "cruelty", "war", "pain", "suffering",
|
| 20 |
+
"harm", "disrespect", "dishonor", "injustice",
|
| 21 |
+
"violence", "torture", "oppression", "betrayal", "greed", "malice"
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
def judge_text_v2(text: str) -> str:
|
| 25 |
+
"""
|
| 26 |
+
Returns 'Good', 'Evil', or 'Neutral' based on sentiment + moral keywords.
|
| 27 |
+
"""
|
| 28 |
+
# 1. Whole-sentence sentiment (-1 to +1)
|
| 29 |
+
result = pipeline(text)[0]
|
| 30 |
+
sentiment_score = result['score'] if result['label'] == 'POSITIVE' else -result['score']
|
| 31 |
+
|
| 32 |
+
# 2. Count explicit moral words (exact word match, case-insensitive)
|
| 33 |
+
lower_text = text.lower()
|
| 34 |
+
good_bonus = sum(1 for w in good_words if w in lower_text)
|
| 35 |
+
evil_bonus = sum(1 for w in evil_words if w in lower_text)
|
| 36 |
+
|
| 37 |
+
# 3. Final score
|
| 38 |
+
final_score = sentiment_score + 1.2 * good_bonus - 1.2 * evil_bonus
|
| 39 |
+
|
| 40 |
+
# 4. Judgment
|
| 41 |
+
if final_score >= 0.4:
|
| 42 |
+
return "Good"
|
| 43 |
+
elif final_score <= -0.4:
|
| 44 |
+
return "Evil"
|
| 45 |
+
else:
|
| 46 |
+
return "Neutral"
|
| 47 |
+
|
| 48 |
+
# ===========================
|
| 49 |
+
# Test it!
|
| 50 |
+
# ===========================
|
| 51 |
+
test_sentences = [
|
| 52 |
+
"I love you so much",
|
| 53 |
+
"I hate everyone and want to watch the world burn",
|
| 54 |
+
"This is just a normal day",
|
| 55 |
+
"We should spread kindness and forgiveness to all people",
|
| 56 |
+
"War brings nothing but pain, suffering, and injustice",
|
| 57 |
+
"I don't care",
|
| 58 |
+
"Justice and peace will prevail",
|
| 59 |
+
"Cruelty and greed destroy everything good"
|
| 60 |
+
]
|
| 61 |
+
|
| 62 |
+
for s in test_sentences:
|
| 63 |
+
print(f'"{s}" → {judge_text_v2(s)}')
|
| 64 |
+
|