relatedposts
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

Related Posts

A utility function to find related content based on title, category, and tags using string similarity and stopword removal.

Features

  • Calculate similarity scores between texts
  • Filter and find related content based on title, category, and tags
  • Use customizable weights for title, category, and tags
  • Remove stopwords from input for more accurate similarity comparisons

Installation

Install the package via npm:

npm install relatedposts
import getRelated from 'relatedposts';

Function

The getRelated function evaluates and returns a list of Comparison objects that are most similar to a given Input object. Similarity is determined based on the title, category, and tags, with scores being weighted according to the provided Options. The results are sorted by their similarity scores in descending order.

Parameters

  1. input (Input):

    • An object representing the input data to be compared against.
    • Fields:
      • title (string): The title of the input.
      • category (string): The category of the input.
      • tags (string[]): An array of tags associated with the input.

    Example:

    const input = {
      title: "The quick brown dog",
      category: "Animals",
      tags: ["quick", "brown", "dog"],
    };
  2. comparisons (Comparison):

    • An array of objects representing the items to be compared against the input.
    • Fields:
      • title (string): The title of the input.
      • category (string): The category of the input.
      • tags (string[]): An array of tags associated with the input.

    Example:

    const comparisons = [
      {
        title: "The quick brown fox",
        category: "Animals",
        tags: ["quick", "brown", "fox"],
      },
      {
        title: "The lazy dog",
        category: "Animals",
        tags: ["lazy", "dog"],
      },
      {
        title: "The quick brown cat",
        category: "Animals",
        tags: ["quick", "brown", "cat"],
      },
    ];
  3. numResults (number, optional):

  • The number of results to return. Defaults to 5.
  1. options (Options, optional):
  • An object containing weights to influence the similarity score calculations.
    • Fields:
    • weights (Weights, optional): Weight for the title similarity score. Defaults to 1.
    • title (number, optional): Weight for the category similarity score. Defaults to 1.
    • tags (number, optional): Weight for the tags similarity score. Defaults to 1.

Returns

  • A Promise that resolves to an array of ScoredComparison objects.
  • Fields:
  • All fields of the Comparison object.
  • score (number): The computed similarity score based on the provided weights and the comparison between the input and each comparison.
const results = [
  {
    title: "The quick brown fox",
    category: "Animals",
    tags: ["quick", "brown", "fox"],
    score: 0.63,
  },
  {
    title: "The lazy dog",
    category: "Animals",
    tags: ["lazy", "dog"],
    score: 0.43,
  },
  {
    title: "The quick brown cat",
    category: "Animals",
    tags: ["quick", "brown", "cat"],
    score: 0.32,
  },
];

Usage

const results = await getRelated(input, comparisons, 5);
console.log(results);

Package Sidebar

Install

npm i relatedposts

Weekly Downloads

3

Version

1.1.0

License

MIT

Unpacked Size

18.4 kB

Total Files

15

Last publish

Collaborators

  • joeyk2k