Miscellaneous
A lightweight React hook for analyzing text and providing various statistics such as estimated reading time, search frequency, word count, and more.
npm install use-text-analyzer
yarn add use-text-analyzer
import React from 'react';
import useTextAnalyzer from 'use-text-analyzer';
function TextAnalyzerExample() {
const text =
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';
const searchTerm = 'Lorem';
const {
wordCount,
charCount,
sentenceCount,
paragraphCount,
searchFrequency,
readingTime,
mostFrequentWord,
leastFrequentWord,
mostFrequentCharacter,
leastFrequentCharacter,
} = useTextAnalyzer({ text, searchTerm });
return (
<div>
<h2>Text Analysis Result</h2>
<p>
Estimated reading time: {readingTime.minutes}m {readingTime.seconds}s
</p>
<p>Total reading time in seconds: {readingTime.total}</p>
<p>Human-readable reading time: {readingTime.text}</p>
<p>
Search frequency of '{searchTerm}': {searchFrequency}
</p>
<p>Word count: {wordCount}</p>
<p>Character count: {charCount}</p>
<p>Sentence count: {sentenceCount}</p>
<p>Paragraph count: {paragraphCount}</p>
<p>Most frequent word: {mostFrequentWord}</p>
<p>Least frequent word: {leastFrequentWord}</p>
<p>Most frequent character: {mostFrequentCharacter}</p>
<p>Least frequent character: {leastFrequentCharacter}</p>
</div>
);
}
export default TextAnalyzerExample;
useTextAnalyzer(options: TextAnalyzerOptions): TextAnalysisResult
A React hook that analyzes the given text and returns various statistics about it.
options
(TextAnalyzerOptions
, required): An object containing options for text analysis.
text
(string, required): The text to analyze.searchTerm
(string, optional): The term to search for in the text.ignoreCase
(boolean, optional, default: true): Whether to ignore case when searching for the term and calculating word and character frequencies.trimText
(boolean, optional, default: true): Whether to trim the text before analysis.wordsPerMinute
(number, optional, default: 250 or 500 for CJK): Custom reading speed in words per minute, automatically adjusted for CJK text if not specified.A TextAnalysisResult
object containing various statistics about the text.
readingTime
(object):
minutes
(number): The total estimated reading time expressed in whole minutes.seconds
(number): Remaining seconds beyond the counted minutes.total
(number): The total estimated reading time expressed in total seconds.text
(string): A human-readable summary of the reading time.searchFrequency
(number): The frequency of the search term in the text.wordCount
(number): The number of words in the text.charCount
(number): The number of characters in the text.sentenceCount
(number): The number of sentences in the text.paragraphCount
(number): The number of paragraphs in the text.mostFrequentWord
(string): The most frequent word in the text.leastFrequentWord
(string): The least frequent word in the text.mostFrequentCharacter
(string): The most frequent character in the text.leastFrequentCharacter
(string): The least frequent character in the text.