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.
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
# Using bun
bun add moving-ngrams
# Using npm
npm install moving-ngrams
# Using yarn
yarn add moving-ngrams
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
*/
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
When analyzing text content, you can use this to:
- Extract potential keyphrases
- Identify compound terms
- Generate search indices
function movingNgrams(words: string[]): string[][][]
- Input: Array of words
- Output: Array of all possible combinations where each combination is an array of word groups
MIT
This project was created using Bun v1.1.41.