word-frequency-counter
Version:
A word frequency counter
37 lines (36 loc) • 1.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.allWordCount = exports.singleWordCount = void 0;
const config_1 = require("../../config");
const sentenceComponents_1 = require("../../sentenceComponents/sentenceComponents");
const punctuation_1 = require("../../utils/punctuation/punctuation");
/**
* Count instances of a given word in a block of text.
*
* @param {string} text - The text to parse.
* @param {string} word - The word to count.
* @param {Config} config - Configuration for the word counter
*/
const singleWordCount = (text, word, config = config_1.defaultConfig) => {
const punctuationRemovedSearch = (0, punctuation_1.punctuationRemover)(word, config);
const sentenceComponents = (0, sentenceComponents_1.getSentenceComponents)(text, config);
const totalOccurrences = sentenceComponents.filter((word) => word === punctuationRemovedSearch);
return totalOccurrences.length;
};
exports.singleWordCount = singleWordCount;
/**
* Count instances of all words in a block of text.
*
* @param {string} text - The text to parse.
* @param {Config} config - Configuration for the word counter
*/
const allWordCount = (text, config = config_1.defaultConfig) => {
const uniqueSentenceComponents = (0, sentenceComponents_1.getUniqueSentenceComponents)(text, config);
const wordMap = new Map();
uniqueSentenceComponents.forEach((word) => {
const wordCount = (0, exports.singleWordCount)(text, word, config);
wordMap.set(word, wordCount);
});
return wordMap;
};
exports.allWordCount = allWordCount;