query-text-match

0.2.7 • Public • Published

Query Text Match

Calculates the matching score between query and text

Installation

yarn add query-text-match

Sample

const _ = require('lodash');
const queryTextMatch = require('./index');

const query = "because I can't you";

const texts = [
  'The stranger officiates the meal.',
  'When transplanting seedlings, candied teapots will make the task easier.',
  'His mind was blown that there was nothing in space except space itself.',
  "Please put on these earmuffs because I can't you hear.",
  'Flash photography is best used in full sunlight.',
  'It dawned on her that others could make her happier, but only she could make herself happy.',
  'He was the type of guy who liked Christmas lights on his house in the middle of July.',
  "Art doesn't have to be intentional.",
  'It was getting dark, and we weren’t there yet.',
  'He learned the important lesson that a picnic at the beach on a windy day is a bad idea.',
];

const results = texts.map((text) => ({ ...queryTextMatch(query, text), text }));

console.log(
  JSON.stringify(
    _.sortBy(results, [
      'unmatchedQueryWordsCount',
      'extraCharactersCount',
      'totalDistance',
    ]),
    null,
    2
  )
);

Outcome:

[
  {
    "matchedQueryWordsCount": 3,
    "unmatchedQueryWordsCount": 1,
    "queryWordsCount": 4,
    "totalDistance": 6,
    "extraCharactersCount": 0,
    "takenWords": [
      {
        "index": 29,
        "actualWord": "because",
        "extraPerfixLength": 0,
        "extraSufixLength": 0,
        "UnNormalizedQueryWord": "because",
        "normalizedQueryWord": "because",
        "extraCharsLength": 0,
        "taken": true,
        "surroundingDistance": 7
      },
      {
        "index": 37,
        "actualWord": "I",
        "extraPerfixLength": 0,
        "extraSufixLength": 0,
        "UnNormalizedQueryWord": "I",
        "normalizedQueryWord": "i",
        "extraCharsLength": 0,
        "taken": true,
        "surroundingDistance": 14
      },
      {
        "index": 45,
        "actualWord": "you",
        "extraPerfixLength": 0,
        "extraSufixLength": 0,
        "UnNormalizedQueryWord": "you",
        "normalizedQueryWord": "you",
        "extraCharsLength": 0,
        "taken": true,
        "surroundingDistance": 7
      }
    ],
    "queryNormalizedWordsList": [
      "because",
      "i",
      "cant",
      "you"
    ],
    "text": "Please put on these earmuffs because I can't you hear."
  },
  {
    "matchedQueryWordsCount": 1,
    "unmatchedQueryWordsCount": 3,
    "queryWordsCount": 4,
    "totalDistance": 0,
    "extraCharactersCount": 0,
    "takenWords": [
      {
        "index": 42,
        "actualWord": "i",
        "extraPerfixLength": 0,
        "extraSufixLength": 0,
        "UnNormalizedQueryWord": "i",
        "normalizedQueryWord": "i",
        "extraCharsLength": 0,
        "taken": true
      }
    ],
    "queryNormalizedWordsList": [
      "because",
      "i",
      "cant",
      "you"
    ],
    "text": "His mind was blown that there was nothing in space except space itself."
  },
  ...
]

/query-text-match/

    Package Sidebar

    Install

    npm i query-text-match

    Weekly Downloads

    1

    Version

    0.2.7

    License

    MIT

    Unpacked Size

    11 kB

    Total Files

    6

    Last publish

    Collaborators

    • faressoft