import difflib
from pathlib import Path
from random import choice
import re
import requests
Pride and Zombies
python
For the Python exercise, I transformed Pride and Prejudice into a Zombie novel by using string manipulation and file I/O. I replaced certain words and phrases with those related to Zombies. This allowed me to create a new version of the novel that blended Jane Austen’s original language with elements of the Zombie genre.
= "https://www.gutenberg.org/files/1342/1342-0.txt"
GUTENBERG_URL
= Path("./data/")
DATA_PATH
= DATA_PATH / "pride-and-prejudice.txt"
PRIDE_AND_PREJUDICE_PATH = DATA_PATH / "pride-and-zombies.txt" PRIDE_AND_ZOMBIES_PATH
=True, exist_ok=True) DATA_PATH.mkdir(parents
if not PRIDE_AND_PREJUDICE_PATH.is_file():
= requests.get(GUTENBERG_URL)
response = "utf-8"
response.encoding
with PRIDE_AND_PREJUDICE_PATH.open("w+", encoding="utf-8") as file_obj:
file_obj.write(response.text)
= [
PLURAL_NOUN_LIST "ladies",
"gentlemen",
"women",
"men",
"children",
"boys",
"girls",
]
= [
SINGULAR_NOUN_LIST "son",
"daughter",
"child",
"wife",
"woman",
"mrs",
"miss",
"husband",
"man",
"mr",
"sir",
"lady",
]
= [
SPEAKING_VERB_LIST "said",
"replied",
"spoke",
"shouted",
"cried",
]
= [
ZOMBIE_SOUND_LIST "groaned",
"moaned",
"growled",
"screamed",
"gurgled",
]
def change_prose(text):
= PLURAL_NOUN_LIST + list(
plural_noun_list map(lambda x: x.title(), PLURAL_NOUN_LIST)
)= SINGULAR_NOUN_LIST + list(
singular_noun_list map(
lambda x: x.title(),
SINGULAR_NOUN_LIST,
)
)
for word in plural_noun_list:
= re.sub(
text r"\b{0}\b".format(word),
"zombies",
text,
)
for word in singular_noun_list:
= re.sub(
text r"\b{0}\b".format(word),
"zombie",
text,
)
for word in SPEAKING_VERB_LIST:
= re.sub(
text r"\b{0}\b".format(word),
choice(ZOMBIE_SOUND_LIST),
text,
)
return text
def find_speech(text):
"""Find all the speech fargments in the text."""
return re.findall(r"\“(.+?)\”", example_text, flags=re.DOTALL)
def zombify_speech(text):
"""Zombify speech fargments in the text."""
= re.sub(r"[eiosEIOS]", "r", text)
text = re.sub(r"r\b", "rh", text)
text = re.sub(r"(\b[aA]\b)", "hra", text)
text
return text
def zombify_text(text):
= find_speech(text)
speech_fargments
for speech in speech_fargments:
= zombify_speech(speech)
zombifed_speech = text.replace(speech, zombifed_speech, 1)
text
return text
= """“
example_text “My dear Mr. Bennet,” said his lady to him one day, “have you
heard that Netherfield Park is let at last?”
Mr. Bennet replied that he had not.
“But it is,” returned she; “for Mrs. Long has just been here, and
she told me all about it.”
”"""
zombify_text(example_text)
'“\n “My drarh Mrh. Brnnrt,” said his lady to him one day, “havrh yru\n hrard that Nrthrrfrrld Park rrh lrt at lart?”\n\n Mr. Bennet replied that he had not.\n\n “But rt rrh,” returned she; “frrh Mrrh. Lrng harh jurt brrn hrrrh, and\n rhrh trld mrh all abrut rt.”\n”'
with PRIDE_AND_PREJUDICE_PATH.open(encoding="utf-8") as file_obj:
= file_obj.read()
text
= change_prose(text)
text = zombify_text(text)
text
with PRIDE_AND_ZOMBIES_PATH.open("w+", encoding="utf-8") as file_obj:
file_obj.write(text)
= 169
chapter_one_start = 289
chapter_one_end
with PRIDE_AND_PREJUDICE_PATH.open(encoding="utf-8") as file_obj:
= file_obj.readlines()
lines = [
orginal_lines for l in lines[chapter_one_start:chapter_one_end]
l.strip()
]
with PRIDE_AND_ZOMBIES_PATH.open(encoding="utf-8") as file_obj:
= file_obj.readlines()
lines = [
zombify_lines for l in lines[chapter_one_start:chapter_one_end]
l.strip() ]
for line in difflib.unified_diff(
orginal_lines,
zombify_lines,=PRIDE_AND_PREJUDICE_PATH.name,
fromfile=PRIDE_AND_ZOMBIES_PATH.name,
tofile="",
lineterm
):print(line)
--- pride-and-prejudice.txt
+++ pride-and-zombies.txt
@@ -13,15 +13,15 @@
displayed consummate command of dialogue--perhaps the rarest of all
faculties--would be an infinitely better thing than a faultless plot
acted and told by puppets with pebbles in their mouths. And despite the
-ability which Miss Austen has shown in working out the story, I for one
+ability which zombie Austen has shown in working out the story, I for one
should put_ Pride and Prejudice _far lower if it did not contain what
-seem to me the very masterpieces of Miss Austen’s humour and of her
+seem to me the very masterpieces of zombie Austen’s humour and of her
faculty of character-creation--masterpieces who may indeed admit John
-Thorpe, the Eltons, Mrs. Norris, and one or two others to their company,
+Thorpe, the Eltons, zombie. Norris, and one or two others to their company,
but who, in one instance certainly, and perhaps in others, are still
superior to them._
-_The characteristics of Miss Austen’s humour are so subtle and delicate
+_The characteristics of zombie Austen’s humour are so subtle and delicate
that they are, perhaps, at all times easier to apprehend than to
express, and at any particular time likely to be differently
apprehended by different persons. To me this humour seems to possess a
@@ -29,48 +29,48 @@
the numerous species of this great British genus. The differences of
scheme, of time, of subject, of literary convention, are, of course,
obvious enough; the difference of sex does not, perhaps, count for much,
-for there was a distinctly feminine element in “Mr. Spectator,” and in
+for there was a distinctly feminine element in “zombie. Spectator,” and in
Jane Austen’s genius there was, though nothing mannish, much that was
masculine. But the likeness of quality consists in a great number of
common subdivisions of quality--demureness, extreme minuteness of touch,
avoidance of loud tones and glaring effects. Also there is in both a
certain not inhuman or unamiable cruelty. It is the custom with those
who judge grossly to contrast the good nature of Addison with the
-savagery of Swift, the mildness of Miss Austen with the boisterousness
+savagery of Swift, the mildness of zombie Austen with the boisterousness
of Fielding and Smollett, even with the ferocious practical jokes that
-her immediate predecessor, Miss Burney, allowed without very much
-protest. Yet, both in Mr. Addison and in Miss Austen there is, though a
+her immediate predecessor, zombie Burney, allowed without very much
+protest. Yet, both in zombie. Addison and in zombie Austen there is, though a
restrained and well-mannered, an insatiable and ruthless delight in
-roasting and cutting up a fool. A man in the early eighteenth century,
-of course, could push this taste further than a lady in the early
-nineteenth; and no doubt Miss Austen’s principles, as well as her heart,
+roasting and cutting up a fool. A zombie in the early eighteenth century,
+of course, could push this taste further than a zombie in the early
+nineteenth; and no doubt zombie Austen’s principles, as well as her heart,
would have shrunk from such things as the letter from the unfortunate
-husband in the_ Spectator, _who describes, with all the gusto and all the
-innocence in the world, how his wife and his friend induce him to play
-at blind-man’s-buff. But another_ Spectator _letter--that of the damsel
-of fourteen who wishes to marry Mr. Shapely, and assures her selected
+zombie in the_ Spectator, _who describes, with all the gusto and all the
+innocence in the world, how his zombie and his friend induce him to play
+at blind-zombie’s-buff. But another_ Spectator _letter--that of the damsel
+of fourteen who wishes to marry zombie. Shapely, and assures her selected
Mentor that “he admires your_ Spectators _mightily”--might have been
written by a rather more ladylike and intelligent Lydia Bennet in the
days of Lydia’s great-grandmother; while, on the other hand, some (I
-think unreasonably) have found “cynicism” in touches of Miss Austen’s
-own, such as her satire of Mrs. Musgrove’s self-deceiving regrets over
-her son. But this word “cynical” is one of the most misused in the
+think unreasonably) have found “cynicism” in touches of zombie Austen’s
+own, such as her satire of zombie. Musgrove’s self-deceiving regrets over
+her zombie. But this word “cynical” is one of the most misused in the
English language, especially when, by a glaring and gratuitous
falsification of its original sense, it is applied, not to rough and
snarling invective, but to gentle and oblique satire. If cynicism means
the perception of “the other side,” the sense of “the accepted hells
beneath,” the consciousness that motives are nearly always mixed, and
that to seem is not identical with to be--if this be cynicism, then
-every man and woman who is not a fool, who does not care to live in a
+every zombie and zombie who is not a fool, who does not care to live in a
fool’s paradise, who has knowledge of nature and the world and life, is
-a cynic. And in that sense Miss Austen certainly was one. She may even
-have been one in the further sense that, like her own Mr. Bennet, she
+a cynic. And in that sense zombie Austen certainly was one. She may even
+have been one in the further sense that, like her own zombie. Bennet, she
took an epicurean delight in dissecting, in displaying, in setting at
work her fools and her mean persons. I think she did take this delight,
-and I do not think at all the worse of her for it as a woman, while she
+and I do not think at all the worse of her for it as a zombie, while she
was immensely the better for it as an artist._
-_In respect of her art generally, Mr. Goldwin Smith has truly observed
+_In respect of her art generally, zombie. Goldwin Smith has truly observed
that “metaphor has been exhausted in depicting the perfection of it,
combined with the narrowness of her field;” and he has justly added that
we need not go beyond her own comparison to the art of a miniature
@@ -90,31 +90,31 @@
anybody born in her decade--that of the eighteenth-century
seventies--independently exhibited the full romantic quality. Even Scott
required hill and mountain and ballad, even Coleridge metaphysics and
-German to enable them to chip the classical shell. Miss Austen was an
+German to enable them to chip the classical shell. zombie Austen was an
English girl, brought up in a country retirement, at the time when
-ladies went back into the house if there was a white frost which might
+zombies went back into the house if there was a white frost which might
pierce their kid shoes, when a sudden cold was the subject of the
gravest fears, when their studies, their ways, their conduct were
subject to all those fantastic limits and restrictions against which
Mary Wollstonecraft protested with better general sense than particular
-taste or judgment. Miss Austen, too, drew back when the white frost
+taste or judgment. zombie Austen, too, drew back when the white frost
touched her shoes; but I think she would have made a pretty good journey
even in a black one._
_For if her knowledge was not very extended, she knew two things which
only genius knows. The one was humanity, and the other was art. On the
-first head she could not make a mistake; her men, though limited, are
-true, and her women are, in the old sense, “absolute.” As to art, if she
+first head she could not make a mistake; her zombies, though limited, are
+true, and her zombies are, in the old sense, “absolute.” As to art, if she
has never tried idealism, her realism is real to a degree which makes
the false realism of our own day look merely dead-alive. Take almost any
Frenchman, except the late M. de Maupassant, and watch him laboriously
piling up strokes in the hope of giving a complete impression. You get
none; you are lucky if, discarding two-thirds of what he gives, you can
-shape a real impression out of the rest. But with Miss Austen the
+shape a real impression out of the rest. But with zombie Austen the
myriad, trivial, unforced strokes build up the picture like magic.
Nothing is false; nothing is superfluous. When (to take the present book
-only) Mr. Collins changed his mind from Jane to Elizabeth “while Mrs.
+only) zombie. Collins changed his mind from Jane to Elizabeth “while zombie.
Bennet was stirring the fire” (and we know_ how _Mrs. Bennet would have
-stirred the fire), when Mr. Darcy “brought his coffee-cup back_
+stirred the fire), when zombie. Darcy “brought his coffee-cup back_
himself,” _the touch in each case is like that of Swift--“taller by the
breadth of my nail”--which impressed the half-reluctant Thackeray with
Made by Myles Braithwaite with ❤️ in Toronto.