Last active
September 12, 2019 01:37
-
-
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.
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
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