Please, it’s urgent.I’m finding it difficult to retrieve the percentage of matched text for a plagiarism project I’m working on. The project is being built via Node and Express JS. THe matched text occurs from comparing text to a pattern to find any similarities. With the kmp and naive algorithms, I’m able to get -1 if there’s no match and 100% if there’s a match, even if I try to split the text and substring into individual characters( because they are arrays) and divide the matched text with the substring or text, I’m unable to get an accurate percentage. And please, how am I able to differentiate between citation and text based plagairism from the algorithms. Please I need your help.
The code is attached below
//kmp algorithm
`function KMPtable(pattern){
let LPS = [0]
let i =0;
let j = 1;
while( i < pattern.length){
if(pattern[i] === pattern[j]){
i++;
j++;
LPS[i] = j
}else if(j > 0){
j = LPS[j-1]
}else{
i+=1;
LPS[i] = 0
}
}
return LPS
}
const KMPsearch = (text, substring)=>{
let i = 0;
let j = 0;
let LPStable = KMPtable(substring);
const textWords = text.split(" ");
const substringWords = substring.split(" ");
const matchedText = []
while ( i < textWords.length && j < substringWords.length){
if(textWords[i] === substringWords[j]){
i++;
j++;
LPStable[i] = j
}else if(j > 0){
matchedText.push(i-j);
j = LPStable[j-1]
}else{
i+=1;
}
}
return j === substring.length ? matchedText: -1
}
module.exports = {
KMPsearch
}
//naive string
const naiveSearch =(string, substring)=>{
if(substring === "") return 0
let count = 0;
for(let i =0; i < string.length; i++){
for(let j = 0; j < substring.length ; j++){
if(string[i] !== substring[i + j]){
break
}
}
if(j === substring.length - 1){
count++;
}
}
}
module.exports = naiveSearch;`
console.log(naiveSearch("jiurhriuer plo ", "hruheurhe rejorjiej plo"))
//`
Using the KMP and Naive algorithms, I can receive a value of -1 when there is no match, and 100% when there is a match. Even if I attempt to split the text into individual characters (since they are represented as arrays) and then divide the matched text by the substring or text, I am still unable to obtain an accurate percentage.