python generated ktouch typing lessons

I have been teaching my self to type with ktouch. It is a pretty good program, I am already faster than I was in my hunt and peck days, about 120 kpm. I got tired of doing the same lessons over and over, starting with two keys and progressively getting more difficult. Even the most difficult lesson gets easier after you have typed the same nonsense sequences a dozen times.

I found that the lessons are stored in xml files and decided to write a script that would fetch articles from wikipedia and turn them into typing exercises.

here is the file ktouch_tfa_wikipediaEN.py and here is the code;

#! /usr/bin/python
# today's featured article from wikipedia scraped into ktouch lesson

from urllib import FancyURLopener
import re

# a custom urlopener with firefox2 user agent
class MyOpener(FancyURLopener) :
        version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'

# get the wikipedia mainpage
myopener = MyOpener()
address = "http://en.wikipedia.org/wiki/Main_Page"
wikiEN = myopener.open(address).read()

# parse out Todays Featured Article
tfa = wikiEN.split('<div id="mp-tfa" style="padding:2px 5px">\n')[1].split('Recently featured:')[0]
tfa = re.sub('\n','',tfa)

# strip out the html tags
p = re.compile(r'<.*?>');
tdaStripped = p.sub('', tfa);

# break string into an array of strings ~70 characters long
l = len(tdaStripped)
lineArray = []
c = 0
while c <= l :
        oldc = c
        c += 70
        if oldc <= (l - 70) :
                while tdaStripped[c] != ' ' :
                        c += 1
                lineArray.append(tdaStripped[oldc:c].lstrip())
        elif (l - oldc) > 50 :
                lineArray.append(tdaStripped[oldc:l].lstrip())

# create /usr/share/kde4/apps/ktouch/wikipedia.ktouch.xml
# Lesson will be called 'Wikipedia English Daily Featured Article (auto-generated)'
output = '''<?xml version="1.0" encoding="UTF-8"?>
<KTouchLecture>
 <Title>Wikipedia English Daily Featured Article (auto-generated)</Title>
 <Comment>KTouch training file generated</Comment>
 <FontSuggestions>Monospace</FontSuggestions>
 <Levels>
  <Level>
   <NewCharacters>Wikipedia Article</NewCharacters>'''
for line in lineArray :
        output += '\n    <Line>' + line + '</Line>'
output += '\n  </Level>\n </Levels>\n</KTouchLecture>'
outFile = open('/usr/share/kde4/apps/ktouch/wikipedia.ktouch.xml', 'w')
outFile.write(output)
outFile.close()

Now I could go ahead and schedule it to be run regularly and I would always have a fresh lesson.

Advertisements

2 thoughts on “python generated ktouch typing lessons

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s