Created
December 12, 2015 21:32
-
-
Save jrladd/e36423d82d01f27ed2a4 to your computer and use it in GitHub Desktop.
This short script uses TextBlob and WordNet to add random glosses to almost every word in a poem. The effect is of a very diligent but overzealous grad student.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
from textblob import TextBlob, Word | |
import random, sys | |
file = sys.argv[1] | |
def gloss_line(line): | |
line = TextBlob(line) | |
glosses = [] | |
for word in line.words: | |
defs = word.definitions | |
if len(defs) > 0: | |
glosses.append((word, random.choice(word.definitions))) | |
return glosses | |
with open(file, 'r') as f: | |
text = f.readlines() | |
for i,line in enumerate(text): | |
if i % 5 == 0: | |
print line.strip(), i | |
else: | |
print line.strip() | |
print "Glosses" | |
for i,line in enumerate(text): | |
for g in gloss_line(line): | |
print i, g[0] + ': ' + g[1] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output from Milton's Nativity Ode:
On the morning of Christs Nativity. Compos'd 1629.
THis is the Month, and this the happy morn
Wherin the Son of Heav'ns eternal King,
Of wedded Maid, and Virgin Mother born, 5
Our great redemption from above did bring;
For so the holy sages once did sing.
That he our deadly forfeit should release,
And with his Father work us a perpetual peace.
Glosses
0 On: in a state required for something to function or be effective
0 morning: the earliest period
0 Christs: a teacher and prophet born in Bethlehem and active in Nazareth; his life and sermons form the basis for Christianity (circa 4 BC - AD 29)
0 Nativity: the theological doctrine that Jesus Christ had no human father; Christians believe that Jesus's birth fulfilled Old Testament prophecies and was attended by miracles; the Nativity is celebrated at Christmas
3 is: have an existence, be extant
3 Month: a time unit of approximately 30 days
3 happy: marked by good fortune
3 morn: the time period between dawn and noon
4 Son: a male human offspring
4 eternal: tiresomely long; seemingly without end
4 King: (chess) the weakest but the most important piece
5 wedded: having been taken in marriage
5 Maid: a female domestic
5 Virgin: the sixth sign of the zodiac; the sun is in this sign from about August 23 to September 22
5 Mother: a term of address for an elderly woman
5 born: put up with something or somebody unpleasant
6 great: of major significance or importance
6 redemption: the act of purchasing back something previously sold
6 above: in or to a place that is higher
6 did: give rise to; cause to happen or occur, not always intentionally
6 bring: be sold for a certain price
7 so: in truth (often tends to intensify)
7 holy: a sacred place of pilgrimage
7 sages: droop, sink, or settle from or as if from pressure or loss of tautness
7 once: at a previous time;
7 did: carry on or function
7 sing: divulge confidential information or secrets
8 he: a very light colorless element that is one of the six inert gasses; the most difficult gas to liquefy; occurs in economically extractable amounts in certain natural gases (as those found in Texas and Kansas)
8 deadly: (used as intensives) extremely
8 forfeit: the act of losing or surrendering something as a penalty for a mistake or fault or failure to perform etc.
8 release: (music) the act or manner of terminating a musical phrase or tone
9 Father:
Father' is a term of address for priests in some churches (especially the Roman Catholic Church or the Orthodox Catholic Church);
Padre' is frequently used in the military9 work: applying the mind to learning and understanding a subject (especially by reading)
9 us: a heavy toxic silvery-white radioactive metallic element; occurs in many isotopes; used for nuclear fuels and nuclear weapons
9 a: the blood group whose red cells carry the A antigen
9 perpetual: continuing forever or indefinitely
9 peace: a treaty to cease hostilities