-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
78 lines (57 loc) · 1.44 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
var dict = require('cmu-pronouncing-dictionary')
var own = {}.hasOwnProperty
var words = []
Object.keys(dict).forEach(function(word) {
words.push({word: word, pron: dict[word]})
})
module.exports = rhymes
rhymes.words = words
function rhymes(value) {
var results = []
var pron
if (!value) return results
value = String(value).toLowerCase()
if (!own.call(dict, value)) return results
pron = dict[value]
words.forEach(check)
return results.sort(sort).slice(0, 20)
function check(other) {
var score = countMatchingTrailingSyllables(pron, other.pron)
if (score > 1) {
results.push({
score: score,
pron: other.pron,
word: cleanAlternative(other.word)
})
}
}
}
function countMatchingTrailingSyllables(a, b) {
var left = reverseSyllables(a)
var right = reverseSyllables(b)
var length = Math.max(left.length, right.length)
var index = -1
var score = 0
while (++index < length) {
if (left[index] !== right[index]) {
return score
}
score++
}
// Do not return words with exactly the same pronunciation (`kat` for `cat`)
return 0
}
// `donkey(1)` -> `donkey`
function cleanAlternative(word) {
var pos = word.indexOf('(')
return pos === -1 ? word : word.slice(0, pos)
}
function reverseSyllables(d) {
return d.split(' ').reverse()
}
function sort(a, b) {
return pick(b) - pick(a) || a.word.localeCompare(b.word)
}
function pick(d) {
return d.score
}