Created
January 6, 2021 16:48
-
-
Save houhoulis/200e5aa016f9b128a6bc7c7ba9b2eb31 to your computer and use it in GitHub Desktop.
Calculate approximate typing accuracy -- a % of words typed correctly -- from % accuracy figures on individual keys
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
# I have been learning touch-typing, largely using https://www.typingstudy.com. | |
# At the end of each drill, it breaks down your performance by % accuracy on each key. This is helpful. | |
# However, drills on some other sites report % word accuracy. | |
# This method calculates a crude approximation of word accuracy from a passed-in list of character | |
# accuracies, by considering all combinations of a specific word length (default 4) of the passed-in | |
# percentages. | |
# Each letter percentage is treated as equal. | |
# We don't generate repeated-letter combination beyond what's passed in, to reduce the exponential | |
# increase in possible combinations. In other words: | |
# | |
# accuracy([1, 1, 0.8, 0.9], 3) | |
# | |
# will consider the combination (0.9, 1, 1), as two "1"s were passed in. | |
# It will not consider (1, 1, 1). | |
# Can be invoked in IRB, or via e.g. | |
# | |
# `ruby typing_accuracy.rb [1, 0.9, 0.9, 0.8] 4` on the command line | |
# | |
# (the last argument is optional: the length of word). | |
# Supporting the command-line usage required some extra work to parse the arguments. | |
def accuracy percentages = [], word_length = 4 | |
# Parse arguments if necessary (typically, if we're being invoked on the command-line). | |
# Also, guard against `percentages` being small or empty. | |
word_length = word_length.to_i unless word_length.is_a? Integer | |
unless percentages.is_a? Array | |
require 'json' | |
percentages = JSON.parse percentages | |
end | |
p "Give an array with at least one percentage!" and return if !percentages.is_a?(Array) || percentages.empty? | |
while percentages.length < word_length | |
percentages *= 2 | |
end | |
# Proceed with acceptable arguments. | |
words = percentages.combination(word_length).to_a | |
word_percentages = words.map { |word| word.reduce(:*) } | |
p word_percentages.sum / word_percentages.length | |
end | |
# Invoke the method if this file is being executed, e.g. on the command line. Don't invoke | |
# if we're being `require`d in Ruby, e.g. in IRB. | |
accuracy *ARGV if ARGV.any? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment