Prior Installments:
Part I
Part II
For my final project in Introduction to Computational Media with Daniel Shiffman, I presented my fiction generator (working title: “FicGen”). Since my previous post about this project, I have added a graphical user interface and significantly expanded/refactored my code, which I moved to a new repository on GitHub. I have also submitted this project as my entry in the ITP Winter Show. For my Networked Media final project, which is due Friday, I plan to put FicGen online.
Here is a screenshot of the GUI, which I implemented in Processing:
When I presented this project in our final ICM class on Tuesday, November 25, the only working elements in the GUI were the text fields and the big red button. Now, most of the buttons and sliders have functionality as well. After pushing the red button, a Python script emails the completed novel to the user in PDF format.
After creating the GUI above, I expanded the material I am using to generate the novels by scraping content from two additional sources: over 2,000 sci-fi/horror stories from scp-wiki.net, and over 47,000 books from Project Gutenberg. I then significantly refactored my code to accommodate these additions. My new Python program, ficgen.py, is far more object oriented and organized than my previous plotgen script, which had become somewhat of a mess by the time I presented my project in class two weeks ago.
Here’s the current code:
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 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 |
import math import argparse import random from random import choice as rc from random import sample as rs from random import randint as ri import string import math from zipfile import ZipFile import nltk import en from g_paths import gPaths from erowid_experience_paths import erowidExpPaths from tropes_character import characterTropeFiles from tropes_setting import settingTropeFiles from scp_paths import scpPaths from firstnames_f import fFirstNames from firstnames_m import mFirstNames from surnames import surnames # TODO: # [X] CLEAN UP TROPE FILE PATHS LIST # [ ] Fix "I'm" and "I'll" problem # [ ] Add Plot Points / Narrative Points / Phlebotinum # [ ] subtrope / sub-trope # [ ] add yelp reviews # [ ] add livejournal # [X] add SCP # System Path sysPath = "/Users/rg/Projects/plotgen/ficgen/" # Argument Values genre_list = ['literary', 'sci-fi', 'fantasy', 'history', 'romance', 'thriller', 'mystery', 'crime', 'pulp', 'horror', 'beat', 'fan', 'western', 'action', 'war', 'family', 'humor', 'sport', 'speculative'] conflict_list = ['nature', 'man', 'god', 'society', 'self', 'fate', 'tech', 'no god', 'reality', 'author'] narr_list = ['first', '1st', '1', 'third', '3rd', '3', 'alt', 'alternating', 'subjective', 'objective', 'sub', 'obj', 'omniscient', 'omn', 'limited', 'lim'] parser = argparse.ArgumentParser(description='Story Parameters') parser.add_argument('--charnames', nargs='*', help="Character Names") parser.add_argument('--title', help="Story Title") parser.add_argument('--length', help="Story Length (0-999)") parser.add_argument('--charcount', help="Character Count (0-999)") parser.add_argument('--genre', nargs='*', help="Genre", choices=genre_list) parser.add_argument('--conflict', nargs='*', help="Conflict", choices=conflict_list) parser.add_argument('--passion', help="Passion (0-999)") parser.add_argument('--verbosity', help="Verbosity (0-999)") parser.add_argument('--realism', help="Realism (0-999)") parser.add_argument('--density', help="Density (0-999)") parser.add_argument('--accessibility', help="Accessibility (0-999)") parser.add_argument('--depravity', help="Depravity (0-999)") parser.add_argument('--linearity', help="Linearity (0-999)") parser.add_argument('--narrator', nargs='*', help="Narrative PoV", choices=narr_list) args = parser.parse_args() # ESTABLISH SYSTEM-WIDE COEFFICIENTS/CONSTANTS # tsv = trope setting volume TSV = (int(args.length)/2.0 + int(args.realism)/6.0 + int(args.passion)/3.0)/1000.0 if 'fan' in args.genre: TSV += 1.0 TSV = int(math.ceil(2.0*TSV)) # cc = actual number of extra characters / MAKE EXPONENTIAL CC = int(math.exp(math.ceil(int(args.charcount)/160.0))/2.0)+10 # chc = chapter count CHC = int(math.exp(math.ceil(int(args.length)/160.0))/2.0)+10 # dtv = drug trip volume DTV = (int(args.length)/4.0 + int(args.realism)/12.0 + int(args.passion)/6.0 + int(args.depravity)*1.5)/1000.0 if 'beat' in args.genre: DTV += 1.0 if 'society' in args.conflict: DTV += 1.0 DTV = int(math.ceil(5.0*DTV)) # scp = scp article volume SCP = int(args.length)/1000.0 if bool(set(['sci-fi', 'horror']) & set(args.genre)): SCP += 1.0 if bool(set(['tech', 'no god', 'reality', 'nature', 'god']) & set(args.conflict)): SCP += 1.0 SCP = int(math.ceil(2.0*SCP)) # den = length (in chars) of project gutenerg excerpts DEN = int(args.density)*10 # ggv = gutenberg excerpt volume GGV = (int(args.length) + int(args.density))/500.0 if 'literary' in args.genre: GGV += 2.0 GGV = int(math.ceil(5.0*GGV)) # chl = chapter length as percent of potential chapter length CHL = int(args.length)/1000.0 # file text fetchers def get_file(fp): f = open(sysPath+fp, 'r') t = f.read() f.close() return t def get_zip(fp): fileName = fp.split('/')[-1] noExtName = fileName.split('.')[0] txtName = noExtName + ".txt" ff = ZipFile(fp, 'r') fileNames = ff.namelist() oo = ff.open(fileNames[0], 'r') tt = oo.read() oo.close() ff.close() return tt # CLASSES class Character(object): def __init__(self, firstName, lastName): self.firstName = firstName self.lastName = lastName self.introDesc = "" self.scenes = [] self.drugTrips = [] self.scpReports = [] self.gbergExcerpts = [] self.friends = [] # list of objects class Chapter(object): def __init__(self, charObj): self.charObj = charObj self.title = "" self.blocks = [] def title_maker(self): charTitle = ri(0, 2) if not bool(charTitle): ttl = self.charObj.firstName + " " + self.charObj.lastName else: titleSource = ri(0, 3) if titleSource == 0: textSource = rc(self.charObj.scenes) elif titleSource == 1: textSource = rc(self.charObj.drugTrips) elif titleSource == 2: textSource = rc(self.charObj.scpReports) elif titleSource == 3: textSource = rc(self.charObj.gbergExcerpts) tokens = nltk.word_tokenize(textSource) if len(tokens) > 20: index = ri(0, len(tokens)-10) titleLen = ri(2, 6) ttl = ' '.join(tokens[index:index+titleLen]) else: ttl = self.charObj.firstName + " " + self.charObj.lastName self.title = ttl def chapter_builder(self): blockList = [self.charObj.introDesc] + self.charObj.scenes + self.charObj.drugTrips + self.charObj.scpReports + self.charObj.gbergExcerpts random.shuffle(blockList) stopAt = int(math.ceil(CHL*len(blockList))) blockList = blockList[:stopAt] self.blocks = blockList # self.blocks.append("stuff") class Novel(object): def __init__(self): self.title = args.title self.characters = [] # list of characters self.chapters = [] # list of chapters def generate(self): self.make_chars() self.assemble_chapters() self.make_tex_file() def make_tex_file(self): # Look at PlotGen for this part outputFileName = self.title latex_special_char_1 = ['&', '%', '$', '#', '_', '{', '}'] latex_special_char_2 = ['~', '^', '\\'] outputFile = open(sysPath+"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") for ch in self.chapters: outputFile.write("\\chapter{"+ch.title+"}\n\r") outputFile.write("\n\r\n\r") rawText = '\n\r\n\r\n\r'.join(ch.blocks) try: rawText = rawText.decode('utf8') except: pass try: rawText = rawText.encode('ascii', 'ignore') except: pass i = 0 for char in rawText: if char == "\b": outputFile.seek(-1, 1) elif char in latex_special_char_1 and rawText[i-1] != "\\": outputFile.write("\\"+char) elif char in latex_special_char_2 and not rawText[i+1] in latex_special_char_1: outputFile.write("-") else: outputFile.write(char) i += 1 outputFile.write("\n\r\n\r") outputFile.write("\n\r\n\r") outputFile.write(closingTexLine) outputFile.close() print '\"'+sysPath+'output/'+outputFileName+'.tex\"' def assemble_chapters(self): novel = [] for c in self.characters: novel.append(Chapter(c)) for ch in novel: ch.title_maker() ch.chapter_builder() random.shuffle(novel) # MAYBE RETHINK THIS LATER self.chapters = novel def make_chars(self): # establish gender ratio charGenders = [ri(0,1) for _ in range(CC)] # initialize list of characters chars = [] # add user defined characters for firstlast in args.charnames: fl_list = firstlast.split('_') # Note that split is an underscore! chars.append(Character(fl_list[0], fl_list[1])) # add generated characters for b in charGenders: if b: chars.append(Character(rc(fFirstNames), rc(surnames))) else: chars.append(Character(rc(mFirstNames), rc(surnames))) # establish list of intro scenes introScenePaths = rs(characterTropeFiles, len(chars)) # establish list of settings settings = rs(settingTropeFiles, len(chars)*TSV) # establish list of drug trips trips = rs(erowidExpPaths, len(chars)*DTV) # establish list of scp articles scps = rs(scpPaths, len(chars)*SCP) # establish list of gberg excerpts gbergs = rs(gPaths.values(), len(chars)*GGV) i = 0 j = 0 m = 0 p = 0 s = 0 for c in chars: # make friends c.friends += rs(chars, ri(1,len(chars)-1)) if c in c.friends: c.friends.remove(c) # add introduction description c.introDesc = self.personal_trope([c], introScenePaths[i]) # add setting scenes for k in range(TSV): c.scenes.append(self.personal_trope([c]+c.friends, settings[j+k])) # add drug trip scenes for n in range(DTV): c.drugTrips.append(self.personal_trip([c]+c.friends, trips[m+n])) # add scp articles for q in range(SCP): c.scpReports.append(self.personal_scp([c]+c.friends, scps[p+q])) # add gberg excerpts for t in range(GGV): c.gbergExcerpts.append(self.personal_gberg([c]+c.friends, gbergs[s+t])) i += 1 j += TSV m += DTV p += SCP s += GGV self.characters = chars def personal_trope(self, charList, filePath): text = get_file(filePath) # text = text.decode('utf8') # text = text.encode('ascii', 'ignore') if len(charList) == 1: characterTrope = True else: characterTrope = False try: pos = en.sentence.tag(text) wordtag = map(list, zip(*pos)) words = wordtag[0] tags = wordtag[1] for i in range(len(words)): charRef = rc([rc(charList), charList[0]]) if words[i].lower() == "character" and i > 0: words[i-1] = charRef.firstName words[i] = charRef.lastName elif tags[i] == "PRP": words[i] = charRef.firstName elif tags[i] == "PRP$": words[i] = charRef.firstName+"\'s" elif tags[i] in ["VBD", "VBG", "VBN", "VBZ"]: try: words[i] = en.verb.past(words[i], person=3, negate=False) except: pass if characterTrope: if words[i] == "have": words[i] = "has" elif words[i] == "are": words[i] = "is" punc = [".", ",", ";", ":", "!", "?"] for i in range(len(words)): if words[i] in punc: words[i] = '\b'+words[i] final_text = " ".join(words) if characterTrope: mainCharRef = rc(charList) index = string.find(final_text, mainCharRef.firstName) if final_text[index+len(mainCharRef.firstName)+1:index+len(mainCharRef.firstName)+1+len(mainCharRef.lastName)] == mainCharRef.lastName: final_text = final_text[index:] else: final_text = mainCharRef.firstName+" "+mainCharRef.lastName+final_text[index+len(mainCharRef.firstName):] replacements = {"trope": "clue", "Trope": "clue", "TROPE": "CLUE"} for x, y in replacements.iteritems(): final_text = string.replace(final_text, x, y) except: final_text = "" return final_text def personal_trip(self, charList, tripPath): fileText = get_file(tripPath) splitText = fileText.split('\\vspace{2mm}') endOfText = splitText[-1] text = endOfText[:len(endOfText)-15] try: pos = en.sentence.tag(text) wordtag = map(list, zip(*pos)) words = wordtag[0] tags = wordtag[1] for i in range(len(words)): charRef = rc([rc(charList), charList[0]]) if tags[i] == "PRP": words[i] = charRef.firstName elif tags[i] == "PRP$": words[i] = charRef.firstName+"\'s" elif tags[i] in ["VBD", "VBG", "VBN", "VBZ"]: try: words[i] = en.verb.past(words[i], person=3, negate=False) except: pass else: pass punc = [".", ",", ";", ":", "!", "?"] for i in range(len(words)): if words[i] in punc: words[i] = '\b'+words[i] final_text = " ".join(words) 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 personal_scp(self, charList, scpPath): text = get_file(scpPath) text = string.replace(text, "SCP", charList[0].lastName) text = string.replace(text, "Foundation", charList[0].lastName) try: pos = en.sentence.tag(text) wordtag = map(list, zip(*pos)) words = wordtag[0] tags = wordtag[1] for i in range(len(words)): charRef = rc(charList) if tags[i] == "PRP": words[i] = charRef.firstName elif tags[i] == "PRP$": words[i] = charRef.firstName+"\'s" elif tags[i] in ["VBD", "VBG", "VBN", "VBZ"]: try: words[i] = en.verb.past(words[i], person=3, negate=False) except: pass else: pass punc = [".", ",", ";", ":", "!", "?"] for i in range(len(words)): if words[i] in punc: words[i] = '\b'+words[i] final_text = " ".join(words) except: final_text = "" return final_text def personal_gberg(self, charList, gPath): full_text = "" while full_text == "": try: full_text = get_zip(gPath) except: full_text = "" gPath = rc(gPaths.values()) endPart = full_text.split("*** START OF THIS PROJECT GUTENBERG EBOOK ")[-1] theMeat = endPart.split("*** END OF THIS PROJECT GUTENBERG EBOOK")[0] theMeat = string.replace(theMeat, "\r\n", " ") if len(theMeat) < DEN+5: text = theMeat else: startLoc = int(len(theMeat)/2.0 - DEN/2.0) text = theMeat[startLoc:startLoc+DEN] spLoc = text.find(" ") text = text[spLoc+1:] try: pos = en.sentence.tag(text) wordtag = map(list, zip(*pos)) words = wordtag[0] tags = wordtag[1] for i in range(len(words)): charRef = rc([rc(charList), charList[0]]) if tags[i] == "PRP": words[i] = charRef.firstName elif tags[i] == "PRP$": words[i] = charRef.firstName+"\'s" elif tags[i] in ["VBD", "VBG", "VBN", "VBZ"]: try: words[i] = en.verb.past(words[i], person=3, negate=False) except: pass else: pass punc = [".", ",", ";", ":", "!", "?"] for i in range(len(words)): if words[i] in punc: words[i] = '\b'+words[i] final_text = " ".join(words) except: final_text = "" return final_text def print_chars(self): c = self.make_chars() for character in c: print 'INTRO DESC' print '\n\n' print character.introDesc print '\n\n' print 'SCENES' print '\n\n' for s in character.scenes: print s print '\n\n' print 'DRUG TRIPS' print '\n\n' for t in character.drugTrips: print t print '\n\n' print 'SCP REPORTS' print '\n\n' for p in character.scpReports: print p print '\n\n' print 'GBERG EXCERPTS' print '\n\n' for q in character.gbergExcerpts: print q print '\n\n' foobar = Novel() foobar.generate() |
The program’s argument values, which I’m using the Python argparse library to deal with, are designed to be inserted by the GUI. However, they can be inserted manually as well in the terminal.
Typing python ficgen.py -h
in the terminal will yield the following help text:
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 |
usage: ficgen.py [-h] [--charnames [CHARNAMES [CHARNAMES ...]]] [--title TITLE] [--length LENGTH] [--charcount CHARCOUNT] [--genre [{literary,sci-fi,fantasy,history,romance,thriller,mystery,crime,pulp,horror,beat,fan,western,action,war,family,humor,sport,speculative} [{literary,sci-fi,fantasy,history,romance,thriller,mystery,crime,pulp,horror,beat,fan,western,action,war,family,humor,sport,speculative} ...]]] [--conflict [{nature,man,god,society,self,fate,tech,no god,reality,author} [{nature,man,god,society,self,fate,tech,no god,reality,author} ...]]] [--passion PASSION] [--verbosity VERBOSITY] [--realism REALISM] [--density DENSITY] [--accessibility ACCESSIBILITY] [--depravity DEPRAVITY] [--linearity LINEARITY] [--narrator [{first,1st,1,third,3rd,3,alt,alternating,subjective,objective,sub,obj,omniscient,omn,limited,lim} [{first,1st,1,third,3rd,3,alt,alternating,subjective,objective,sub,obj,omniscient,omn,limited,lim} ...]]] Story Parameters optional arguments: -h, --help show this help message and exit --charnames [CHARNAMES [CHARNAMES ...]] Character Names --title TITLE Story Title --length LENGTH Story Length (0-999) --charcount CHARCOUNT Character Count (0-999) --genre [{literary,sci-fi,fantasy,history,romance,thriller,mystery,crime,pulp,horror,beat,fan,western,action,war,family,humor,sport,speculative} [{literary,sci-fi,fantasy,history,romance,thriller,mystery,crime,pulp,horror,beat,fan,western,action,war,family,humor,sport,speculative} ...]] Genre --conflict [{nature,man,god,society,self,fate,tech,no god,reality,author} [{nature,man,god,society,self,fate,tech,no god,reality,author} ...]] Conflict --passion PASSION Passion (0-999) --verbosity VERBOSITY Verbosity (0-999) --realism REALISM Realism (0-999) --density DENSITY Density (0-999) --accessibility ACCESSIBILITY Accessibility (0-999) --depravity DEPRAVITY Depravity (0-999) --linearity LINEARITY Linearity (0-999) --narrator [{first,1st,1,third,3rd,3,alt,alternating,subjective,objective,sub,obj,omniscient,omn,limited,lim} [{first,1st,1,third,3rd,3,alt,alternating,subjective,objective,sub,obj,omniscient,omn,limited,lim} ...]] Narrative PoV |
Finally, here are some sample novels generated by the new code (titles chosen by volunteers):