npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

quick-match-intent

v0.0.1

Published

Fast string matcher conceived for quick replies chat and multiple choice scenario.

Downloads

2

Readme

quick-match

Build Status NPM version js-standard-style stability-stable

Conversational interfaces are increasingly popular, Artificial intelligence, NLP/NLU solutions are at the forefront.
But, there is often the need for something simple, blazingly fast, and "offline" to solve the text matching problem.
This is a common issue with chat Quick Replies, multiple choice answers, and vocal interfaces (even less reliable).
Quick Match provides a toolkit to address all these problems in the best possible way.

Do you have a good idea or want to make the matching algorithm even more efficient? Collaborate!

Usage

Install

npm i -s quick-match

Simple initialization with default algorithm:

const { QuickMatch } = require('quick-match')

const qm = new QuickMatch()

const userInput = 'I want a pizza'
const candidates = ['Free hot dog here', 'Pizza for sale', 'Rent your cola']

const { bestCandidateIdx } = qm.run(userInput, candidates) // 1

Available algorithms

  • Dice's coefficient (max score => best result)
const { QuickMatch } = require('quick-match')
const qm = new QuickMatch({ algorithm: 'dice' })

const input = 'I want a pizza'
const qr = ['Free hot dog here', 'Pizza for sale', 'Rent your cola']

const { candidates, bestCandidateIdx, maxScore } = qm.run(input, qr)

// candidates
// [
//   {
//     text: 'Free hot dog here',
//     keywords: [],
//     score: 0.06896551724137931,
//     stemmed: [ 'free', 'here' ],
//     intersections: []
//   },
//   {
//     text: 'Pizza for sale',
//     keywords: [],
//     score: 0.3076923076923077,
//     stemmed: [ 'pizza', 'sale' ],
//     intersections: [ 'pizza' ]
//   },
//   {
//     text: 'Rent your cola',
//     keywords: [],
//     score: 0.15384615384615385,
//     stemmed: [ 'rent', 'your', 'cola' ],
//     intersections: []
//   }
// ]

// bestCandidateIdx: 1
// maxScore: 0.3076923076923077
  • Levenshtein distance (min score => best result)
const { QuickMatch } = require('quick-match')
const qm = new QuickMatch({ algorithm: 'levenshtein' })

const input = 'I want a pizza'
const qr = ['Free hot dog here', 'Pizza for sale', 'Rent your cola']

const { candidates, bestCandidateIdx, minScore } = qm.run(input, qr)

// candidates
// [
//   {
//     text: 'Free hot dog here',
//     keywords: [],
//     score: 15,
//     stemmed: [ 'free', 'here' ],
//     intersections: []
//   },
//   {
//     text: 'Pizza for sale',
//     keywords: [],
//     score: 12,
//     stemmed: [ 'pizza', 'sale' ],
//     intersections: [ 'pizza' ]
//   },
//   {
//     text: 'Rent your cola',
//     keywords: [],
//     score: 12,
//     stemmed: [ 'rent', 'your', 'cola' ],
//     intersections: []
//   }
// ]

// bestCandidateIdx: 1
// minScore: 12

Stemming

You can also enable them stemming, applying the algorithms only on the stem (root) of the words.
This, sometimes, is useful to reduce the noise.

const { QuickMatch } = require('quick-match')
const qm = new QuickMatch({
  algorithm: 'dice',
  enableStemming: true,
  stemming: {
    language: 'English',
    // (optional) Shorter words than this number BEFORE stemming are removed
    minPreStemmingLength: 3,
    // (optional) Shorter words than this number AFTER stemming are removed
    minPostStemmingLength: 4
  }
})

const input = 'I discussed about food'
const qr = [
  { text: 'Discussing food' },
  { text: 'Eating and running' }
]

const { stemmedText, bestCandidateIdx } = qm.run('i have discussed about food', qr)

// stemmedText: [ 'have', 'discuss', 'about', 'food', 'drink' ]
// bestCandidateIdx: 0

Available languages:

  • Arabic
  • Danish
  • Dutch
  • English
  • French
  • German
  • Greek
  • Hungarian
  • Italian
  • Portuguese
  • Romanian
  • Russian
  • Spanish
  • Swedish
  • Tamil
  • Turkish

Words intersections

Another useful feature is the possibility to have the intersections among user input and possible candidates. This can be used further to better assess the fittest candidate.

const { QuickMatch } = require('quick-match')
const qm = new QuickMatch({
  algorithm: 'dice',
  enableStemming: true,
  stemming: { language: 'English', minPreStemmingLength: 4, minPostStemmingLength: 4 }
})

const input = 'I have discussed about mealing and foot'
// As you can see, the candidates can be only strings or object in the format { text: string, keywords: [string] } to improve the matching with relatex keyworkds
const qr = [
  { text: 'Discussing food', keywords: ['eating', 'meal'] },
  { text: 'Eating and running', keywords: ['jogging', 'footing'] }
]

