Skip to content

Instantly share code, notes, and snippets.

@MatthewScholefield
Created January 12, 2020 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatthewScholefield/d4f29dd195415cf2dbd2d0f29cffdec5 to your computer and use it in GitHub Desktop.
Save MatthewScholefield/d4f29dd195415cf2dbd2d0f29cffdec5 to your computer and use it in GitHub Desktop.
A script to find the least used keys on a keyboard from a key distribution file
#!/usr/bin/env python3
from os.path import expanduser
from argparse import ArgumentParser
import json
def main():
parser = ArgumentParser(description='Script to find least used key on keyboard')
parser.add_argument('action', default='sort', nargs='?', choices=['sort', 'extra'])
parser.add_argument('-f', '--key-dist-file', default='~/.kefrey.json', help='Key distribution json file')
args = parser.parse_args()
with open(expanduser(args.key_dist_file)) as f:
freq = json.load(f)
symbol_to_name = {
'-': 'minus',
'=': 'equal',
'[': 'bracketleft',
']': 'bracketright',
';': 'semicolon',
"'": 'apostrophe',
',': 'comma',
'.': 'period',
'!': 'exclam',
'@': 'at',
'#': 'numbersign',
'$': 'dollar',
'%': 'percent',
'^': 'asciicircum',
'&': 'ampersand',
'*': 'asterisk',
'(': 'parenleft',
')': 'parenright',
'_': 'underscore',
'+': 'plus',
'{': 'braceleft',
'}': 'braceright',
':': 'color',
'"': 'quotedbl',
'<': 'less',
'>': 'greater',
'?': 'question',
}
keys = [
('1234567890-=', '!@#$%^&*()_+'),
('qwertyuiop[]', 'QWERTYUIOP{}'),
('asdfghjkl;\'', 'ASDFGHJKL:"'),
('zxcvbnm,./', 'ZXCVBNM<>?')
]
possibles = []
for key_names, alt_names in keys:
for c, a in zip(key_names, alt_names):
if c in symbol_to_name:
c = symbol_to_name[c]
if a in symbol_to_name:
a = symbol_to_name[a]
possibles.append((freq.pop(c, 0) + freq.pop(a, 0), c))
possibles.sort(reverse=True)
if args.action == 'sort':
for amount, name in possibles:
print(name, '-', amount)
elif args.action == 'extra':
for name, amount in sorted(list(freq.items()), key=lambda x: x[-1]):
print(name, '-', amount)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment