-
Notifications
You must be signed in to change notification settings - Fork 4
/
grams.js
109 lines (91 loc) · 2.56 KB
/
grams.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
function get(object, key){
return object[key.toUpperCase()] || 0
}
function letter1 (possiblefirstletters) {
return possiblefirstletters
.split("")
.sort(function(a,b){
return -(get(onegrams,a.toUpperCase())-get(onegrams,b.toUpperCase()))
})[0]
}
function letter2 (firstletter, possiblenextletters) {
var probs = twograms[firstletter.toUpperCase()]
return nextletters = possiblenextletters
.split("")
.sort(function(a,b){
return get(probs,b)-get(probs,a)
})[0]
}
function lastletter (previoustwo, possibilities) {
var probs = threegrams_end[previoustwo.toUpperCase()]
return possibilities
.split("")
.sort(function(a,b){
return get(probs,b)-get(probs,a)
})
.map(function(a){
return [a, get(probs,a)]
})[0]
}
function middleletter (letterbefore, possibilities, letterafter) {
var probs = threegrams_middle[(letterbefore+letterafter).toUpperCase()]
return possibilities.split('')
.sort(function(a,b){
return get(probs,b)-get(probs,a)
})
.map(function(a){
return [a, get(probs,a)]
})[0]
}
function firstletter (possibilities, twoafter) {
var probs = threegrams_middle[twoafter.toUpperCase()]
return possibilities.split('')
.sort(function(a,b){
return get(probs,b)-get(probs,a)
})
.map(function(a){
return [a, get(probs,a)]
})[0]
}
function probability(letters, letter, index) {
return [
letter,
lastletter(letters[index-2]+letters[index-1],letter)[1] *
middleletter(letters[index-1],letter,letters[index+1])[1] *
firstletter(letter,letters[index+1]+letters[index+2])[1]
]
}
function bestpossibility(letters, index){
var alphabet = 'abcdefghijklmnopqrstuvwxyz'
var probofcurrent = probability(letters, letters[index], index)[1]
return alphabet.split('').map(function(letter){
var base = probability(letters, letter, index)
return [base[0], base[1]-probofcurrent]
})
.sort(function(a,b){
return b[1]-a[1]
})[0]
}
function fixword (word) {
var letters = word.split('')
var possibilities = []
for(var i = 2; i< letters.length-2; i++){
possibilities.push([bestpossibility(letters, i),i])
console.log('hello',bestpossibility(letters, i),i)
}
var replacement = possibilities.sort(function(a,b){
return b[0][1]-a[0][1]
})[0]
letters[replacement[1]] = [replacement[0][0]]
return letters.join('')
}
function fullword (possibilityarray) {
var full = letter1(possibilityarray.shift())
if(possibilityarray.length>1){
full += letter2(full, possibilityarray.shift())
while(possibilityarray.length>0){
full += lastletter(full.slice(-2), possibilityarray.shift())[0]
}
}
return full.toLowerCase()
}