aboutsummaryrefslogtreecommitdiff
path: root/internal/reader/readingtime/readingtime.go
blob: faf7847110c008935b27e4fa9f81fc39c6010e89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

// Package readtime provides a function to estimate the reading time of an article.
package readingtime

import (
	"math"
	"strings"
	"unicode/utf8"

	"miniflux.app/v2/internal/reader/sanitizer"

	"github.com/abadojack/whatlanggo"
)

// EstimateReadingTime returns the estimated reading time of an article in minute.
func EstimateReadingTime(content string, defaultReadingSpeed, cjkReadingSpeed int) int {
	sanitizedContent := sanitizer.StripTags(content)
	langInfo := whatlanggo.Detect(sanitizedContent)

	var timeToReadInt int
	if langInfo.IsReliable() && (langInfo.Lang == whatlanggo.Jpn || langInfo.Lang == whatlanggo.Cmn || langInfo.Lang == whatlanggo.Kor) {
		timeToReadInt = int(math.Ceil(float64(utf8.RuneCountInString(sanitizedContent)) / float64(cjkReadingSpeed)))
	} else {
		nbOfWords := len(strings.Fields(sanitizedContent))
		timeToReadInt = int(math.Ceil(float64(nbOfWords) / float64(defaultReadingSpeed)))
	}

	return timeToReadInt
}