Simple utility to turn swipes into words -- "plugin" for wvkbd to enable swipe-typing under wayland SXMO.
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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define BUFSIZE 1024
char buffer[BUFSIZE];
// TODO: Support UTF-8.
int map[256][3];

void makeMap(FILE *f) {
	int x = 0; int y = 0;
	int c = 0;
	while ((c = fgetc(f)) != EOF) {
		switch (c) {
		case '\t': x++; break;
		case '\n': y++; x=0; break;
		default:
		explicit_label:
			map[c][0] = x;
			map[c][1] = y;
			map[c][2] = 1;
			c = toupper(c);
			if (map[toupper(c)][2] == 0) goto explicit_label;
			break;
		}
	}
}

int scoreWord(char *word, int (*fun)(int*,int*)) {
	char *c = word;
	int score = 0;
	char *p = c;
	for (c++;*c;c++) {
		if (*c == '\n') break;
		// pass over ignore unset chars.
		if(map[(unsigned)*c][3] == 0) continue;
		score += (*fun)(map[(unsigned)*p],map[(unsigned)*c]);
		p=c;
	}
	return score;
}

int manhattanDist(int *p1,int *p2) {
	return abs(p1[0]-p2[0]) + abs(p1[1]-p2[1]);
}
int beeDist(int *p1, int *p2) {
	const int yd = p1[1] - p2[1];
	const int xd = p1[0] - p2[0];
	const int ayd = abs(yd);
	int axd = abs(xd);
	if (ayd % 2) {
		if ((p1[1]/2 + p2[1]/2) % 2) {
			if (xd * yd > 0) axd--;
		} else {
			if (xd * yd < 0) axd--;
		}
	}
	axd -= ayd / 2;
	if (axd < 0) axd = 0;
	return axd + ayd;
}

int main(int argc, char **argv) {
	if (argc < 2) {
		fprintf(stderr, "Usage: mapScore map.tsv <words.txt >scoredWords.tsv");
		return 1;
	}

int (*fun)(int*,int*) = &manhattanDist;
	if (argc >= 3) {
		fun = &beeDist;
	}

	FILE *f = fopen(argv[1], "r");
	makeMap(f);
	fclose(f);
	while (fgets(buffer, BUFSIZE, stdin)) {
		printf("%d\t%s", scoreWord(buffer, fun), buffer);
	}
}