-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghost.py
170 lines (142 loc) · 6.48 KB
/
ghost.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
import random
import numpy as np
from emotions import Emotion
from textblob import TextBlob
from word import Word, Words
import text2emotion as te
import markovify
'''
Ghost class definition, includes the ghost emotion, ghost antithetical emotion, the ghost's current collection of words, and handling of ghost choices
'''
class Ghost:
def __init__(self):
emotion_list = ["Angry", "Fear", "Happy", "Sad", "Surprise"]
self.feeling = random.choice(emotion_list)
emotion_list.remove(self.feeling)
self.opposite = random.choice(emotion_list)
self.read_rate = random.randint(1,10) # number of sentences to read
#self.current_power = random.randint(0,30) # starting power?
self.sentences = []
self.words = Words(self.feeling, self.opposite)
self.markov_blob = None
# def get_power_words(self):
# new_set = []
# for word in self.words:
# if word.type == "Power":
# new_set.append(word)
# return new_set
# def get_opposite_words(self):
# return np.select([self.words.type == "Obstacle"],self.words)
# def get_other_words(self):
# return np.select([self.words.type == "Null"], self.words)
# def get_power(self):
# return np.sum(self.get_power_words().power)
def read(self,story,timestep):
# Get the story text
#f = open(story, "r")
#text_blob_object = TextBlob(f.read())
#f.close()
# Get the current state of the story and read where the ghost is in the story at it's read rate
start_sentence = timestep*self.read_rate
# Check that the start word isn't greater than the number of sentences
# if it is then you're done
if(len(story.sentences) < start_sentence):
return str(story), 'DONE'
new_story = ""
for i in range(start_sentence):
new_story += str(story.sentences[i]) + " "
# otherwise start reading until either the story is over or you've read your read rate
# Get chunk starting at start_word and read
for i in range(start_sentence, start_sentence + self.read_rate):
if(i < len(story.sentences)):
#print("------------")
#print("Original Sentence: " + str(story.sentences[i]))
new_sentence = self.parse_sentence(story.sentences[i])
story.sentences[i] = new_sentence
new_story += str(new_sentence) + " "
#if(str(new_sentence) != str(story.sentences[i])):
#print("------------")
#print("String mismatch", flush=True)
#print("New Sentence: " + str(story.sentences[i]), flush=True)
if(start_sentence + self.read_rate<len(story.sentences)):
for i in range(start_sentence + self.read_rate, len(story.sentences)):
new_story +=str(story.sentences[i]) + " "
#self.write(story, story.text)
# If still reading, return READING, otherwise, return DONE
if(start_sentence + self.read_rate >= len(story.sentences)):
return new_story, 'DONE'
else:
return new_story, 'READING'
def parse_sentence(self, sentence):
emotion_analysis = te.get_emotion(str(sentence))
if emotion_analysis[self.opposite] > emotion_analysis[self.feeling]:
# Re write it
sentence = self.rewrite(sentence)
elif emotion_analysis[self.feeling] > emotion_analysis[self.opposite]:
# Take its words
#self.words_markov = markovify.combine([self.words_markov,markovify.Text(str(sentence))],[1,1])
self.sentences.append(str(sentence))
self.markov_blob = markovify.Text(self.sentences)
for word in sentence.words:
#self.words.append(Word(word,te.get_emotion(word),self.feeling,self.opposite))
self.words.add_word(word)
#for word in word_bag:
#if word == self.SCREAM:
# power surge?
return sentence
def rewrite(self, sentence):
if(self.markov_blob == None):
return sentence
emotion_analysis = te.get_emotion(str(sentence))
#b.sentence =
#used_words = []
#words_to_use = self.get_power_words().copy()
#no_power = False
i = 0
#while emotion_analysis[self.opposite] >= emotion_analysis[self.feeling] and not no_power:
while emotion_analysis[self.opposite] >= emotion_analysis[self.feeling] and i < 5:
used_words = []
#words_to_use = self.get_power_words().copy()
# Change the sentence
test_sentence = self.markov_blob.make_short_sentence(len(str(sentence)))
if(test_sentence != None):
emotion_analysis = te.get_emotion(test_sentence)
if(emotion_analysis[self.opposite] < emotion_analysis[self.feeling]):
new_blob = TextBlob(test_sentence)
if(self.words.has_words(new_blob.sentences[0].words)):
sentence = new_blob.sentences[0]
self.words.remove_words(new_blob.sentences[0].words)
i = 5
else:
i = 5
i += 1
# Remove negative words? Find markovify fits for good words?
# Tell it to just change words until it gets a good score set?
# for word in sentence.words:
# pos, neg = self.parse_word(word)
# if(neg > pos):
# if len(words_to_use) > 0:
# new = random.choice(words_to_use)
# words_to_use.remove(new)
# used_words.append(new)
# word = new
# if len(words_to_use) < 1:
# no_power = True
# used_words = []
# break
#emotion_analysis = te.get_emotion(str(sentence))
#if(i < 5 or (emotion_analysis[self.opposite] < emotion_analysis[self.feeling])):
# Remove all the words we used
#for word in used_words:
# self.words.remove(word)
return sentence
def parse_word(self,word):
analyse = te.get_emotion(word)
positive = analyse[self.feeling]
negative = analyse[self.opposite]
return positive, negative
def write(self, story, text):
f.open(story, "w")
f.write(text)
f.close()