-
Notifications
You must be signed in to change notification settings - Fork 4
/
klbm.py
83 lines (64 loc) · 1.8 KB
/
klbm.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Keyboard layout benchmarker
# Copyright (c) 2017 MakotoKurauchi
# MIT License
import sys
import json
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from collections import Counter
## load files
argvs = sys.argv
argc = len(argvs)
if (argc != 4):
print ('Usage: $ python %s textfile keymap.json cost.json' % argvs[0])
quit()
f = open(argvs[1],'r')
txt_lst = f.read()
f.close()
keycounter_dict = Counter(txt_lst)
f = open(argvs[2],'r')
keymap_dict = json.load(f)
f.close()
f = open(argvs[3],'r')
cost_dict = json.load(f)
f.close()
position_list = cost_dict['position']
hand_list = cost_dict['hand']
finger_list = cost_dict['finger']
## cost
p_cost = 0
h_cost = 0
count = 0
lasthand = 0
lastfinger = 0
for c in txt_lst:
if c in keymap_dict:
## position cost
p_cost += position_list[keymap_dict[c][0]][keymap_dict[c][1]]
## hand/finger cost
if lasthand != hand_list[keymap_dict[c][0]][keymap_dict[c][1]]:
h_cost += 1
else:
if lastfinger != finger_list[keymap_dict[c][0]][keymap_dict[c][1]]:
h_cost += 4
else:
h_cost += 5
lasthand = hand_list[keymap_dict[c][0]][keymap_dict[c][1]]
lastfinger = finger_list[keymap_dict[c][0]][keymap_dict[c][1]]
count += 1
print("Position cost :", "{0:4d}".format(int(p_cost/count*100)))
print("Hand/Finger cost:", "{0:4d}".format(int(h_cost/count*100)))
print("Total cost :", "{0:4d}".format(int((p_cost/count + h_cost/count)*100)))
## heatmap
lst = []
for k, v in keycounter_dict.items():
if k in keymap_dict:
lst.append([k, v, keymap_dict[k][0], keymap_dict[k][1]])
#else:
#print(k ,"is no match.")
df = pd.DataFrame(lst,columns=["key", "num", "row", "col"])
pivots = df.pivot("row","col","num")
sns.heatmap(pivots, annot=False, cbar=False, square=True, linewidths=1, cmap="PuRd")
plt.show()