/
t9.js
108 lines (83 loc) · 2.58 KB
/
t9.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
/*! t9 v0.0.0 - MIT license */
'use strict';
var T9Tree = function (prefix) {
// your code goes here
var t9 = Object.create(t9Properties);
// c is short for characters
t9.nextChar = {};
t9.value = prefix || "";
t9.isWord = false;
return t9;
}
var t9Properties = {
insertWord: function(word) {
// 'Word' ==> firstChar:'W'; remainig:'ord'
var firstChar = word[0];
var remaining = word.slice(1);
var nextNode = this.nextChar[firstChar];
if (!nextNode) {
var prefix = this.value + firstChar;
this.nextChar[firstChar] = T9Tree(prefix);
nextNode = this.nextChar[firstChar];
}
if (remaining) {
nextNode.insertWord(remaining);
} else {
// No more characters remaining in the word?
nextNode.isWord = true;
}
},
getAllWords: function() {
var words = [];
if (this.isWord) {
words.push(this.value);
}
for (var key in this.nextChar) {
words = words.concat(this.nextChar[key].getAllWords());
}
return words;
},
getWords: function(numbers) {
numbers = numbers.toString().split('');
var posibilities = [];
for (var i = 0; i < numbers.length; i++) {
var number = numbers[i];
posibilities.push(this.keyMap[number]);
}
/*
- Elements in posibilities ARE ordered dependeing on input 'numbers'
- Each layer is an element of posiblities. LAYER => ['A','B','C','a','b','c']
- Each layer is composed of characters that we will look up in nextChar of current trie
['G', ***'H'*** ,'I','g','h','i'],
['D','E','F','d', ***'e'*** ,'f'],
['W','X','Y','Z','w','x', ***'y'*** ,'z']
- if we can't go deeper (No more layers), then we will look for All words in and below that trie
*/
var results = [];
var goDeeper = function(layers, trie) {
var baseLayer = layers[0];
if (!baseLayer) {
results = results.concat(trie.getAllWords());
} else {
for (var i = 0; i < baseLayer.length; i++) {
var character = baseLayer[i];
if (trie.nextChar[character]) {
goDeeper(layers.slice(1), trie.nextChar[character]);
}
}
}
};
goDeeper(posibilities, this)
return results;
},
keyMap: {
2: ['A','B','C','a','b','c'],
3: ['D','E','F','d','e','f'],
4: ['G','H','I','g','h','i'],
5: ['J','K','L','j','k','l'],
6: ['M','N','O','m','n','o'],
7: ['P','Q','R','S','p','q','r','s'],
8: ['T','U','V','t','u','v'],
9: ['W','X','Y','Z','w','x','y','z']
}
};