Created
June 23, 2014 00:41
-
-
Save fractaledmind/961efcc20159b08021f2 to your computer and use it in GitHub Desktop.
spritz.py
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 | |
| # -*- coding: utf-8 -*- | |
| from __future__ import print_function, unicode_literals | |
| import sys | |
| import fileinput | |
| import time | |
| import math | |
| def to_unicode(text, encoding='utf-8'): | |
| """Convert ``text`` to unicode using ``encoding`` | |
| :param text: string object to convert to ``unicode`` | |
| :type text: ``str`` or ``unicode`` | |
| :returns: string object as unicode object | |
| :rytpe: ``unicode`` | |
| """ | |
| if isinstance(text, basestring): | |
| if not isinstance(text, unicode): | |
| text = unicode(text, encoding) | |
| return text | |
| def get_orp(integer): | |
| """Get Optimal Reading Position (ORP) given ``integer``. | |
| ORP is slightly left of center. | |
| :param integer: length of string object to calculate ORP | |
| :type integer: ``integer`` | |
| :returns: value of ORP | |
| :rytpe: ``integer`` | |
| """ | |
| percentage = 0.45 | |
| return int(math.ceil(integer * percentage)) | |
| def calculate_spaces(word, max_length): | |
| """Determine buffer spaces for ``word`` given the ``max_length`` | |
| :param word: string object for calculation | |
| :type word: ``unicode`` | |
| :param max_length: value of longest word in full text | |
| :type max_length: ``integer`` | |
| :returns: word's ORP, number of prefix spaces, and number of post spaces | |
| :rytpe: ``tuple`` of ``integers`` | |
| """ | |
| max_orp = get_orp(max_length) | |
| orp = get_orp(len(word)) | |
| prefix_space = (max_orp - orp) | |
| postfix_space = (max_length - len(word) - prefix_space) | |
| return (orp, prefix_space, postfix_space) | |
| def find_max(reading): | |
| """ | |
| Find longest word in ``reading`` | |
| :param reading: the full string object to be spritzed | |
| :type reading: ``unicode`` | |
| :returns: number of characters in the longest word | |
| :rytpe: ``integer`` | |
| """ | |
| reading = sorted(reading, key=len, reverse=True) | |
| return len(reading[0]) | |
| def parse_article(article): | |
| """ | |
| Clean up input ``article`` and insert appropriate pauses. | |
| :param article: the full string object to be spritzed | |
| :type article: ``unicode`` | |
| :returns: words in ``article`` | |
| :rytpe: ``list`` | |
| """ | |
| remove = (',', '.', '!', '?', '-', ';') | |
| for char in remove: | |
| article = article.replace(char, " <pause> ") | |
| article = article.strip() | |
| article = article.replace("\n", " <pause> <pause> ") | |
| return article.split() | |
| def insert_color(word, orp): | |
| """Prepare color-coded `word`""" | |
| #color_red = "\033[91m" | |
| #color_restore = "\033[0m" | |
| left = '>' | |
| right = '<' | |
| return word[0:orp] + left + word[orp] + right + word[orp+1:] | |
| def print_word(word, orp_config): | |
| """Pretty print `word` with spritz color formatting""" | |
| (orp, prefix, postfix) = orp_config | |
| print_string = " " * prefix + insert_color(word, orp-1) + " " * postfix | |
| print("\r{}".format(print_string)) | |
| sys.stdout.flush() | |
| def spritz(wpm, reading): | |
| """ | |
| function to perform "spritz" | |
| """ | |
| spw = 60.0 / wpm | |
| sleep_interval = spw | |
| max_length = find_max(reading) | |
| for word in reading: | |
| if word == "<pause>": | |
| time.sleep(sleep_interval * 10) | |
| continue | |
| word_sleep_interval = 0.01 * len(word) | |
| time.sleep(sleep_interval + word_sleep_interval) | |
| orp_config = calculate_spaces(word, max_length) | |
| #print(word) | |
| print_word(word, orp_config) | |
| def main(wpm, article): | |
| """ | |
| Main function | |
| """ | |
| reading = parse_article(article) | |
| spritz(wpm, reading) | |
| if __name__ == '__main__': | |
| wpm = 750 | |
| article = "This is a simple test." | |
| main(wpm, article) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment