Skip to content

Commit

Permalink
word comparison fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbots committed Jul 15, 2022
1 parent d61d03f commit e9bb446
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions app/utils/textReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const setWorker = async (language) => {
await worker.load();
await worker.loadLanguage(language);
await worker.initialize(language);
}
};

const readTextFrom = async (buffer, scale) => {
let img = await Jimp.read(buffer);
Expand All @@ -20,9 +20,23 @@ const readTextFrom = async (buffer, scale) => {
return words;
};

const getField = (attr) => (obj) => obj[attr];
const percentComparison = (first, second) => {
return ([...first].reduce((total, letter) =>
[...second].includes(letter) ? total + 1 : total, 0) / first.length) * 100
if (second.length > first.length) {
[first, second] = [second, first];
}

let mainLetters = Array.from(new Set([...first])).map(letter => ({letter, index: []}));
[...first].forEach(letter => {
let prevIndexes = mainLetters.find((mainLetter) => mainLetter.letter == letter).index;
let lastPrevIndex = prevIndexes[prevIndexes.length - 1];
let index = second.indexOf(letter, lastPrevIndex ? lastPrevIndex + 1 : 0);
if(index != -1) {
prevIndexes.push(index);
}
});

return mainLetters.flatMap(getField(`index`)).length / first.length * 100
};

const sortWordsByItem = (words, itemHeight) => {
Expand Down

0 comments on commit e9bb446

Please sign in to comment.