summaryrefslogtreecommitdiffstats
path: root/typo.c
blob: e3b8e43444569b93973411458de6155bd4c5dc82 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*********************************************************************
 * bfs                                                               *
 * Copyright (C) 2016 Tavian Barnes <tavianator@tavianator.com>      *
 *                                                                   *
 * This program is free software. It comes without any warranty, to  *
 * the extent permitted by applicable law. You can redistribute it   *
 * and/or modify it under the terms of the Do What The Fuck You Want *
 * To Public License, Version 2, as published by Sam Hocevar. See    *
 * the COPYING file or http://www.wtfpl.net/ for more details.       *
 *********************************************************************/

#include "typo.h"
#include <limits.h>
#include <stdlib.h>
#include <string.h>

struct coords {
	int x, y;
};

// Assume QWERTY layout for now
static const struct coords key_coords[UCHAR_MAX] = {
	['`']  = { 0,  0},
	['~']  = { 0,  0},
	['1']  = { 3,  0},
	['!']  = { 3,  0},
	['2']  = { 6,  0},
	['@']  = { 6,  0},
	['3']  = { 9,  0},
	['#']  = { 9,  0},
	['4']  = {12,  0},
	['$']  = {12,  0},
	['5']  = {15,  0},
	['%']  = {15,  0},
	['6']  = {18,  0},
	['^']  = {18,  0},
	['7']  = {21,  0},
	['&']  = {21,  0},
	['8']  = {24,  0},
	['*']  = {24,  0},
	['9']  = {27,  0},
	['(']  = {27,  0},
	['0']  = {30,  0},
	[')']  = {30,  0},
	['-']  = {33,  0},
	['_']  = {33,  0},
	['=']  = {36,  0},
	['+']  = {36,  0},

	['\t'] = { 1,  3},
	['q']  = { 4,  3},
	['Q']  = { 4,  3},
	['w']  = { 7,  3},
	['W']  = { 7,  3},
	['e']  = {10,  3},
	['E']  = {10,  3},
	['r']  = {13,  3},
	['R']  = {13,  3},
	['t']  = {16,  3},
	['T']  = {16,  3},
	['y']  = {19,  3},
	['Y']  = {19,  3},
	['u']  = {22,  3},
	['U']  = {22,  3},
	['i']  = {25,  3},
	['I']  = {25,  3},
	['o']  = {28,  3},
	['O']  = {28,  3},
	['p']  = {31,  3},
	['P']  = {31,  3},
	['[']  = {34,  3},
	['{']  = {34,  3},
	[']']  = {37,  3},
	['}']  = {37,  3},
	['\\'] = {40,  3},
	['|']  = {40,  3},

	['a']  = { 5,  6},
	['A']  = { 5,  6},
	['s']  = { 8,  6},
	['S']  = { 8,  6},
	['d']  = {11,  6},
	['D']  = {11,  6},
	['f']  = {14,  6},
	['F']  = {14,  6},
	['g']  = {17,  6},
	['G']  = {17,  6},
	['h']  = {20,  6},
	['H']  = {20,  6},
	['j']  = {23,  6},
	['J']  = {23,  6},
	['k']  = {26,  6},
	['K']  = {26,  6},
	['l']  = {29,  6},
	['L']  = {29,  6},
	[';']  = {32,  6},
	[':']  = {32,  6},
	['\''] = {35,  6},
	['"']  = {35,  6},
	['\n'] = {38,  6},

	['z']  = { 6,  9},
	['Z']  = { 6,  9},
	['x']  = { 9,  9},
	['X']  = { 9,  9},
	['c']  = {12,  9},
	['C']  = {12,  9},
	['v']  = {15,  9},
	['V']  = {15,  9},
	['b']  = {18,  9},
	['B']  = {18,  9},
	['n']  = {21,  9},
	['N']  = {21,  9},
	['m']  = {24,  9},
	['M']  = {24,  9},
	[',']  = {27,  9},
	['<']  = {27,  9},
	['.']  = {30,  9},
	['>']  = {30,  9},
	['/']  = {33,  9},
	['?']  = {33,  9},

	[' ']  = {18, 12},
};

static int char_distance(char a, char b) {
	const struct coords *ac = &key_coords[(unsigned char)a], *bc = &key_coords[(unsigned char)b];
	int dx = abs(ac->x - bc->x);
	int dy = abs(ac->y - bc->y);
	return dx + dy;
}

int typo_distance(const char *actual, const char *expected) {
	// This is the Wagner-Fischer algorithm for Levenshtein distance, using
	// Manhattan distance on the keyboard for individual characters.

	const int insert_cost = 12;

	size_t rows = strlen(actual) + 1;
	size_t cols = strlen(expected) + 1;

	int arr0[cols], arr1[cols];
	int *row0 = arr0, *row1 = arr1;

	for (size_t j = 0; j < cols; ++j) {
		row0[j] = insert_cost * j;
	}

	for (size_t i = 1; i < rows; ++i) {
		row1[0] = row0[0] + insert_cost;

		char a = actual[i - 1];
		for (size_t j = 1; j < cols; ++j) {
			char b = expected[j - 1];
			int cost = row0[j - 1] + char_distance(a, b);
			int del_cost = row0[j] + insert_cost;
			if (del_cost < cost) {
				cost = del_cost;
			}
			int ins_cost = row1[j - 1] + insert_cost;
			if (ins_cost < cost) {
				cost = ins_cost;
			}
			row1[j] = cost;
		}

		int *tmp = row0;
		row0 = row1;
		row1 = tmp;
	}

	return row0[cols - 1];
}