from dodona import core, keyboards, wordlists
from copy import deepcopy
%pylab
%matplotlib inline
wordlist = core.WordList()
wordlist.LoadFromFile("wordlist.dat")
def make_fitaly_keyboard(letters = "zvchwkfitalynegdorsbqjumpx"):
keyboard = core.Keyboard()
#Make a square
polygon = core.Polygon()
polygon.AddVertex(-0.4, -0.4)
polygon.AddVertex(-0.4, 0.4)
polygon.AddVertex(0.4, 0.4)
polygon.AddVertex(0.4, -0.4)
#cycle through and add each key
letter_index = 0
for i in range(5):
low, high = 0, 6
if i == 2:
low, high = 2, 4
for j in range(low, high):
new_polygon = deepcopy(polygon)
new_polygon.Translate(j - 2.5, -i + 2.5)
keyboard.AddKey(letters[letter_index], new_polygon)
letter_index += 1
return keyboard
#Construct the keyboard and test it out on a word
keyboard = make_fitaly_keyboard()
model = core.SimpleGaussianModel()
model.SetScale(0.2)
vector = model.RandomVector("fitaly", keyboard)
keyboards.DrawKeyboard(keyboard, inputvector=vector, wordlist=wordlist,
nopalette=True, colormap=mpl.cm.Blues, markersize=20)
print("Stylus Movement:", vector.SpatialLength())
def average_distance(keyboard, model):
weighted_sum = 0
for i in range(wordlist.Words()):
word = wordlist.Word(i)
vector = model.RandomVector(word, keyboard)
weighted_sum += vector.SpatialLength()*wordlist.Occurances(word)
return weighted_sum/wordlist.TotalOccurances()
#Evaluate the average distance for FITALY
print("FITALY average distance:", average_distance(keyboard, model))
#And for a random keyboard
random_keyboard = make_fitaly_keyboard()
random_keyboard.Randomize()
print("Random average distance:", average_distance(random_keyboard, model))
#And let's see what the random one looked like
keyboards.DrawKeyboard(random_keyboard, wordlist=wordlist,
nopalette=True, colormap=mpl.cm.Blues, markersize=20)