Simple utility to turn swipes into words -- "plugin" for wvkbd to enable swipe-typing under wayland SXMO.
mapScore: add bee distance
like manhattan distance, but on a hex grid
Zach DeCook 2022-01-24
parent 09a8e79 · commit ce12882
-rw-r--r--README.md3
-rwxr-xr-xmapScore.py23
2 files changed, 20 insertions, 6 deletions
diff --git a/README.md b/README.md
index 5dfadda..0756440 100644
--- a/README.md
+++ b/README.md
@@ -46,6 +46,9 @@ Smarter sorting would be keyboard-layout aware. mapscore.py can do that for you
map.tsv uses tabs and newlines to create the grid-based layout. See `map.qwerty.simplegrid.tsv` for a sample of how to format this file.
+If your keys are in a hexagonal layout, use mapScore like
+`./mapScore.py map.simple.tsv bee`.
+
### alternate formats
Alternatively, for performance, you can use a directory with the following format: each file is named with the first and last letters of the contained words.
diff --git a/mapScore.py b/mapScore.py
index 61a5259..430feb8 100755
--- a/mapScore.py
+++ b/mapScore.py
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
import sys
+import math
def makeMap(filename):
l= [(0,0) for x in range(0,127)]
@@ -10,24 +11,34 @@ def makeMap(filename):
if c == '\t':
x=x+1
elif c == '\n':
- x=0;y=y+1
+ y=y+1
+ x = (y%2)/2
elif c:
l[ord(c)] =(x,y)
else:
break
return l
-def scoreWord(word,mm):
+def scoreWord(word,mm,fun):
pc=word[0] or ' '
s=0
for c in word:
- #manhattan dist
- s+=abs(mm[ord(pc)][0]-mm[ord(c)][0])
- s+=abs(mm[ord(pc)][1]-mm[ord(c)][1])
+ s += fun(mm[ord(pc)],mm[ord(c)])
pc=c
return s
+def manhattan_dist(p1, p2):
+ return abs(int(p1[0])-int(p2[0])) + abs(p1[1]-p2[1])
+
+def bee_dist(p1, p2):
+ yd = abs(p1[1] - p2[1])
+ xd = max(0, math.floor(abs(p1[0]-p2[0])) - yd//2)
+ return xd+yd
+
def main(argv):
+ fun = manhattan_dist
+ if len(argv) >= 3 and argv[2] == 'bee':
+ fun = bee_dist
mm=makeMap(argv[1])
while 1:
line = sys.stdin.readline()
@@ -35,7 +46,7 @@ def main(argv):
break
w=line.strip()
if w:
- print(scoreWord(w,mm),w,sep='\t')
+ print(scoreWord(w,mm,fun),w,sep='\t')
if __name__ == '__main__':
main(sys.argv)