Created
December 17, 2015 08:28
-
-
Save hugovk/dea17bf820cc59ffa9dd to your computer and use it in GitHub Desktop.
Split a text into sentences shorter than 140 characters.
This file contains hidden or 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 | |
# encoding: utf-8 | |
""" | |
Split a text into sentences shorter than 140 characters. | |
You'll need to first install TextBlob by running: | |
pip install -U textblob | |
Then stick your text into the text bit below and run this script: | |
python tweetables.py | |
""" | |
from textblob import TextBlob | |
text = ''' | |
But if, in the face of all this, you still declare that whaling has no | |
aesthetically noble associations connected with it, then am I ready to | |
shiver fifty lances with you there, and unhorse you with a split helmet | |
every time. | |
The whale has no famous author, and whaling no famous chronicler, you | |
will say. | |
THE WHALE NO FAMOUS AUTHOR, AND WHALING NO FAMOUS CHRONICLER? Who wrote | |
the first account of our Leviathan? Who but mighty Job! And who composed | |
the first narrative of a whaling-voyage? Who, but no less a prince than | |
Alfred the Great, who, with his own royal pen, took down the words from | |
Other, the Norwegian whale-hunter of those times! And who pronounced our | |
glowing eulogy in Parliament? Who, but Edmund Burke! | |
True enough, but then whalemen themselves are poor devils; they have no | |
good blood in their veins. | |
NO GOOD BLOOD IN THEIR VEINS? They have something better than royal | |
blood there. The grandmother of Benjamin Franklin was Mary Morrel; | |
afterwards, by marriage, Mary Folger, one of the old settlers | |
of Nantucket, and the ancestress to a long line of Folgers and | |
harpooneers--all kith and kin to noble Benjamin--this day darting the | |
barbed iron from one side of the world to the other. | |
Good again; but then all confess that somehow whaling is not | |
respectable. | |
WHALING NOT RESPECTABLE? Whaling is imperial! By old English statutory | |
law, the whale is declared "a royal fish."* | |
Oh, that's only nominal! The whale himself has never figured in any | |
grand imposing way. | |
THE WHALE NEVER FIGURED IN ANY GRAND IMPOSING WAY? In one of the mighty | |
triumphs given to a Roman general upon his entering the world's capital, | |
the bones of a whale, brought all the way from the Syrian coast, were | |
the most conspicuous object in the cymballed procession.* | |
*See subsequent chapters for something more on this head. | |
Grant it, since you cite it; but, say what you will, there is no real | |
dignity in whaling.''' | |
blob = TextBlob(text) | |
for sentence in blob.sentences: | |
if len(sentence) <= 140: | |
print("-----") | |
print(sentence) | |
# End of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: