Skip to content

Generate all possible meaningful combinations of consecutive words

Notifications You must be signed in to change notification settings

apto-space/moving-ngrams

Repository files navigation

moving-ngrams

A utility package for generating all possible n-gram combinations from a sequence of words, useful for search engine query analysis and natural language processing tasks.

Description

moving-ngrams helps you analyze text by generating all possible meaningful combinations of consecutive words. This is particularly useful for:

  • Search engine query analysis
  • Automatic keyphrase extraction
  • Faceted search implementation
  • Natural language understanding

Installation

# Using bun
bun add moving-ngrams

# Using npm
npm install moving-ngrams

# Using yarn
yarn add moving-ngrams

Usage

import { movingNgrams } from 'moving-ngrams';

// Example 1: Simple phrase analysis
const result = movingNgrams(['hello', 'world']);
console.log(result);
/* Output:
[
  [['hello'], ['world']],      // Individual words
  [['hello', 'world']]         // Combined phrase
]
*/

// Example 2: Search query analysis
const searchQuery = movingNgrams(['elon', 'musk', 'san', 'francisco']);
console.log(searchQuery);
/* Output will include all possible combinations like:
- [['elon'], ['musk'], ['san'], ['francisco']]
- [['elon', 'musk'], ['san'], ['francisco']]
- [['elon'], ['musk', 'san'], ['francisco']]
- [['elon'], ['musk'], ['san', 'francisco']]
... and more combinations
*/

Use Cases

1. Search Query Analysis

When users enter a search query like "Elon Musk San Francisco", the package helps you analyze all possible meaningful combinations:

  • Individual terms: "Elon", "Musk", "San", "Francisco"
  • Compound terms: "Elon Musk", "San Francisco"
  • Mixed combinations: ["Elon Musk", "San Francisco"], ["Elon", "Musk San Francisco"], etc.

This is particularly useful for:

  • Improving search relevance
  • Understanding user intent
  • Supporting multi-term matching

2. Keyphrase Extraction

When analyzing text content, you can use this to:

  • Extract potential keyphrases
  • Identify compound terms
  • Generate search indices

API

function movingNgrams(words: string[]): string[][][]
  • Input: Array of words
  • Output: Array of all possible combinations where each combination is an array of word groups

License

MIT


This project was created using Bun v1.1.41.

About

Generate all possible meaningful combinations of consecutive words

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published