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 🙏

© 2025 – Pkg Stats / Ryan Hefner

relatinator

v2.1.0

Published

A humble library for finding related posts and content. Uses tf-idf under the hood. Primarily aimed at static site generators.

Downloads

142

Readme

Relatinator

A TypeScript library for finding related documents using TF-IDF and BM25 algorithms.

Installation

npm install relatinator
# or
pnpm add relatinator
# or
yarn add relatinator

Features

  • Two similarity algorithms: TF-IDF and BM25
  • Find related documents based on content similarity
  • Get top terms for a document
  • Find documents related to a specific term
  • Written in TypeScript with full type support

Usage

import { BM25Utils, TfIdfUtils } from "relatinator";

// Example using BM25Utils. TfIdfUtils works similarly.

// Ensure the instance is initialized (happens automatically on first use)
await BM25Utils.getInstance();

// Train the model with your documents
const documents = [
  { id: "doc1", content: "This is the first document about cats." },
  { id: "doc2", content: "Another document about dogs." },
  { id: "doc3", content: "A document about both cats and dogs." },
];

await BM25Utils.train(documents);

// Find related documents
const related = await BM25Utils.findRelated("A document about cats", "doc1", 2);
console.log(related); // ['doc3', 'doc2']

// Get top terms for a document
const terms = await BM25Utils.getTopTermsForId("doc1");
console.log(terms); // [{ term: 'cats', score: 0.8 }, ...]

// Find documents related to a term
const relatedDocs = await BM25Utils.getTopRelatedDocumentsForTerm("cats");
console.log(relatedDocs); // ['doc1', 'doc3']

API

The library exports two main utility objects: BM25Utils and TfIdfUtils.

BM25Utils / TfIdfUtils

These objects contain the methods for interacting with the respective algorithms.

getInstance(): Promise<BM25VectorizerType | TfIdf>

Returns a promise that resolves with the singleton instance of the vectorizer/model. Ensures the underlying NLP model is initialized. Usually not needed for direct use, as other methods call it internally.

resetInstance(): Promise<BM25VectorizerType | TfIdf>

Resets the singleton instance, clearing all learned data and the internal document map. Returns the new, empty instance. Necessary if you need to retrain from scratch without restarting the application.

train(documents: RelatinatorDocument[], debug?: boolean): Promise<void>

Trains the model with the provided documents. RelatinatorDocument is { id: string, content: string }.

findRelated(documentToCompare: string, id: string, topN?: number, debug?: boolean): Promise<string[]>

Finds document IDs related to the input documentToCompare string. Excludes the document with the provided id from the results. Returns an array of related document IDs, sorted by relevance.

getTopTermsForId(id: string, topN?: number, debug?: boolean): Promise<{ term: string, score: number }[]>

Gets the top terms (and their scores) for a document specified by its id.

getTopRelatedDocumentsForTerm(term: string, topN?: number, debug?: boolean): Promise<string[]>

Finds document IDs related to a specific term. Returns an array of related document IDs, sorted by relevance.

Development

# Install dependencies
pnpm install

# Run tests
pnpm test

License

MIT