-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathnovelizer_v1.py
418 lines (325 loc) · 9.24 KB
/
novelizer_v1.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
from sys import argv
import random
from random import choice as c
import string
import math
import nltk
import en
from erowid_experience_paths import *
from tropes_character import *
from tropes_setting import *
from firstnames_f import *
from firstnames_m import *
from surnames import *
script, outputFileName, inputCharName = argv
def name_chars():
charNo = random.randint(7,51)
charGenders = [random.randint(0,1) for _ in range(charNo)]
charNames = [inputCharName]
for i in charGenders:
if charGenders[i]:
charNames.append(c(fFirstNames)+" "+c(surnames))
else:
charNames.append(c(mFirstNames)+" "+c(surnames))
return charNames
def char_match():
charNames = name_chars()
charTropes = random.sample(characterTropeFiles, len(charNames))
tropeText = []
for tropePath in charTropes:
f = open(tropePath, 'r')
tropeText.append(f.read())
f.close()
# charTropeDict = dict(zip(charNames, tropeText))
tropes = []
for t in range(len(charNames)):
trope = personalize(charNames[t], tropeText[t])
tropes.append(trope)
charDrugPaths = random.sample(erowidExpPaths, len(charNames)*5)
charDrugs = []
j = 0
for path in charDrugPaths:
ff = open(path, 'r')
drugText = ff.read()
ff.close()
splitText = drugText.split('\\vspace{2mm}')
endOfText = splitText[-1]
report = endOfText[:len(endOfText)-15]
chars = [charNames[int(math.floor(j/5))]]+random.sample(charNames, random.randint(0, 3))
report = personal_trip(chars, report)
charDrugs.append(report)
j+=1
charSettingPaths = random.sample(settingTropeFiles, len(charNames))
charSettings = []
k = 0
for path in charSettingPaths:
fff = open(path, 'r')
settingText = fff.read()
fff.close()
churs = [charNames[k]]+random.sample(charNames, random.randint(0, 3))
setting = personal_trip(churs, settingText)
charSettings.append(setting)
k+=1
charTropeDict = {}
for i in range(len(charNames)):
drugsIndex = i*5
charTropeDict[charNames[i]] = [tropes[i], charDrugs[drugsIndex:drugsIndex+5], charSettings[i]]
return charTropeDict
def personal_trip(c, t):
charCount = len(c)
nameList1 = c[0].split(' ')
firstName1 = nameList1[0]
lastName1 = nameList1[1]
if charCount > 1:
nameList2 = c[1].split(' ')
firstName2 = nameList2[0]
lastName2 = nameList2[1]
if charCount > 2:
nameList3 = c[2].split(' ')
firstName3 = nameList3[0]
lastName3 = nameList3[1]
if charCount > 3:
nameList4 = c[3].split(' ')
firstName4 = nameList4[0]
lastName4 = nameList4[1]
t = t.split('\n')
t = ' '.join(t)
try:
pos = en.sentence.tag(t)
wordtag = map(list, zip(*pos))
words = wordtag[0]
tags = wordtag[1]
pronoun_count = 0
for i in range(len(words)):
if tags[i] == "PRP":
if pronoun_count % charCount == 0:
words[i] = firstName1
elif pronoun_count % charCount == 1:
words[i] = firstName2
elif pronoun_count % charCount == 2:
words[i] = firstName3
elif pronoun_count % charCount == 3:
words[i] = firstName4
pronoun_count += 1
elif tags[i] == "PRP$":
if pronoun_count % charCount == 0:
words[i] = firstName1+"\'s"
elif pronoun_count % charCount == 1:
words[i] = firstName2+"\'s"
elif pronoun_count % charCount == 2:
words[i] = firstName3+"\'s"
elif pronoun_count % charCount == 3:
words[i] = firstName4+"\'s"
pronoun_count += 1
elif tags[i] in ["VBD", "VBG", "VBN", "VBZ"]:
try:
words[i] = en.verb.past(words[i], person=3, negate=False)
except KeyError:
pass
else:
pass
punc = [".", ",", ";", ":", "!", "?"]
for i in range(len(words)):
if words[i] in punc:
words[i] = '`'+words[i]
final_text = " ".join(words)
final_text = final_text.decode('utf8')
final_text = final_text.encode('ascii', 'ignore')
final_text = string.replace(final_text, "\\end{itemize}", "")
final_text = string.replace(final_text, "\\begin{itemize}", "")
final_text = string.replace(final_text, "\\end{center}", "")
final_text = string.replace(final_text, "\\begin{center}", "")
final_text = string.replace(final_text, "\\ldots", " . . . ")
final_text = string.replace(final_text, "\\egroup", "")
final_text = string.replace(final_text, "EROWID", "GOVERNMENT")
final_text = string.replace(final_text, "erowid", "government")
final_text = string.replace(final_text, "Erowid", "Government")
except:
final_text = ""
return final_text
def personalize(c, t):
nameList = c.split(' ')
firstName = nameList[0]
lastName = nameList[1]
t = t.split('\n')
t = ' '.join(t)
try:
pos = en.sentence.tag(t)
wordtag = map(list, zip(*pos))
words = wordtag[0]
tags = wordtag[1]
for i in range(len(words)):
if words[i].lower() == "character" and i > 0:
words[i-1] = firstName
words[i] = lastName
containsName = True
elif tags[i] == "PRP":
words[i] = firstName
containsName = True
elif tags[i] == "PRP$":
words[i] = firstName+"\'s"
cointainsName = True
elif tags[i] in ["VBD", "VBG", "VBN", "VBZ"]:
try:
words[i] = en.verb.past(words[i], person=3, negate=False)
except KeyError:
pass
elif words[i] == "trope":
words[i] = "clue"
elif words[i] == "tropes":
words[i] = "clues"
elif words[i] == "Trope":
words[i] = "Clue"
elif words[i] == "Tropes":
words[i] = "Clues"
elif words[i] == "have":
words[i] = "has"
elif words[i] == "are":
words[i] = "is"
else:
pass
punc = [".", ",", ";", ":", "!", "?"]
for i in range(len(words)):
if words[i] in punc:
words[i] = '`'+words[i]
final_text = " ".join(words)
index = string.find(final_text, firstName)
if final_text[index+len(firstName)+1:index+len(firstName)+1+len(lastName)] == lastName:
final_text = final_text[index:]
else:
final_text = firstName+" "+lastName+final_text[index+len(firstName):]
final_text = final_text.decode('utf8')
final_text = final_text.encode('ascii', 'ignore')
except:
final_text = ""
return final_text
latex_special_char_1 = ['&', '%', '$', '#', '_', '{', '}']
latex_special_char_2 = ['~', '^', '\\']
outputFile = open("output/"+outputFileName+".tex", 'w')
openingTexLines = ["\\documentclass[12pt]{book}",
"\\usepackage{ucs}",
"\\usepackage[utf8x]{inputenc}",
"\\usepackage{hyperref}",
"\\title{"+outputFileName+"}",
"\\author{collective consciousness fiction generator\\\\http://rossgoodwin.com/ficgen}",
"\\date{\\today}",
"\\begin{document}",
"\\maketitle"]
closingTexLine = "\\end{document}"
for line in openingTexLines:
outputFile.write(line+"\n\r")
outputFile.write("\n\r\n\r")
intros = char_match()
for x, y in intros.iteritems():
outputFile.write("\\chapter{"+x+"}\n\r")
chapter_type = random.randint(0, 4)
bonus_drug_trip = random.randint(0, 1)
trip_count = random.randint(1,4)
# BLOCK ONE
if chapter_type in [0, 3]:
for char in y[0]:
if char == "`":
outputFile.seek(-1, 1)
elif char in latex_special_char_1:
outputFile.write("\\"+char)
elif char in latex_special_char_2:
if char == '~':
outputFile.write("")
elif char == '^':
outputFile.write("")
elif char == '\\':
outputFile.write("-")
else:
pass
else:
outputFile.write(char)
elif chapter_type in [1, 4]:
for char in y[2]:
if char == "`":
outputFile.seek(-1, 1)
elif char in latex_special_char_1:
outputFile.write("\\"+char)
elif char in latex_special_char_2:
if char == '~':
outputFile.write("")
elif char == '^':
outputFile.write("")
elif char == '\\':
outputFile.write("-")
else:
pass
else:
outputFile.write(char)
elif chapter_type == 2:
for char in y[1][0]:
if char == "`":
outputFile.seek(-1, 1)
else:
outputFile.write(char)
outputFile.write("\n\r\n\r\n\r")
# BLOCK TWO
if chapter_type == 0:
for char in y[2]:
if char == "`":
outputFile.seek(-1, 1)
elif char in latex_special_char_1:
outputFile.write("\\"+char)
elif char in latex_special_char_2:
if char == '~':
outputFile.write("")
elif char == '^':
outputFile.write("")
elif char == '\\':
outputFile.write("-")
else:
pass
else:
outputFile.write(char)
elif chapter_type == 1:
for char in y[0]:
if char == "`":
outputFile.seek(-1, 1)
elif char in latex_special_char_1:
outputFile.write("\\"+char)
elif char in latex_special_char_2:
if char == '~':
outputFile.write("")
elif char == '^':
outputFile.write("")
elif char == '\\':
outputFile.write("-")
else:
pass
else:
outputFile.write(char)
elif chapter_type in [3, 4]:
for char in y[1][0]:
if char == "`":
outputFile.seek(-1, 1)
else:
outputFile.write(char)
elif chapter_type == 2 and bonus_drug_trip:
for tripIndex in range(trip_count):
for char in y[1][tripIndex+1]:
if char == "`":
outputFile.seek(-1, 1)
else:
outputFile.write(char)
else:
pass
outputFile.write("\n\r\n\r\n\r")
# BLOCK THREE
if chapter_type in [0, 1, 3, 4] and bonus_drug_trip:
for tripIndex in range(trip_count):
for char in y[1][tripIndex+1]:
if char == "`":
outputFile.seek(-1, 1)
else:
outputFile.write(char)
outputFile.write("\n\r\n\r\n\r")
else:
pass
outputFile.write("\n\r\n\r")
outputFile.write(closingTexLine)
outputFile.close()
print '\"output/'+outputFileName+'.tex\"'