Shaokang's Blog

This is a simple auto correct javascript function based on Bi-gram. It will handle input and find any possible similarity based on threshold. It is a part of a coursework. This work is solely on my own. And because of a previous bad experience on posting code, just a short portion of key codes will be shown.

Core function

The getSimilarLevel function will produce the similarity level between two input words. Basically, it is just to compare and calculate the number of 2-grams that is the same. getTwoGram function is just to generate 2-gram used to compare.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function getTwoGram(data) {
let toRe = [];
for (let i = 0; i < data.length-1; i++) {
toRe.push(data[i] + data[i+1]);
}
return toRe;
}

function getSimilarLevel(data1, data2) {
data1 = data1.toLocaleLowerCase();
data2 = data2.toLocaleLowerCase();
const twogram1 = getTwoGram(data1), twogram2 = getTwoGram(data2);
let count = 0;

for (let i of twogram1) {
if (twogram2.indexOf(i) !== -1) {
count++;
}
}

return count / Math.max(twogram1.length, twogram2.length);
}

Possible way of using:

A word will be pushed to the possible waiting list when they have a similarity level greater than the threshold.

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
let keywords = ["a","b","c"]
generateSuggestion(input) {
let possibleSuggestion = [];

for (let i of keywords) {
let similarity = getSimilarLevel(i, input);
if(similarity > threshold)
possibleSuggestion.push([similarity, i]);
}
//sort

possibleSuggestion.sort();
possibleSuggestion.reverse();

if(possibleSuggestion.length <=6) {
let toRe = [];
for(let i = 0;i<possibleSuggestion.length;i++){
toRe.push(possibleSuggestion[i][1]);
}
return toRe;
}

let toRe = [];

for(let i = 0;i<6;i++){
toRe.push(possibleSuggestion[i][1]);
}

return toRe;
}

 Comments