How to calculate reading time

This entry was posted on

  • snippet
  • tutorial

In this snippet, I will be showing how to calculate the reading time for a given string.

First, we will create a new data structure to hold the reading time. It contains the number of words and the minutes and seconds to read the post.

public record ReadingTimeData
{
    public ReadingTimeData(int minutes, int seconds, int words)
    {
        Minutes = minutes;
        Seconds = seconds;
        Words = words;
    }

    public int Minutes { get; }

    public int Seconds { get; }

    public int Words { get; }

    public int RoundedMinutes
        => Seconds < 30
            ? Minutes
            : Minutes + 1;
}
  • RoundedMinutes is the rounded time to make things simpler for a reader. Anything less than 30 seconds gets ignore, and anything more than 30 seconds gets rounded up to the next minute.

Now, we can calculate the reading time and fill in the ReadingTimeData.

private ReadingTimeData GetReadingTime(string content, int wordsPerMinute)
{
    const Regex SpacesRegex = new Regex(@"\S+", RegexOptions.Multiline);
    var words = SpacesRegex.Matches(content).Count;

    var minutes = words / WordsPerMinute;
    var remainingWords = words % WordsPerMinute;
    var seconds = remainingWords * 60 / WordsPerMinute;

    return new ReadingTimeData(minutes, seconds, words);
}
  • Line 3-4: Get all the words in the string by using \S as our regex expression, which matches all characters that are not a white space.
  • Line 6: Get the number of minutes it takes to read the string based on how many words we can read in a minute.
  • Line 7: Get the number of unread words which we weren’t able to read in a full minute.
  • Line 8: Get the number of seconds to read the remaining text.

Concusion

Now, we can show the estimated reading on our articles.

More like this

Ankur Sheel © 2021
Connect with me
GithubTwitterLinkedIn