-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
64 lines (50 loc) · 1.7 KB
/
main.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
const universeOfDiscourse = [
"Google", "Alphabet", "Facebook", "Amazon", "Apple", "Microsoft"
];
function getBigram(word) {
let result = [];
for (let i = 0; i < word.length-1; i++) {
result.push(word[i] + word[i+1]);
}
return result;
}
function getSimilarity(word1, word2) {
word1 = word1.toLowerCase();
word2 = word2.toLowerCase();
const bigram1 = getBigram(word1), bigram2 = getBigram(word2);
let similar = [];
for (let i = 0; i < bigram1.length; i++) {
if (bigram2.indexOf(bigram1[i]) > -1) {
similar.push(bigram1[i]);
}
}
return similar.length / Math.max(bigram1.length, bigram2.length);
}
function autoCorrect(word, knownWords=universeOfDiscourse, similarityThreshold=0.5) {
let maxSimilarity = 0;
let mostSimilar = word;
for (let i = 0; i < knownWords.length; i++) {
let similarity = getSimilarity(knownWords[i], word);
if (similarity > maxSimilarity) {
maxSimilarity = similarity;
mostSimilar = knownWords[i];
}
}
return maxSimilarity > similarityThreshold ? mostSimilar : word;
}
function submit() {
let text = document.getElementById("text").value;
if (text.length > 0) {
const lastChar = text[text.length - 1];
if (lastChar === " ") {
text = text.split(" ");
const lastWord = text[text.length - 2];
text[text.length - 2] = autoCorrect(lastWord);
text = text.join(" ");
}
document.getElementById("text").value = text;
document.getElementById("output").innerHTML = "You know: <br/>" + text;
}
else
document.getElementById("output").innerHTML = "";
}