forked from yaegaki/typing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtyping.js
588 lines (561 loc) · 17 KB
/
typing.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
;(function (window) {
var _Typing = window.Typing
? window.Typing
: (window.Typing = function () {
this.original = '' //問題文
this.question = [] //問題文を変換したもの
this.remainedQuestion = '' //残りの問題文
this.length = 0 //問題文の文字数(this.question.length)
this.answered = 0 //回答した文字数
this.answeredString = '' //回答した文字
this.absoluteAnswered = 0 //回答した絶対的文字数(っきゃ で3文字)
this.currentIndex = 0 //現在回答している文字の何アルファベット目か
this.currentArrayIndex = 0 //現在回答している文字のなん配列目か
this.select = [] //複数ある回答のうち、どれを選択しているか
this.fault = 0
this.stringNum = [] //文字を構成する数のリスト
})
var _convertList = {
あ: ['a'],
い: ['i'],
う: ['u', 'wu'],
え: ['e'],
お: ['o'],
か: ['ka', 'ca'],
き: ['ki'],
く: ['ku'],
け: ['ke'],
こ: ['ko', 'co'],
さ: ['sa'],
し: ['si', 'shi', 'ci'],
す: ['su'],
せ: ['se', 'ce'],
そ: ['so'],
た: ['ta'],
ち: ['ti', 'chi'],
つ: ['tu', 'tsu'],
て: ['te'],
と: ['to'],
な: ['na'],
に: ['ni'],
ぬ: ['nu'],
ね: ['ne'],
の: ['no'],
は: ['ha'],
ひ: ['hi'],
ふ: ['hu', 'fu'],
へ: ['he'],
ほ: ['ho'],
ま: ['ma'],
み: ['mi'],
む: ['mu'],
め: ['me'],
も: ['mo'],
や: ['ya'],
ゆ: ['yu'],
よ: ['yo'],
ら: ['ra'],
り: ['ri'],
る: ['ru'],
れ: ['re'],
ろ: ['ro'],
わ: ['wa'],
を: ['wo'],
ん: ['nn', 'xn'],
が: ['ga'],
ぎ: ['gi'],
ぐ: ['gu'],
げ: ['ge'],
ご: ['go'],
ざ: ['za'],
じ: ['zi', 'ji'],
ず: ['zu'],
ぜ: ['ze'],
ぞ: ['zo'],
だ: ['da'],
ぢ: ['di'],
づ: ['du'],
で: ['de'],
ど: ['do'],
ば: ['ba'],
び: ['bi'],
ぶ: ['bu'],
べ: ['be'],
ぼ: ['bo'],
ぱ: ['pa'],
ぴ: ['pi'],
ぷ: ['pu'],
ぺ: ['pe'],
ぽ: ['po'],
ぁ: ['la', 'xa'],
ぃ: ['li', 'xi'],
ぅ: ['lu', 'xu'],
ぇ: ['le', 'xe'],
ぉ: ['lo', 'xo'],
ゃ: ['lya', 'xya'],
ゅ: ['lyu', 'xyu'],
ょ: ['lyo', 'xyo'],
ゎ: ['lwa', 'xwu'],
っ: ['ltu', 'xtu'],
ゐ: ['i', 'wyi'],
ゑ: ['e', 'wye'],
ヴ: ['vu'],
}
var _specificConvertList = {
ゃ: ['ya', 'ha', 'a'],
ゅ: ['yu', 'hu', 'u'],
ょ: ['yo', 'ho', 'o'],
// "ぁ": ["", "ha", "a"],
ぁ: ['', 'ha', 'a', 'wa'],
ぃ: ['yi', 'hi', 'i'],
ぅ: ['', 'hu', 'u'],
ぇ: ['ye', 'he', 'e'],
ぉ: ['', '', 'ho'],
}
function convert(char) {
var result = _convertList[char]
if (result) result = result.slice(0)
return result
}
function specificConvert(char) {
var result = _specificConvertList[char]
if (result) result = result.slice(0)
return result
}
/*
きゃきゅきょなどの変換
*/
function specificStringConvert(string) {
var result = []
switch (string[0]) {
case 'き':
if (string[1] == 'ぁ' || string[1] == 'ぅ' || string[1] == 'ぉ') break
result.push('k' + specificConvert(string[1])[0])
break
case 'く':
if (string[1] == 'ぁ') result.push('k' + specificConvert(string[1])[3])
break
case 'し':
if (string[1] == 'ぁ' || string[1] == 'ぅ' || string[1] == 'ぉ') break
result.push('s' + specificConvert(string[1])[0])
if (string[1] != 'ぃ') result.push('s' + specificConvert(string[1])[1])
break
case 'ち':
if (string[1] == 'ぁ' || string[1] == 'ぅ' || string[1] == 'ぉ') break
result.push('c' + specificConvert(string[1])[0])
result.push('t' + specificConvert(string[1])[0])
if (string[1] != 'ぃ') result.push('c' + specificConvert(string[1])[1])
break
case 'て':
if (string[1] == 'ぁ' || string[1] == 'ぅ' || string[1] == 'ぉ') break
result.push('t' + specificConvert(string[1])[1])
break
case 'に':
if (string[1] == 'ぁ' || string[1] == 'ぅ' || string[1] == 'ぉ') break
result.push('n' + specificConvert(string[1])[0])
break
case 'ひ':
if (string[1] == 'ぁ' || string[1] == 'ぅ' || string[1] == 'ぉ') break
result.push('h' + specificConvert(string[1])[0])
break
case 'み':
if (string[1] == 'ぁ' || string[1] == 'ぅ' || string[1] == 'ぉ') break
result.push('m' + specificConvert(string[1])[0])
break
case 'り':
if (string[1] == 'ぁ' || string[1] == 'ぅ' || string[1] == 'ぉ') break
result.push('r' + specificConvert(string[1])[0])
break
case 'う':
if (string[1] == 'ぃ' || string[1] == 'ぇ' || string[1] == 'ぉ')
result.push('w' + specificConvert(string[1])[2])
break
case 'ふ':
if (string[1] == 'ぁ' || string[1] == 'ぃ' || string[1] == 'ぇ')
result.push('f' + specificConvert(string[1])[2])
else if (string[1] == 'ぉ') result.push('fo')
break
case 'ぎ':
if (string[1] == 'ぁ' || string[1] == 'ぅ' || string[1] == 'ぉ') break
result.push('g' + specificConvert(string[1])[0])
break
case 'じ':
if (string[1] == 'ぁ' || string[1] == 'ぅ' || string[1] == 'ぉ') break
if (string[1] == 'ゃ' || string[1] == 'ゅ' || string[1] == 'ょ')
result.push('j' + specificConvert(string[1])[2])
if (string[1] != 'ぃ') result.push('j' + specificConvert(string[1])[2])
result.push('j' + specificConvert(string[1])[0])
result.push('z' + specificConvert(string[1])[0])
break
case 'ぢ':
if (string[1] == 'ぁ' || string[1] == 'ぅ' || string[1] == 'ぉ') break
result.push('d' + specificConvert(string[1])[0])
break
case 'び':
if (string[1] == 'ぁ' || string[1] == 'ぅ' || string[1] == 'ぉ') break
result.push('b' + specificConvert(string[1])[0])
break
case 'ぴ':
if (string[1] == 'ぁ' || string[1] == 'ぅ' || string[1] == 'ぉ') break
result.push('p' + specificConvert(string[1])[0])
break
case 'で':
if (string[1] == 'ぁ' || string[1] == 'ぅ' || string[1] == 'ぉ') break
result.push('d' + specificConvert(string[1])[1])
break
case 'ヴ':
switch (string[1]) {
case 'ぁ':
result.push('va')
break
case 'ぃ':
result.push('vi')
result.push('vyi')
break
case 'ぅ':
result.push('vu')
break
case 'ぇ':
result.push('ve')
result.push('vye')
break
case 'ぉ':
result.push('vo')
break
case 'ゃ':
result.push('vya')
break
case 'ゅ':
result.push('vyu')
break
case 'ょ':
result.push('vyo')
break
}
}
var str1 = convert(string[0])
str1 = str1 ? str1 : []
var str2 = convert(string[1])
for (var i = 0; i < str1.length; i++) {
for (var j = 0; j < str2.length; j++) {
result.push([str1[i], str2[j]])
}
}
return result
}
function checkContractedSounds(char) {
switch (char) {
case 'ぁ':
case 'ぃ':
case 'ぅ':
case 'ぇ':
case 'ぉ':
case 'ゃ':
case 'ゅ':
case 'ょ':
return true
default:
return false
}
}
function toRoman(input) {
input = input.replace(
/[A-Za-z0-9!”#$%&’()=~|{}‘*+_?<>、。¥;:」ー「@^- ]/g,
function (s) {
switch (s) {
case '”':
return '"'
case '’':
return "'"
case '‘':
return "'"
case '、':
return ','
case '。':
return '.'
case '¥':
return '\\'
case '「':
return '['
case '」':
return ']'
case 'ー':
return '-'
case ' ':
return ' '
default:
return String.fromCharCode(s.charCodeAt(0) - 0xfee0)
}
}
)
var result = []
for (var i = 0; i < input.length; i++) {
var temp
while (input[i] == 'っ') {
var next = convert(input[i + 1])
if (
next &&
!input[i + 1].match(/[あ-お]/) &&
!input[i + 1].match(/[な-の]/) &&
!checkContractedSounds(next) &&
input[i + 1] != 'ゎ'
) {
temp = []
if (checkContractedSounds(input[i + 2])) {
next = specificStringConvert(input[i + 1] + input[i + 2])
i++
}
for (var j = 0; j < next.length; j++) {
if (typeof next[j] == typeof []) {
var foo = []
for (var k = 0; k < next[j].length; k++) {
foo.push(k == 0 ? next[j][k][0] + next[j][k] : next[j][k])
}
temp.push(foo)
} else {
temp.push(next[j][0] + next[j])
}
var foo = convert('っ')
var foo2 = []
for (var k = 0; k < foo.length; k++) {
if (typeof next[j] == typeof []) {
foo2 = []
foo2.push(foo[k])
for (var l = 0; l < next[j].length; l++) {
foo2.push(next[j][l])
}
temp.push(foo2)
} else {
temp.push([foo[k], next[j]])
}
}
}
i++
result.push(temp)
} else {
result.push(convert(input[i]))
}
i++
if (i >= input.length) return result
}
var temp = convert(input[i])
if (temp && checkContractedSounds(input[i + 1])) {
temp = specificStringConvert(input[i] + input[i + 1])
i++
}
result.push(temp ? temp : [input[i]])
}
return result
}
function calcStringNum(list) {
var result = []
var max = 0
for (var i = 0; i < list.length; i++) {
max = 0
for (var j = 0; j < list[i].length; j++) {
if (typeof list[i][j] == typeof []) {
if (max < list[i][j].length) max = list[i][j].length
} else {
if (max < 1) max = 1
}
}
result.push(max)
}
return result
}
function register(string) {
this.original = string
this.question = toRoman(string)
this.answeredString = ''
this.length = this.question.length
this.select = []
this.currentIndex = 0
this.answered = 0
this.currentArrayIndex = 0
this.fault = 0
this.stringNum = calcStringNum(this.question)
this.absoluteAnswered = 0
for (var i = 0; i < this.length; i++) {
this.select.push(0)
}
this.remainedQuestion = this.getQuestion()
}
function deleteElement(array, deleteList) {
var count = 0
for (var i = 0; i < deleteList.length; i++) {
array.splice(deleteList[i] - i, 1)
}
}
/*
配列のx番目までのlengthの合計
*/
function arrayInnerLength(array, x) {
var sum = 0
for (var i = 0; i < x; i++) {
sum += array[i].length
}
return sum
}
function answer(char, base) {
if (!base) base = 0
var temp = this.question[base]
var correct = false
var currentSelect = 0
var deleteList = []
if (temp[0] == 'nn' && this.currentIndex == 1 && this.question[1]) {
this.currentIndex = 0
if (char == 'n') {
this.question.splice(0, 1)
this.absoluteAnswered++
this.answered++
return true
} else if (this.answer(char, 1)) {
this.question.splice(0, 1)
this.absoluteAnswered++
this.answered++
return true
} else {
this.currentIndex = 1
this.fault++
return false
}
}
for (var i = 0; i < temp.length; i++) {
if (typeof temp[i] == typeof []) {
var currentArray = temp[i][this.currentArrayIndex]
if (currentArray[this.currentIndex - arrayInnerLength(temp[i], this.currentArrayIndex)] == char) {
if (!correct) {
correct = true
this.select[this.answered] = i
currentSelect = i
this.answeredString += char
}
if (this.currentIndex + 1 == arrayInnerLength(temp[i], temp[i].length)) {
this.currentIndex = 0
this.currentArrayIndex = 0
this.absoluteAnswered += this.stringNum[this.answered]
this.answered++
this.question.splice(0, 1)
return true
}
} else {
deleteList.push(i)
}
} else {
if (temp[i][this.currentIndex] == char) {
if (!correct) {
correct = true
this.select[this.answered] = i
currentSelect = i
this.answeredString += char
}
if (this.currentIndex + 1 == temp[i].length) {
this.currentIndex = 0
this.currentArrayIndex = 0
this.absoluteAnswered += this.stringNum[this.answered]
this.answered++
this.question.splice(0, 1)
return true
}
} else {
deleteList.push(i)
}
}
}
if (correct) {
this.currentIndex++
if (typeof temp[currentSelect] == typeof []) {
if (this.currentIndex == arrayInnerLength(temp[currentSelect], this.currentArrayIndex + 1)) {
this.currentArrayIndex++
this.absoluteAnswered++
this.stringNum[this.answered]--
}
if (this.currentIndex == arrayInnerLength(temp[currentSelect], temp[currentSelect].length)) {
this.currentIndex = 0
this.currentArrayIndex = 0
this.answered++
this.question.splice(0, 1)
} else {
deleteElement(temp, deleteList)
}
} else {
if (this.currentIndex == temp[currentSelect].length) {
this.currentIndex = 0
this.currentArrayIndex = 0
this.answered++
this.question.splice(0, 1)
} else {
deleteElement(temp, deleteList)
}
}
return true
} else {
this.fault++
return false
}
}
// 次の正しいキー入力を取得
function getNextKey() {
//console.log("-----------------------------------------");
var temp = this.question[0]
var res = []
for (i = 0; i < temp.length; i++) {
if (typeof temp[i] == typeof []) {
if (temp[i][0].length <= this.currentIndex) {
res[temp[i][1].substr(this.currentIndex - temp[i][0].length, 1)] = ''
} else {
res[temp[i][0].substr(this.currentIndex, 1)] = ''
}
} else {
res[temp[i].substr(this.currentIndex, 1)] = ''
}
}
//console.log(res);
return res
}
function isFinish() {
if (this.answered == this.length) return true
else return false
}
function getFault() {
return this.fault
}
function getQuestion() {
var result = ''
for (var i = 0; i < this.length; i++) {
if (!this.question[i]) continue
if (typeof this.question[i] == typeof []) {
for (var j = 0; j < this.question[i][0].length; j++) {
result += this.question[i][0][j]
}
} else {
result += this.question[i][0]
}
}
return result
}
function getRemainedQuestion() {
var result = this.getQuestion()
return result.substr(this.currentIndex, result.length)
}
function getRemainedOriginalQuestion() {}
function getAnsweredString() {
return this.answeredString
}
function getOriginalQuestion() {
return this.original
}
function getAbsoluteAnswered() {
return this.absoluteAnswered
}
_Typing.prototype.register = register
_Typing.prototype.answer = answer
_Typing.prototype.getNextKey = getNextKey
_Typing.prototype.isFinish = isFinish
_Typing.prototype.getQuestion = getQuestion
_Typing.prototype.getRemainedQuestion = getRemainedQuestion
_Typing.prototype.getAnsweredString = getAnsweredString
_Typing.prototype.getOriginalQuestion = getOriginalQuestion
_Typing.prototype.getAbsoluteAnswered = getAbsoluteAnswered
_Typing.prototype.getFault = getFault
})(window)