repl.it
@mat1/

Keyboards

Python

No description

fork
loading
Files
  • main.py
  • keyboards
  • wordlists
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import requests
import math

keyboard = 'qwerty'
word_list = 'words'

# qwerty = 2.44
# azerty = 2.55
# dvorak = 2.90
# colemak = 2.43

with open(f'wordlists/{word_list}.txt', 'r') as f:
	words = f.read().splitlines()

with open(f'keyboards/{keyboard}.txt') as f:
	keyboard_data = f.read()

def get_letter_positions(letter):
	if letter == ' ': letter = '_'
	positions = []
	for y, line in enumerate(keyboard_data.splitlines()):
		for x, char in enumerate(line):
			x = x / 2
			if char == letter:
				positions.append((x, y))
	return positions

def find_distance_berween_points(point1, point2):
	distance_x = abs(point1[0] - point2[0])
	distance_y = abs(point1[1] - point2[1])
	distance = math.sqrt(distance_x ** 2 + distance_y ** 2)
	return distance


def find_letter_distance(letter1, letter2):
	letter1_positions = get_letter_positions(letter1)
	letter2_positions = get_letter_positions(letter2)
	min_distance = float('inf')
	for letter1_pos in letter1_positions:
		for letter2_pos in letter2_positions:
			distance = find_distance_berween_points(letter1_pos, letter2_pos)
			if distance < min_distance:
				min_distance = distance
	return min_distance

def calculate_difficulty(word):
	word += ' ' # space after each word
	total_travel_distance = 0
	prev_letter = word[0]
	for letter in word[1:]:
		distance = find_letter_distance(prev_letter, letter)
		if distance > 999: continue
		total_travel_distance += distance
		prev_letter = letter
	return total_travel_distance / len(word)

print('\033[2J\033[H', end='')
print('Calculating average key distance for', keyboard)

total_difficulty = 0
for i, word in enumerate(words):
	difficulty = calculate_difficulty(word)
	total_difficulty += difficulty
	if i % 1000 == 0:
		print('\033[2J\033[H', end='')
		print((i / len(words) * 100), '%', total_difficulty / (i+1))

print('\033[2J\033[H', end='')
print(keyboard.upper() + ':', total_difficulty / len(words))
Fetching token
?