Measure the Time You Take to Type a Word Using Python

2

Geekswipe - Measure the Time You Take to Type a Word Using Python

Python is truly a seductive language and it seems to get better every time I try to do something with it. Measuring the time between keystrokes is one such curious project I did to learn Python. Here is the entire concept and a basic logic to measure the time between keystrokes using just 41 lines of Python.

Prerequisites

I assume you have Python installed on your system already. If not, read the ‘installing’ section of this article on making a Python twitter bot and follow the instructions to install Python 2.7.x.

WordTime

Not a great name for our little app, but it’ll work. As it is a learning project to me, I decided to use GUI this time rather than typing stuffs in the console. I am going to use Python’s standard GUI library, TkInter. Let’s start by importing the Tkinter module using import Tkinter. Now the second requirement is something to generate the time. Let’s import the time module using import time.

GUI Part

Now let’s start designing our little window element, which will be used to hold all the text boxes used to type the word. Let’s call our GUI as key_watcher.

key_watcher = Tkinter.Tk()

Let’s add a text box for typing the word.

#type word input
word_type_label = Tkinter.Label(key_watcher, text="Repeat the word in your normal typing speed to calculate the time")
word_type_label.pack(side=Tkinter.LEFT)

word_type_entry_box = Tkinter.Entry(key_watcher)
word_type_entry_box.pack(side=Tkinter.RIGHT)

The above codes are self-explanatory.

The logic

The logic is the complex part here. To measure the time in between the letters, we are going to measure the time taken between the initial and the final keystrokes. Let’s say we want to measure the time taken to type my name, ‘karthikeyan’. So that’s 11 letters and we need to exactly measure the time from the moment I hit the letter ‘k’ to the moment I finally hit the letter ‘n’. So it is necessary that we need to tell our program about the word length and the data of the first letter and the last. This is can be done by storing the same the word to a different variable, let’s call it compare_word, and compare it with the word we type (stored in type_word variable) in real-time. Now before we define the logic further, let’s finish our GUI by adding a text field element for our compare_word variable.

#compare word input
word_compare_label = Tkinter.Label(key_watcher, text="Type the word that you want to test")
word_compare_label.pack(side=Tkinter.LEFT)

word_compare_entry_box = Tkinter.Entry(key_watcher)
word_compare_entry_box.pack(side=Tkinter.LEFT)

Add this code before our type_word text field for consistency. Add this bit of code word_compare_entry_box.focus() at the end to make sure the cursor is focused to our word_compare_entry_box by default. Now the GUI is complete and let’s start contininuing with our logic part of the program.

Receive all the data from GUI

Now that we have a GUI, we need plug it back to our logic, as we need the data from the two text boxes to continue. Create a function called pressed using def pressed(): and start assigning all the parameters needed. First, let’s get the data from the word_compare_entry_box and store the necessary data like the length of the word, individual letters, first letter and last letter of that word and assign it to concerned variables.

	#compare word parameters
	compare_word = word_compare_entry_box.get()
	compare_word_size = len(compare_word)
	compare_word_list = tuple(compare_word)
	compare_word_first_letter = str(compare_word_list[0])
	compare_word_last_letter = str(compare_word_list[-1])

Let’s get the data from word_type_entry_box and store it in some variables like this.

	#type word parameters
	type_word = word_type_entry_box.get()
	type_word_size = len(type_word)

Now we add our first loop to validate the entry and check if it’s actually a word with atleast one letter.

	if type_word_size > 0:
		type_word_list = tuple(type_word)
		type_word_first_letter = str(type_word_list[0])
		type_word_last_letter = str(type_word_list[-1])

Before continuing, let’s see how we are going to measure the time.

Keyevent recording

To measure the time, we have to accurately start the timer once the letter ‘k’ is pressed and stop the timer when the letter ‘n’ is pressed. Which means that in total there are 11 keyevents including the initial event and the final event. Remember the fucntion pressed we created for our logic? We are going to set keyevent as a parameter. Change it to def pressed(keyevent):.

Conditions to start the timer

Let’s resume with our logic. As I have mentioned, the timer should start as soon as we hit the first letter of our word. So the logic here is to check if you have started typing in the text box and the corresponding key pressed (keyevent) matches the compare_word_first_letter value. We also need to check if the word actually has more than one letters. So we also need to check if the compare_word_size is actually greater than one. When all this conditions are satisfied, we then can start the timer. So when we put this in code, it would look like something below.

	if type_word_size == 1 and compare_word_size > 1 and compare_word_first_letter == keyevent.char:
		global start
		start = time.time()

Conditions to stop the timer

The timer must only stop when the size of the two text boxes match. We also need to be sure that the word you type is not a single letter word. We can also add another nested condition to check if the two last letters matches. If the input satisfies these conditions, the timer can be stopped. Now the difference between the two timestamps can easily be calculated. So the conditional snippet would be like the one shown below.

	if type_word_size == compare_word_size and type_word_size != 1: 
		print "Match"
		if compare_word_last_letter == type_word_last_letter:
			stop = time.time()
			total_time = stop-start

Output and other cases

Before we output the results, let’s think about some situations where the program would fail. What would be the output if you type ‘book’ instead of ‘beak’? We are just comparing the word length, and the first and last letters of the word. So either we could compare each letter in the word, or we could just add some conditions to handle this.

			if compare_word == type_word:
				print "You took " + str(float(total_time)) + " seconds to type the word " + str(compare_word) + "."
			else:
				print "You took " + str(float(total_time)) + " seconds to type the word that was similar to " + str(compare_word) + "."
				print "Though the words are of the same size, it doesn't appear that the two words match! Retry again for accurate results..."

Nest this inside the previous condition to handle similar word length cases.

What would happen if you type a single word? It won’t take a significant time anyway. Add the following condition as the first condition after our parameters to show a passive message.

	if compare_word_size == 1 and type_word_size == 1:
		#Even though you measure time, it is insignificant! Also, it returns 0...
		print "It's just one letter... You atleast need a two letter word to calculate the time!"

Binding the keyevent

As we have a function that takes the keyevent as the parameter, we need to bind the the actual keystroke events from the word_type_entry_box text box from our GUI to this keyevent. We do that by using word_type_entry_box.bind('<KeyRelease>', pressed). The reason why I use <KeyRelease> is because, it makes sense that you end the word by releasing a key and the timer ends in a better way.

Now we finish our little app here with key_watcher.mainloop(), which runs the GUI when the python script is executed.

Here is the complete program for your reference.

'''
Title           :WordTime
Description     :A Python program to estimate the time taken to type a word.
Author          :Karthikeyan KC
URL             :https://geekswipe.net/technology/computing/measure-the-time-you-take-to-type-a-word-using-python/
Date            :13-10-2015
Version         :0.2
Usage           :python WordTime.py
Python Version  :2.7.8
'''

import Tkinter
import time

key_watcher = Tkinter.Tk()

#compare word input
word_compare_label = Tkinter.Label(key_watcher, text="Type the word that you want to test")
word_compare_label.pack(side=Tkinter.LEFT)

word_compare_entry_box = Tkinter.Entry(key_watcher)
word_compare_entry_box.pack(side=Tkinter.LEFT)

#type word input
word_type_label = Tkinter.Label(key_watcher, text="Repeat the word in your normal typing speed to calculate the time")
word_type_label.pack(side=Tkinter.LEFT)

word_type_entry_box = Tkinter.Entry(key_watcher)
word_type_entry_box.pack(side=Tkinter.RIGHT)

word_compare_entry_box.focus()

def pressed(keyevent):
	#compare word parameters
	compare_word = word_compare_entry_box.get()
	compare_word_size = len(compare_word)
	compare_word_list = tuple(compare_word)
	compare_word_first_letter = str(compare_word_list[0])
	compare_word_last_letter = str(compare_word_list[-1])

	#type word parameters
	type_word = word_type_entry_box.get()
	type_word_size = len(type_word)

	if type_word_size > 0:
		type_word_list = tuple(type_word)
		type_word_first_letter = str(type_word_list[0])
		type_word_last_letter = str(type_word_list[-1])

	if compare_word_size == 1 and type_word_size == 1:
		#Even though you measure time, it is insignificant! Also, it returns 0...
		print "It's just one letter... You at least need a two letter word to calculate the time!"

	if type_word_size == 1 and compare_word_size > 1 and compare_word_first_letter == keyevent.char:
		global start
		start = time.time()

	if type_word_size == compare_word_size and type_word_size != 1: 
		print "Match"
		if compare_word_last_letter == type_word_last_letter:
			stop = time.time()
			total_time = stop-start
			if compare_word == type_word:
				print "You took " + str(float(total_time)) + " seconds to type the word " + str(compare_word) + "."
			else:
				print "You took " + str(float(total_time)) + " seconds to type the word that was similar to " + str(compare_word) + "."
				print "Though the words are of the same size, it doesn't appear that the two words match! Retry again for accurate results..."

word_type_entry_box.bind('<KeyRelease>', pressed)
key_watcher.mainloop()

I hope you had a fun journey with Python here. I did. This piece of code here is a learning project and it does have a few inconsistencies. You can always clone it and help me understand and explore Python better.

Avatar

Karthikeyan KC

Aeronautical engineer, dev, science fiction author, gamer, and an explorer. I am the creator of Geekswipe. I love writing about physics, aerospace, astronomy, and python. I created Swyde. Currently working on Arclind Mindspace.

Leave a Reply

Your email address will not be published. Required fields are marked *

2 Responses

  1. Avatar joes pizza

    no code does not work

  2. This is a good concept.You have done some neat code.

Related