-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtone.py
More file actions
149 lines (116 loc) · 4.83 KB
/
tone.py
File metadata and controls
149 lines (116 loc) · 4.83 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#This might have been better off being a class.
import random
import re
from tone_words import *
# (min_score, max_score, label) (where bounds meet, the more negative score is chosen)
sentiment_ranges = [
(-1.0, -0.1, "negative"),
(-0.1, -0.02, "melancholic"),
(-0.02, 0.03, "neutral"),
(0.03, 0.14, "hopeful"),
(0.14, 1.0, "positive")
]
positive_sentiment = ["hopeful", "positive"]
negative_sentiment = ["melancholic", "negative"]
def get_sentiment_label(score, ranges=sentiment_ranges):
for min_val, max_val, label in ranges:
if min_val <= score <= max_val:
return label
symbol = "Neural Network"
house = "category"
tone = "neutral"
#second sentence must have some prediction in it. let us say there are some base templates
neutral_templates = ["Expect {} in your future.",
"There are {} nearing you.",
"You will be visited by {}.",
"Only {} await you.",
"You may find {} ahead.",
"Be not afraid of the {} ahead.",
"Your omen comes in the form of {}.",
"There will be {}.",
"Your path is lined with {}.",
"Try to understand the {}.",
"Some {} are in your future."]
negative_templates = ["Visions of {} will follow you.",
"Be wary of the {}.",
"Watch carefully of your {}.",
"Be warned of the {}.",
"There will be {} that haunt you.",
"You will be befallen by the {}.",
"Evil {} are in your future."]
positive_templates = ["Your luck will lead you to {}.",
"Prosperity and {} will visit you.",
"Your path will be plentifully lined with {}.",
"You will be rewarded with {}.",
"Gifts of {} await you.",
"Golden {} are in your future."]
def get_symbol(prop_nouns):
prop_nouns = [x for x in prop_nouns if len(x) > 2] #filter for only symbols above 2 characters
if len(prop_nouns) == 0:
return "King of Knights" #default
index = random.randint(0, len(prop_nouns)-1)
return prop_nouns[index].title()
def get_house(categories):
meta_keywords = [
"articles", "pages", "wikipedia", "stub", "short description",
"coordinates", "infobox", "template", "use dmy", "use mdy",
"all articles", "commons", "dead external links", "official website",
"redirects", "containing", "Cs1"
]
filtered = []
for cat in categories:
cat_lower = cat.lower()
if re.search(r"use .* english", cat_lower): #filter out a weird set of categories
continue
# remove meta keywords
if any(mk in cat_lower for mk in meta_keywords):
continue
# remove purely numeric/date categories
if re.match(r"^\d{3,4}.*", cat):
continue
# remove categories with too generic words (optional)
if any(word in cat_lower for word in ["list", "links", "cs1"]):
continue
filtered.append(cat)
if len(filtered) == 0:
return "Mirrors" #default in case no good category
else:
index = random.randint(0, len(filtered)-1)
return filtered[index].title()
def get_tone_word(tone):
tone_mapping = {
"neutral": neutral,
"hopeful": hopeful,
"positive": positive,
"melancholic": melancholic,
"negative": negative
}
words = tone_mapping[tone]
return random.choice(words)
def get_omen_sentence(tone, nouns):
sentence = ""
filler = get_sentence_filler(nouns)
if tone in positive_sentiment:
index = random.randint(0, len(positive_templates)-1)
sentence = positive_templates[index]
elif tone in negative_sentiment:
index = random.randint(0, len(negative_templates)-1)
sentence = negative_templates[index]
else:
index = random.randint(0, len(neutral_templates)-1)
sentence = neutral_templates[index]
return sentence.format(filler)
def get_sentence_filler(sing_nouns):
sing_nouns = [x for x in sing_nouns if x != "links"] #appears a lot, is useless
if len(sing_nouns) == 0:
return "infinite paths"
index = random.randint(0, len(sing_nouns)-1)
return sing_nouns[index].lower()
def get_omen(tone_score, prop_nouns, sing_nouns, categories):
sentiment = get_sentiment_label(tone_score)
symbol = get_symbol(prop_nouns)
house = get_house(categories)
tone = get_tone_word(sentiment)
sentence = get_omen_sentence(sentiment, sing_nouns)
omen = f"You have drawn {symbol} from the house of {house}, a sign of {tone}. {sentence}"
return omen