Skip to content

Instantly share code, notes, and snippets.

@zed
Created May 5, 2014 12:39
Show Gist options
  • Select an option

  • Save zed/11535593 to your computer and use it in GitHub Desktop.

Select an option

Save zed/11535593 to your computer and use it in GitHub Desktop.
Shuffle sentences in a StackOverflow question
#!/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