-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacterSwap.py
257 lines (240 loc) · 9.15 KB
/
characterSwap.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#! /usr/bin/env python2
"""
Filename: characterSwap.py
Author: Emily Daniels
Date: November 2015
Purpose: Swaps the names and genders of characters in a novel.
"""
import re
import string
import os
import characterDict
class SwapText(object):
def __init__(self, filename):
self.filename = filename
self.is_mixed = False
self.text = ""
self.split_text = []
self.opposite_names = {}
self.they_names = {}
self.she_names = {}
self.he_names = {}
self.opposite_name_swapped_text = []
self.they_name_swapped_text = []
self.she_name_swapped_text = []
self.he_name_swapped_text = []
self.opposite_pronouns = {}
self.they_pronouns = {}
self.she_pronouns = {}
self.he_pronouns = {}
self.opposite_pronoun_swapped_text = []
self.they_pronoun_swapped_text = []
self.she_pronoun_swapped_text = []
self.he_pronoun_swapped_text = []
self.read_text()
self.split_into_sentences()
self.create_names()
self.replace_names('opposite')
self.replace_names('they')
self.replace_names('she')
self.replace_names('he')
self.create_pronouns()
self.replace_pronouns('opposite')
self.replace_pronouns('they')
self.replace_pronouns('she')
self.replace_pronouns('he')
self.write('opposite')
self.write('they')
self.write('she')
self.write('he')
def read_text(self):
"""
Reads the text from a text file.
"""
with open(self.filename, "rb") as f:
self.text = f.read()
return self.text
def split_into_sentences(self):
"""
Split sentences on .?! "" and not on abbreviations of titles.
"""
sentence_enders = re.compile(r"""
# Split sentences on whitespace between them.
(?: # Group for two positive lookbehinds.
(?<=[.!?]) # Either an end of sentence punct,
| (?<=[.!?]['"]) # or end of sentence punct and quote.
) # End group of two positive lookbehinds.
(?<! Mr\. ) # Don't end sentence on "Mr."
(?<! Mrs\. ) # Don't end sentence on "Mrs."
(?<! Ms\. ) # Don't end sentence on "Ms."
(?<! Jr\. ) # Don't end sentence on "Jr."
(?<! Dr\. ) # Don't end sentence on "Dr."
(?<! Prof\. ) # Don't end sentence on "Prof."
(?<! Sr\. ) # Don't end sentence on "Sr."
(?<! St\. ) # Don't end sentence on "St."
(?<! M\. ) # Don't end sentence on "M."
\s+ # Split on whitespace between sentences.
""", re.IGNORECASE | re.VERBOSE)
self.split_text = sentence_enders.split(self.text)
return self.split_text
def create_names(self):
"""
Creates dictionaries of all name types, given created name csv files
in a names folder.
"""
self.opposite_names = characterDict.CreateDict(os.path.abspath(
"names/opposite_names.csv"))
self.they_names = characterDict.CreateDict(os.path.abspath(
"names/they_names.csv"))
self.she_names = characterDict.CreateDict(os.path.abspath(
"names/she_names.csv"))
self.he_names = characterDict.CreateDict(os.path.abspath(
"names/he_names.csv"))
def replace_names(self, type):
"""
Replaces text with all name types.
"""
if type == 'opposite':
self.opposite_name_swapped_text = self.swap_names(
self.opposite_names)
if type == 'they':
self.they_name_swapped_text = self.swap_names(
self.they_names)
if type == 'she':
self.she_name_swapped_text = self.swap_names(
self.she_names)
if type == 'he':
self.he_name_swapped_text = self.swap_names(
self.he_names)
def swap_names(self, names):
"""
Replaces the original character names with the specified names.
"""
name_swapped_text = []
for line in self.split_text:
for old, new in names:
line = line.replace(old, new)
name_swapped_text.append(line)
return name_swapped_text
def create_pronouns(self):
"""
Creates dictionaries of all pronoun types, creating a path to where
characterSwap is located.
"""
self.opposite_pronouns = characterDict.CreateDict(
os.path.abspath(os.path.join(os.path.dirname(__file__),
"opposite_pronouns.csv")))
self.they_pronouns = characterDict.CreateDict(
os.path.abspath(os.path.join(os.path.dirname(__file__),
"they_pronouns.csv")))
self.she_pronouns = characterDict.CreateDict(
os.path.abspath(os.path.join(os.path.dirname(__file__),
"she_pronouns.csv")))
self.he_pronouns = characterDict.CreateDict(
os.path.abspath(os.path.join(os.path.dirname(__file__),
"he_pronouns.csv")))
def replace_pronouns(self, type):
"""
Replaces text with all pronoun types.
"""
if type == 'opposite':
self.is_mixed = True
self.opposite_pronoun_swapped_text = self.swap_pronouns(
self.opposite_pronouns, self.opposite_name_swapped_text)
if type == 'they':
self.is_mixed = False
self.they_pronoun_swapped_text = self.swap_pronouns(
self.they_pronouns, self.they_name_swapped_text)
if type == 'she':
self.is_mixed = False
self.she_pronoun_swapped_text = self.swap_pronouns(
self.she_pronouns, self.she_name_swapped_text)
if type == 'he':
self.is_mixed = False
self.he_pronoun_swapped_text = self.swap_pronouns(
self.he_pronouns, self.he_name_swapped_text)
def swap_pronouns(self, pronouns, name_swapped_text):
"""
Replaces the original character pronouns with the specified pronouns.
"""
pronoun_swapped_text = []
for line in name_swapped_text:
words = line.split()
new_words = []
for word in words:
for old, new in pronouns:
word = self.check_word(word, old, new)
new_words.append(word)
new_line = ' '.join(new_words)
pronoun_swapped_text.append(new_line)
return pronoun_swapped_text
def check_word(self, word, old, new):
"""
Checks and replaces words based on the word lists.
"""
# remove word starters
starter = False
add_starter = ""
# remove word enders
ender = False
add_ender = ""
for punc in set(string.punctuation):
while word.startswith(punc):
starter = True
add_starter = punc + add_starter
word = word[1:]
while word.endswith(punc):
ender = True
add_ender = punc + add_ender
word = word[:-1]
# remove possession
possessive = False
if word.endswith("'s"):
possessive = True
word = word[:-2]
# compare word to word lists
word = self.replace_word(word, old, new)
# add back word enders and possession
if possessive:
word += "'s"
if ender:
word += add_ender
if starter:
word = add_starter + word
return word
def replace_word(self, word, old, new):
"""
Compares words without punctuation or case and replaces
the word if needed.
"""
if word.lower() == old:
if word[0].isupper():
word = word.replace(word, new.title())
else:
word = word.replace(word, new)
elif self.is_mixed and word.lower() == new:
if word[0].isupper():
word = word.replace(word, old.title())
else:
word = word.replace(word, old)
return word
def write(self, type):
"""
Writes each pronoun swapped text to a text file.
"""
if type == 'opposite':
self.write_text('Opposite_', self.opposite_pronoun_swapped_text)
if type == 'they':
self.write_text('They_', self.they_pronoun_swapped_text)
if type == 'she':
self.write_text('She_', self.she_pronoun_swapped_text)
if type == 'he':
self.write_text('He_', self.he_pronoun_swapped_text)
def write_text(self, modifier, pronoun_swapped_text):
"""
Writes the modified text to a text file.
"""
regex = re.compile(r'.{1,80}(?:\s+|$)')
with open(modifier + self.filename, "wb") as f:
f.write('\n'.join(s.rstrip() for line in pronoun_swapped_text
for s in regex.findall(line)))