Code Monkey home page Code Monkey logo

use-text-analyzer's Introduction

use-text-analyzer

A lightweight React hook for analyzing text and providing various statistics such as estimated reading time, search frequency, word count, and more.

NPM Version npm bundle size GitHub top language GitHub License

Installation ๐Ÿ› ๏ธ

npm

npm install use-text-analyzer

yarn

yarn add use-text-analyzer

Features โœจ

  • Lightweight ๐Ÿชถ: 1KB in size, making it lightweight and efficient.
  • TypeScript Support ๐Ÿ‡น๐Ÿ‡ธ: Works seamlessly with both JavaScript and TypeScript projects.
  • Advanced Reading Time Estimation ๐Ÿ•’: Provides detailed reading time estimates in minutes and seconds, along with a human-readable format for immediate use in interfaces.
  • Efficient Algorithms โšก: Utilizes efficient algorithms for finding the most and least frequent words/characters.
  • CJK Support ๐Ÿ“œ: Enhanced support for texts in Chinese, Japanese, and Korean, ensuring accurate analysis across these languages.
  • SSR Compatible ๐ŸŒ: Fully compatible with server-side rendering, ensuring reliable performance across server and client environments.

Usage ๐Ÿ“

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;

API โš™๏ธ

useTextAnalyzer(options: TextAnalyzerOptions): TextAnalysisResult

A React hook that analyzes the given text and returns various statistics about it.

Parameters

  • 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.

Returns

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.

use-text-analyzer's People

Contributors

dependabot[bot] avatar invulner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.