Last modified: Feb 28, 2023 By Alexander Williams

Estimating Reading Time of Text and Text File using Python

Reading Time is the estimated time it takes to read a piece of text. In this tutorial we'll see how get reading time for a text and a text file using python.

Get reading time using Math

Let's assume that human takes 1 min to read 200 words. However, To get reading time using math we:

  1. Split the text by word
  2. Count the words
  3. divid the word count by the assumed average reading speed of 200

Here is an example:

import math


text = '''
In Python, finding a specific word or element within a list is a familiar task developers often search for. 
This tutorial will teach us four effective methods to find a word in a list.
As you can see, the program finds the word python in the my_list and returns The word 'python' is present in the list.

By using the index() method, we can find a word in a list. 
However, this method returns the index of the first occurrence of the specified word in the list.

In our situation, we will use the item parameter. In the following example, we'll find the word python.
As we can see, the program found the word python in my_list and returned the index of the word. As we know, every list starts with an index of 0

The index() method is more powerful than the in keyword because it provides the index of the word in the list.
If the word is not present will raise a ValueError, as in the following example.
'''

word_count = len(text.split()) # Split and Count

reading_time = math.ceil(word_count / 200) # average reading speed of 200 words per minute

print(f"Estimated reading time: {reading_time} minutes")
    

Output:

Estimated reading time: 1 minutes

As you can see, the text tasks 1 minute to read it.

The bellow example will show us how to get reading time of a text file.

import math

with open(file_path, 'r') as file:
    contents = file.read()
    word_count = len(contents.split())
    reading_time = math.ceil(word_count / 200)
    print(f"Estimated reading time: {reading_time} minutes")

Get reading time using readtime

readtime is a library that calculates the time that it takes human to read a text. To install it using pip run the following command:

pip install readtime

Now let's see how to use readtime.

import readtime

text = '''
In Python, finding a specific word or element within a list is a familiar task developers often search for. 
This tutorial will teach us four effective methods to find a word in a list.
As you can see, the program finds the word python in the my_list and returns The word 'python' is present in the list.

By using the index() method, we can find a word in a list. 
However, this method returns the index of the first occurrence of the specified word in the list.

In our situation, we will use the item parameter. In the following example, we'll find the word python.
As we can see, the program found the word python in my_list and returned the index of the word. As we know, every list starts with an index of 0

The index() method is more powerful than the in keyword because it provides the index of the word in the list.
If the word is not present will raise a ValueError, as in the following example.
'''

result = readtime.of_text(text)

print(result)

Output:

1 min read

To get the time in seconds:

print(result.seconds)

Output:

40

Counclustion

In conclusion, it is pretty easy to get the read time of any text using python. As you can see in this article, we've learned 2 ways to do that. You can choose whatever you want, the math way or the readtime library.

However, This tool can be particularly helpful for bloggers, journalists, or anyone else who wants to engage their readers and provide them the read time of the article.