Skip to content

Instantly share code, notes, and snippets.

@zachwhalen
Last active September 12, 2019 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachwhalen/95678c6c2647ed27d2d1036a4da00930 to your computer and use it in GitHub Desktop.
Save zachwhalen/95678c6c2647ed27d2d1036a4da00930 to your computer and use it in GitHub Desktop.
This generates a poem from the input of a text file called "pride.txt". It relies on TextBlob.
from textblob import TextBlob
source = open('/path/to/file.txt')
np = TextBlob(source.read()).sentences
def sentenceLength(sent):
return len(sent)
def filter(sent):
for ex in ["Chapter","Section",'\'']:
if ex in sent:
return False
return True
sp = sorted(np, key=sentenceLength)
breaks = 1
for i in sp:
if (i[0].isupper() and filter(i) and breaks < 21):
print(i, ['\n',''][breaks % 2])
breaks += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment