🔢Frequency Functions

A pair of functions to efficiently calculate and retrieve the frequency of words and characters in a given text, providing useful insights for text analysis and processing tasks.

Table of Contents

wordFrequency

Calculates the frequency of words in a given input string and returns the top N most frequent words.

Parameters

  • inputString (String): The input text for calculating word frequencies.

  • limit (Number, optional): The maximum number of top frequent words to return. Defaults to 100.

Returns

  • Map: A sorted map containing the top N most frequent words with their corresponding frequencies.

Example

javascriptCopy codeconst text = "This is a sample text. This text is just a sample.";
const wordFreq = wordFrequency(text, 3);
console.log(wordFreq); // Output: Map { 'This' => 2, 'is' => 2, 'a' => 2 }

charFrequency

Calculates the frequency of characters in a given input string and returns them sorted by frequency.

Parameters

  • inputString (String): The input text for calculating character frequencies.

Returns

  • Map: A sorted map containing characters with their corresponding frequencies.

Example

javascriptCopy codeconst text = "This is a sample text.";
const charFreq = charFrequency(text);
console.log(charFreq); // Output: Map { ' ' => 5, 's' => 4, 't' => 3, 'i' => 2, ... }

Last updated