Created
November 23, 2014 13:05
-
-
Save saurabhwahile/d5fdc45ff642cb416b37 to your computer and use it in GitHub Desktop.
Jumble sentences in paragraph(s)
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
import os | |
import random | |
import collections | |
def jumbleSentences(paragraph): | |
sentences = paragraph.split('. ') | |
jumbledSentences = collections.OrderedDict() | |
jumbleCounter = 0 | |
while jumbleCounter < len(sentences): | |
position = random.randrange(0, len(sentences)) | |
if jumbledSentences.has_key(position): | |
continue | |
else: | |
jumbledSentences[position] = sentences[position] | |
jumbleCounter = jumbleCounter+1 | |
return jumbledSentences | |
def formatJumbledSentences(sentencesDict): | |
output = '' | |
index = 1 | |
for key, value in sentencesDict.iteritems(): | |
output = output+str(index)+'. '+value+'\n' | |
index = index+1 | |
return output | |
def splitParagraphs(input): | |
return input.split('\n') | |
outputFile = open('output.txt', 'wb+') | |
input = '' | |
if os.path.isfile('input.txt'): | |
inputFile = open('input.txt') | |
input = inputFile.read() | |
else: | |
input = raw_input() | |
paragraphs = splitParagraphs(input) | |
output = '' | |
for paragraph in paragraphs: | |
try: | |
if paragraph[0].isdigit() or paragraph[0] is ' ': | |
continue | |
except: | |
continue | |
output = output+formatJumbledSentences(jumbleSentences(paragraph))+'\n\n' | |
outputFile.write(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment