Socket
Socket
Sign inDemoInstall

@basementuniverse/bm25

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @basementuniverse/bm25

Search for terms in an array of documents


Version published
Maintainers
1
Created

Readme

Source

Okapi BM25

Search for terms in an array of documents using Okapi BM25.

Installation

npm install -g @basementuniverse/bm25

Usage

import { Corpus } from '@basementuniverse/bm25';

const corpus = new Corpus([
  'This is a document',
  'Here is another document',
]);

const results = corpus.search('document');

results will look something like:

[
  {
    "document": "This is a document",
    "score": 0.5
  },
  {
    "document": "Here is another document",
    "score": 0.5
  }
]

The documents passed into the Corpus constructor will be treated as strings by default, and will be converted to lowercase and split by non-word characters.

However, it is possible to pass in values of any type here, as long as you provide a function to convert each value to an array of strings. For example:

const corpus = new Corpus(
  [
    {
      id: '1234',
      name: 'John Doe',
    },
    {
      id: '2345',
      name: 'Jane Doe',
    },
  ],
  {
    processor: document => [document.id, ...document.name.toLowerCase().split(' ')],
  },
);

Partial term matching can be enabled by passing true as the second argument to search():

const results = corpus.search('doe', true);

Options

The 2nd argument to the Corpus constructor is an options object, which can contain the following properties:

  • processor (function) - A function to convert each document to an array of strings.
  • k1 (number between 1.2 and 2, default: 1.5) - Controls the impact of term frequency saturation.
  • b (number between 0 and 1, default: 0.75) - Controls how much the document length affects the term frequency score.
  • gamma (number, default: 1) - Addresses a deficiency of BM25 in which term frequency normalization by document length is not properly lower-bounded.

Keywords

FAQs

Last updated on 22 Feb 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc