-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathord.stava2.js
224 lines (194 loc) · 4.58 KB
/
ord.stava2.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
var mappings = [
["ssio", "6"],
["sch", "6"],
["stj", "6"],
["skj", "6"],
["sky", "6"],
["skö", "6"],
["ske", "6"],
["skä", "6"],
["lju", "6"],
["ski", "6"],
["ask", "07"],
["ky", "6"],
["kj", "6"],
["gj", "6"],
["gi", "6"],
["dj", "6"],
["sj", "6"],
["sh", "6"],
["tj", "6"],
["ch", "6"],
["ck", "7"],
["kö", "6"],
["cc", "70"],
["co", "7"],
["ca", "7"],
["x", "70"],
["j", "6"],
["s", "0"],
["z", "0"],
["c", "0"],
["d", "1"],
["t", "1"],
["n", "2"],
["m", "3"],
["r", "4"],
["l", "5"],
["g", "7"],
["k", "7"],
["q", "7"],
["f", "8"],
["v", "8"],
["w", "8"],
["p", "9"],
["b", "9"]
];
var vowels = {"-":1,"è":1,"á":1,"a":1,"é":1,"e":1,"i":1,"o":1,"u":1,"y":1,"å":1,"ä":1,"ö":1};
window.dict = {};
var words = window.words;
var parseWordIx = function(word, i, lastParse)
{
// try special mapping
for (n = 0; n < mappings.length; n++)
{
mapping = mappings[n][0];
if (word.substr(i, mapping.length) == mapping)
{
return [mappings[n][1], mapping.length];
}
}
return ["", 1];
};
parseWord = function(word)
{
word = word.toLowerCase();
var result = "";
var i, n, k, mapping, lastParse;
for (i = 0; i < word.length; i++)
{
// if (vowels[word[i]])
// {
// lastParse = "";
// continue;
// }
var parsedObj = parseWordIx(word, i, lastParse);
var parsed = parsedObj[0];
var parsedNumChars = parsedObj[1];
i += (parsedNumChars-1);
// For debug
// if (parsed === "" && word[i] !== "h")
// {
// console.log("Unexpected empty parse for: " + word + ", " + i + ", " + lastParse);
// }
if (parsed == lastParse)
{
continue;
}
result += parsed;
lastParse = parsed;
}
return result;
};
var test = function(word, expected)
{
var p = parseWord(word).substr(0,3);
if (p !== expected)
{
console.log("Err: " + word + " => " + p + " (" + expected + ")");
}
};
var runTests = function()
{
test("kjolar", "654");
test("köttsaft", "610");
test("access", "700");
test("casino", "702");
test("session", "062");
test("kyssen", "602");
//test("kisande", "602");
test("gissning", "602");
test("casablanca", "709");
test("cd-skiva", "016");
test("husdjur", "064");
test("symaskin", "036");
test("finger", "827");
test("skjutet", "611");
test("skörd", "641");
test("skymf", "638");
test("skägglös", "675");
test("skeppsbrott", "690");
test("mask", "307");
};
var gogogo = function()
{
var startTime = Date.now();
for (var i = 0; i < window.words.length; i++)
{
var parsed = parseWord(window.words[i]);
if (parsed.length < 2)
{
// too short
continue;
}
if (parsed.length == 2)
{
// 21 becomes 211 which may be acceptable
parsed = parsed + parsed[1];
}
if (parsed.length > 3)
{
// let's not care about the last numbers.
parsed = parsed.substr(0, 3);
}
var list = dict[parsed];
if (!list)
{
list = [];
dict[parsed] = list;
}
list.push(window.words[i]);
}
runTests();
console.log("Time: " + (Date.now() - startTime)/1000 + " sec");
};
var render = function()
{
var $input = $("<input/>")
.attr("placeholder", "Läser in ordlistan...")
.attr("type", "number")
.css({
"font-family": "arial",
"font-size": "1.1em",
"margin-bottom": "0.5em"});
var $result = $("<div/>");
$("body").css("font-family", "arial").append(
$input,
$result);
$input.on("input", function()
{
var val = $input.val();
$result.empty();
var words;
if (val.length == 0)
{
words = [];
} else if (val.length != 3)
{
words = ["Skriv tre siffror"];
$result.css("color", "gray");
}
else
{
words = window.dict[val] || ["Inga ord hittade..."];
$result.css("color", "black");
}
$result.html(words.join("<br>"));
});
window.setTimeout(function()
{
gogogo();
$($input).attr("placeholder", "Skriv tre siffror");
},100 );
};
$(document).ready(render);