Henrique Rodrigues
Henrique Rodrigues

Reputation: 73

How to find similar text with nodejs

I'm trying to develop a simple search engine to get match sentences in a text file with nodejs, but i want to improve my search engine to get similar text and not just the exact text, any suggestions of how can I do this?

this is my code:

const folder = "./movies/data";
const fs = require("fs");

function search(params) {
  let list = [];

  fs.readdirSync(folder).forEach((file) => {
    const data = fs.readFileSync(`movies/data/${file}`, {
      encoding: "utf8",
      flag: "r",
    });
    if (data.includes(params)) {
      list.push(data);
    }
  });
  console.log(
    `Foram encontradas ${list.length} ocorrências pelo termo ${params}.`
  );
  return `Foram encontradas ${list.length} ocorrências pelo termo ${params}.`;
}

let args = process.argv.slice(2);
search(args.join(" "));

module.exports = search;

Upvotes: 3

Views: 1193

Answers (1)

JustinMartinDev
JustinMartinDev

Reputation: 639

Before to implement any algorith, you have to choose a text comparison algorithm.

On of the best is the Levenshtein distance
https://en.wikipedia.org/wiki/Levenshtein_distance

Link of Levenshtein distance implementation in JS
https://www.tutorialspoint.com/levenshtein-distance-in-javascript

Upvotes: 1

Related Questions