A "Reading TIme" Estimation Package For Laravel

Published: 9 months ago - Updated: 8 months ago

3 minutes - 266 Words

article 'A "Reading TIme" Estimation Package For Laravel' banner

Summary

Estimate reading time of text content with Bookworm for Laravel. Better user experience for readers. Easy to install and use.

Bookworm for Laravel: A Time Estimation Package

Bookworm for Laravel is a time estimation package that can be used to estimate how much time is needed to read a certain piece of text. It can be installed in Laravel projects and used to calculate the estimated reading time of articles, blog posts, or any other text content.

Benefits

Bookworm for Laravel is a useful package for any Laravel developer who needs to estimate the reading time of text content. This package can be used in a variety of scenarios, such as:

  • Estimating the reading time of blog posts or articles
  • Take the reading time of code blocks and Images into consideration
  • Helping users decide whether to read a piece of content based on its estimated reading time

By using Bookworm for Laravel, you can provide a better user experience for your readers by giving them an idea of how long it will take to read your content. This can help them plan their time more effectively and make informed decisions about what content to read.

Installation

To install Bookworm for Laravel, run the following command in your Laravel project:

composer require shtayeb/bookworm

After installing the package, you need to publish the config file by running the following command:

php artisan vendor:publish --provider="SHTayeb\\Bookworm\\BookwormServiceProvider"

This will create a bookworm.php file in your config directory.

Usage

To use Bookworm for Laravel, you need to create an instance of the Bookworm class and call the estimate method with the text you want to estimate the reading time for.

<?php
use SHTayeb\\Bookworm\\Bookworm;

$text = '...';

$time = (new Bookworm())->estimate($text);

echo $time; // 5 minutes

You can also count the number of words in a text using the countWords method:

$word_count = (new Bookworm())->countWords($text);

echo $word_count; // 1,000

Configuration

The package comes with default configuration values that can be overridden by publishing the config file. You can change these values in the config/bookworm.php file.

return [
	'words_per_minute' => 200,
	'codewords_per_minute' => 200,
	'seconds_per_image' => 12,
];
  • The words_per_minute value is the average number of words a person can read in one minute.
  • The codewords_per_minute value is the average number of words in a code section a person can read in one minute.
  • The seconds_per_image value is the average number of seconds a person looks at an image.

Real-life Example

Suppose you run a blog and want to display the estimated reading time of your blog posts. You can use this package to calculate the estimated reading time and display it on your blog post page.

In a Controller

<?php
use SHTayeb\\Bookworm\\Bookworm;

// get blog post content from database
$post = Post::find(1);

// calculate estimated reading time
$reading_time = (new Bookworm())->estimate($post->content);

And display the result in your blade views

// display estimated reading time on blog post page
<div class="reading-time">{{ $reading_time }} min read</div>

Using Accessors

Accessors are methods that allow you to access and modify the values of an object’s properties.
In this case, create an accessor for content property of your post and return the read time and word count for that post.

public function getReadTimeAttribute():array
{
    return [
         'word_count' => (new Bookworm())->countWords($this->content),
         'read_time' => (new Bookworm())->estimate($this->content),
    ];
       
}

Warning

Be carefull of performance while using it with Accessors, since it will be calculated for each post and If you get a list of 100 post, It has to be calculted to all of them.

Conclusion

Bookworm for Laravel is a useful package for any Laravel developer who needs to estimate the reading time of text content. By using this package, you can provide a better user experience for your readers and help them make informed decisions about what content to read.

Add Comment

Conversations (0)

Sign up for our newsletter

Stay up to date with the latest news and articles.