Created
August 29, 2012 14:11
-
-
Save ianonavy/3513154 to your computer and use it in GitHub Desktop.
I wrote this script in high school to facilitate the reading of my English textbooks. It splits a text up into individual sentences and flashes them one at a time based on how fast you can read (in words per minute).
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
Title: | |
Tartuffe | |
Filename: | |
tartuffe.txt | |
Speed (in words per minute): | |
600 | |
Saved books: | |
Title: | |
Wuthering Heights | |
Filename: | |
wuthering_heights.txt | |
Title: | |
A Room of One's Own | |
Filename: | |
a_room_of_ones_own.txt | |
Title: | |
Oedipus Rex | |
Filename: | |
oedipus_rex.txt |
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 | |
import os | |
import time | |
import textwrap | |
config = open("config.txt", 'r').read().split('\n') # Read config file. | |
# Get configuration settings. | |
title = config[1].strip() | |
filename = config[3].strip() | |
wpm = int(config[5].strip()) | |
book = open(filename, 'r').read() # Get the book. | |
os.system("clear") | |
print "You are reading %s at %d words per minute." % (title, wpm) | |
cps = wpm * 5.0 / 60.0 # Convert words per minute to characters per second. | |
# Approximate the time to finish the book at the given speed. | |
hours, remainder = divmod((len(book) / cps), 3600) | |
minutes, seconds = divmod(remainder, 60) | |
print "It takes about %02d:%02d:%02d to read %s at your speed." % \ | |
(hours, minutes, seconds, title) | |
# Ask user when to start. | |
print "Start with line:", | |
start = int(raw_input()) | |
# Split the book by sentence. | |
#sentences = re.split(r"""([^.?!"]+[.?!"])""", book) | |
sentences = book.split("\n\r") | |
sentences = [f for f in sentences if f] # Delete any blank lines. | |
total = len(sentences) # Save the number of sentences. | |
i = 1 | |
for sentence in sentences: | |
if i >= start: # Skip to the starting number | |
os.system("clear") | |
# Print the line number and how much time is left. | |
print "%d of %d\t%.1f%%" % (i, total, 100 * (i * 1.0 / total)) | |
timeleft = (len(book) - book.find(sentence)) / cps | |
h, r = divmod(timeleft, 3600) | |
m, s = divmod(r, 60) | |
print "Approximately %02d:%02d:%02d left.\n" % (h, m, s) | |
s = sentence.strip() # Strip whitespace. | |
print "\n".join(textwrap.wrap(s, 80)) # Automatic break at 80 chars. | |
time.sleep(max([1, len(sentence) / cps])) # Wait till user reads. | |
i += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment