MCAce commited on
Commit
ef90192
·
verified ·
1 Parent(s): 5f94494

Update goodneutraloreviltext

Browse files

import 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)}')

Files changed (1) hide show
  1. goodneutraloreviltext +63 -46
goodneutraloreviltext CHANGED
@@ -1,47 +1,64 @@
1
- # Import the transformers and nltk libraries
2
  import transformers
3
- import nltk
4
- # Load the pre-trained model and tokenizer
5
- model = transformers.pipeline("sentiment-analysis")
6
- tokenizer = transformers.AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
7
- # Define a list of good words
8
- good_words = ["love", "kindness", "peace", "joy", "happiness", "help", "care", "respect", "honor", "justice"]
9
- # Define a list of evil words
10
- evil_words = ["hate", "cruelty", "war", "pain", "suffering", "harm", "disrespect", "dishonor", "injustice"]
11
- # Define a function that takes a text as input and returns a judgment
12
- def judge_text(text):
13
- # Tokenize the text into words
14
- words = nltk.word_tokenize(text)
15
- # Initialize a score variable
16
- score = 0
17
- # Loop through the words
18
- for word in words:
19
- # Add the special tokens for the model
20
- word = tokenizer.encode(word, add_special_tokens=True)
21
- # Get the model output for the word
22
- output = model(word)
23
- # Get the sentiment label and score from the output
24
- label = output[0]["label"]
25
- score += output[0]["score"]
26
- # If the label is NEGATIVE, multiply the score by -1
27
- if label == "NEGATIVE":
28
- score *= -1
29
- # If the word is in the good words list, increase the score by 1
30
- if word.lower() in good_words:
31
- score += 1
32
- # If the word is in the evil words list, decrease the score by 1
33
- elif word.lower() in evil_words:
34
- score -= 1
35
- # If the score is positive, return "Good"
36
- if score > 0:
37
- return "Good"
38
- # If the score is negative, return "Evil"
39
- elif score < 0:
40
- return "Evil"
41
- # If the score is zero, return "Neutral"
42
- else:
43
- return "Neutral"
44
- # Test the function with some examples
45
- print(judge_text("I love you")) # Good
46
- print(judge_text("I hate you")) # Evil
47
- print(judge_text("I don't care")) # Evil
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+