-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
62 lines (58 loc) · 1.88 KB
/
index.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
'use strict'
const createAC = require('./ac')
const trie = require('./data')
const automata = createAC(trie)
module.exports = function (number) {
const numbers = search(number.split('').map(function (n) { return +n }), automata, [])
// Filter a few out
return numbers.filter(function (num) {
// Nothing but numbers?
if (/^\d+$/.test(num)) return false
// Only 1-letter words + numbers?
if (/\d/.test(num) && !(/[a-z]{2}/.test(num))) return false
return true
})
}
function coalesceNumbers (soFar, nums) {
const lastSoFar = soFar[soFar.length - 1]
if (lastSoFar && lastSoFar.length > 0 && '0123456789'.indexOf(lastSoFar[lastSoFar.length - 1]) !== -1) {
return soFar.slice(0, soFar.length - 1).concat(nums)
} else {
return soFar.concat(nums)
}
}
function search (data, initState, soFar) {
if (data.length === 0) {
if (soFar.length === 0) return []
return [soFar.join(' ')]
}
let leads = ''
while (data.length > 0 && data[0] < 2) {
leads += data[0]
data = data.slice(1)
}
if (leads !== '') {
soFar = coalesceNumbers(soFar, leads)
}
let wordlist = []
let state = initState
for (let i = 0; i < data.length;) {
state = state.push(data[i++])
for (let cur = state; cur.v !== undefined; cur = cur.next) {
// Nothing here, add as number and try again.
if (cur.v.length === 0) {
wordlist = wordlist.concat(search(data.slice(i), initState, coalesceNumbers(soFar, data.slice(0, i).join(''))))
}
cur.v.forEach(function (word) {
if (word.length === i) {
wordlist = wordlist.concat(search(data.slice(i), initState, soFar.concat(word)))
return
}
const sf = coalesceNumbers(soFar, data.slice(0, i - word.length).join(''))
wordlist = wordlist.concat(search(data.slice(i), initState, sf.concat(word)))
})
}
}
return wordlist
}
// Current as of __GITHASH__