const { candidates, maxIntersections, maxIntersectionsCandidateIdx, bestCandidateIdx } = qm.run(input, qr)
// candidates
// [
//   {
//     text: 'Discussing food',
//     keywords: [ 'eating', 'meal' ],
//     score: 0.4230769230769231,
//     stemmed: [ 'discuss', 'food', 'meal' ],
//     intersections: [ 'discuss', 'meal' ]
//   },
//   {
//     text: 'Eating and running',
//     keywords: [ 'jogging', 'footing' ],
//     score: 0.2545454545454545,
//     stemmed: [ 'foot' ],
//     intersections: [ 'foot' ]
//   }
// ]

// maxIntersections: 2
// maxIntersectionsCandidateIdx: 0
// bestCandidateIdx: 0

Usage of numbers

It sometimes useful, especially in chat and voice interaction to recognize digits as user's answer

Simple digit matching

const { QuickMatch } = require('quick-match')
const qm = new QuickMatch({
  numbers: {
    enableDigits: true,
    maxDigit: 5
  }
})
const qr = ['foo', 'bar', 'zoo']
const { numberMatch, numberMatchType, bestCandidateIdx } = qm.run('1', qr)

// numberMatch: true
// numberMatchType: 'digit'
// bestCandidateIdx: 0

Simple cardinal matching

const { QuickMatch } = require('quick-match')
const qm = new QuickMatch({
  numbers: {
    enableCardinals: true,
    cardinals: ['uno', 'due', 'tre'] // Specify your custom cardinals based on your language
  }
})
const qr = ['foo', 'bar', 'zoo']
const res = qm.run('la due', qr) // It tries to get the number even if other small "noise" words

// numberMatch: true
// numberMatchType: 'cardinal'
// bestCandidateIdx: 1

Simple ordinal matching

const { QuickMatch } = require('quick-match')
const qm = new QuickMatch({
  numbers: {
    enableOrdinals: true,
    ordinals: ['prima', 'seconda', 'terza'] // Specify your custom ordinals based on your language
  }
})
const qr = ['foo', 'bar', 'zoo']
const res = qm.run('seconda scelta', qr)

// numberMatch: true
// numberMatchType: 'ordinal'
// bestCandidateIdx: 1

Lots of possibilities

A lot of customization with options for every detail:

const { QuickMatch } = require('quick-match')

const qm = new QuickMatch({
  algorithm: 'dice',
  enableStemming: true,
  stemming: {
    language: 'English',
    minPreStemmingLength: 4,
    minPostStemmingLength: 4
  },
  enableAlgorithmOnKeywords: false,
  numbers: {
    enableDigits: true,
    enableCardinals: true,
    enableOrdinals: true,
    maxDigit: 10,
    maxWordsEnablingNumbers: 2,
    cardinals: [
      'one', 'two', 'three', 'four', 'five',
      'six', 'seven', 'eigth', 'nine', 'ten'
    ],
    ordinals: [
      'first', 'second', 'third', 'fourth', 'fifth',
      'sixth', 'seventh', 'eighth', 'ninth', 'tenth'
    ]
  },
  limits: {
    minLengthCandidate: 3,
    maxCandidateWords: 5
  },
  weightIntersectionMultiplier: 1
})

// Declare your input text, candidates and run the algorithm
const userInput = 'I want a pizza'
const candidates = [
  { text: 'Free hot dog here', keywords: ['hot dog', 'free'] },
  { text: 'Pizza for sale', keywords: ['pizza', 'margherita'] },
  { text: 'Rent your cola', keywords: ['coke', 'cola'] }
]

const { bestCandidateIdx } = qm.run(userInput, candidates) // 1

Result output format:

{
  "algorithm": "dice",
  "minScore": 0.06451612903225806,
  "maxScore": 0.2857142857142857,
  "maxIntersections": 1,
  "candidates": [
    {
      "text": "I want hot-dog here",
      "keywords": [
        "hot-dog",
        "free"
      ],
      "score": 0.24242424242424243,
      "stemmed": [
        "want",
        "here",
        "hot-dog",
        "free"
      ],
      "intersections": [
        "want"
      ]
    },
    {
      "text": "Pizza for sale",
      "keywords": [
        "pizza",
        "margherita"
      ],
      "score": 0.2857142857142857,
      "stemmed": [
        "pizza",
        "sale",
        "pizza",
        "margherita"
      ],
      "intersections": [
        "pizza"
      ]
    },
    {
      "text": "Renting your cola",
      "keywords": [
        "coke",
        "cola"
      ],
      "score": 0.06451612903225806,
      "stemmed": [
        "rent",
        "your",
        "cola",
        "coke",
        "cola"
      ],
      "intersections": []
    }
  ],
  "text": "I wanted a pizza",
  "stemmedText": [
    "want",
    "pizza"
  ],
  "minCandidateIdx": 2,
  "maxCandidateIdx": 1,
  "maxIntersectionsCandidateIdx": 0,
  "bestCandidateIdx": 1,
  "bestCandidate": {
    "text": "Pizza for sale",
    "keywords": [
      "pizza",
      "margherita"
    ],
    "score": 0.2857142857142857,
    "stemmed": [
      "pizza",
      "sale",
      "pizza",
      "margherita"
    ],
    "intersections": [
      "pizza"
    ]
  }
}