Created
May 5, 2014 12:39
-
-
Save zed/11535593 to your computer and use it in GitHub Desktop.
Shuffle sentences in a StackOverflow question
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 | |
| """Shuffle sentences in a StackOverflow question. | |
| http://stackoverflow.com/q/19471967 | |
| """ | |
| import random | |
| import sys | |
| import urllib | |
| import requests # $ pip install requests | |
| from nltk.tokenize import sent_tokenize # $ pip install nltk | |
| question_id = int(sys.argv[1]) if len(sys.argv) > 1 else 19471967 | |
| filter_name = urllib.quote('!9WA((IfJa') # with body_markdown | |
| r = requests.get('https://api.stackexchange.com/2.2/questions/{id}' | |
| '?site=stackoverflow&filter={filter}'.format( | |
| id=question_id, filter=filter_name)) | |
| question = r.json()['items'][0] | |
| text = question['body_markdown'].replace('\r\n', '\n').replace('\r', '\n') | |
| sentences = sent_tokenize(text) | |
| random.shuffle(sentences) | |
| print("\n".join(sentences)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment