Introducing: PyKBLayoutOptimizer

I’m happy to announce my first software release on GitHub! The new tool is called PyKBLayoutOptimizer, which is a custom keyboard layout tool written in Python. This tool imports a document that you have written to create a suggested keyboard layout based on your own writing preferences. A link to download the software is included near the end of this post.

The Concept

It was a bit of a wild hair that led me to writing this app. Early one morning at 2 A.M. I was up thinking about the Dvorak keyboard and how some say that it’s more efficient to type on than the ubiquitous QWERTY format keyboards. While that might be up for debate, it did get me thinking that one’s writing style, language, or use of certain words would impact the efficiency of a given keyboard design. If the goal is to reduce the amount of movement between typing keys with the most commonly used keys on the “home” row (asdfghjkl on the QWERTY keyboard), then it makes sense that these would be different, even if slightly, for each individual.

The question then became: How do you know which layout would be best for each user?

I’m not going to say that my method of answering that question is the most scientific. I’m sure there are other variables at play like hand positioning, right- or left-handedness (go lefties!). One of the easiest ways I could think of to calculate the best keyboard layout would be to go with quantifiable data – from the user’s own writing.

A long-form document, think an essay, a story, a long article, or even a novel could all be used to derive a user’s preferences for typing. While any length document would do, a long-form document provides more data to be quantified and compared, hopefully leading to more accurate results. If I could build a tool that would import a document, count the number of times different letters were used, I could then apply that to a keyboard layout to make a custom suggestion per the user’s own writing.

The Design

I knew that I wanted to build this app in Python. It’s currently the language I’m trying to learn, not necessarily for professional pursuits but simply for the fact that it’s so useful and easy to write. My problem was this – how to import a large document, preferably in a .txt format to keep it simple, parse the document and pull out counts of all the letters typed. Then, I would need to store that data in some kind of related list and reference it to be displayed as on a keyboard.

One of the first things to look at was a standard keyboard layout. Due to its commonality, I used the QWERTY keyboard layout of 10 letter keys on the top row, 9 on the “home” row in the middle, and 7 letter keys on the bottom row. I avoided number and punctuation keys for this exercise. Once I determined the layout, I created a priority for each key based on its proximity to the home row and also the middle of the keyboard. These priority rankings would correspond to how frequently a letter appears in a user’s writing. See below for the diagram I used to determine the ranking of each key:

Keyboard layout showing each key’s priority, 1-26.

Each key has a ranking, 1-26. The highest or most frequent letter is ranked at number one, while the least-used letter is ranked at 26. As you can see from the layout above, the frequently used keys are near the center of the keyboard, while the seldom-used keys are at the extremities. This of course is editable. In future versions I may create a left/right-handed selection that the user can provide that would change the key priorities above. I also might change a few of the key priority positions, for example moving the 1 and 2 keys to where most people rest their index fingers at the “F” and “J” keys on a QWERTY keyboard.

The code solution actually turned out to be rather simple. I took bites off what I wanted to accomplish in order to write the program. First, I knew that the user would need to enter the file location so that it could be saved as a variable and passed to the open() function. Later, I added a definition for parsing the document data. This involved importing the string and collections modules. Lastly, I was able to save the output to a list using the counts.most_common() function, putting the data in a format I could use to transpose onto a standard keyboard layout to provided the suggested key organization.

Snapshot of the code during development

I had no idea when I started this process that my goal could be achieved in less than 35 lines of code. Actually, less than that due to white space for readability and commented lines. I’m sure a more experienced programmer would have a much better idea of how to to perform this task.

The Results

During development, I used a simple text file with some random letters in it to check the results. Results were promising, but I felt for a good sample size of someone’s work, it would be important to utilize a long document, such as an e-book. The book I chose is one of my favorites, Jules Verne’s “From the Earth to the Moon,” translated from the original French and posted on Project Gutenberg’s web site in various formats. I chose the .txt format and saved it to my desktop. I ran the PyKBLayoutOptimizer script and the result is what I received below:

Output from running the script on Jules Verne’s novel “From the Earth to the Moon”

The script processed 444,209 characters and produced a suggested keyboard layout of the work in roughly 30 seconds on my workstation. Each key has the total individual letter count contained in the novel, and as you can see they correspond with the key priority rankings provided earlier in this post.

As mentioned in the beginning of this post, the purpose of this application would be for a user to take the suggested layout and then use one of the many tools depending on their operating system, to manually create this new layout for their keyboard.

Obviously, this application is more of a thought exercise than a practicable tool to create an enhanced typing experience catered to individual user’s typing habits. There are limitations, some of which have been discussed already. It is quite possible that a user would get different results depending on the work they submitted. There’s also the plain, simple fact that many have become so accustomed to the current keyboards available that switching to a new layout would not be practical. You would need to overcome years of practice, conditioning, and muscle memory to make the transition.

I have a hypothesis that if this application was used to gather data from enough users, the results could be compared in a statistical analysis and would find that the letter frequencies are similar, and perhaps enough so to develop a new keyboard layout that is efficiently laid out for the largest number of people. One can dream, like I should’ve been doing that morning at 2 A.M.

Anyways, enjoy this simple app I created, check out your results, and feel free to edit the code and improve it. Happy coding!

You can download PyKBLayoutOptimizer here.

All you need to run PyKBLayoutOptimizer is Python on your computer. Simply run the .py file in the .zip or .tar.gz file to get started.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: