-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathestimator.go
More file actions
79 lines (68 loc) · 2.57 KB
/
estimator.go
File metadata and controls
79 lines (68 loc) · 2.57 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package reading_time
import (
"math"
"time"
)
// Estimator holds the parameters to calculate the reading time.
type Estimator struct {
WordsPerMinute int
SecondsPerLine int
BaseImageSeconds int
ImageSecondsDecay int
ImageThreshold int
}
// NewEstimator creates a new Estimator object with custom parameters.
func NewEstimator(wordsPerMinute, secondsPerLine, baseImageSeconds, imageSecondsDecay, imageThreshold int) *Estimator {
return &Estimator{
WordsPerMinute: wordsPerMinute,
SecondsPerLine: secondsPerLine,
BaseImageSeconds: baseImageSeconds,
ImageSecondsDecay: imageSecondsDecay,
ImageThreshold: imageThreshold,
}
}
// StandardEstimator is a default estimator with common parameters for average reading speed.
var StandardEstimator = NewEstimator(200, 2, 12, 1, 10)
// FastEstimator for faster reading.
var FastEstimator = NewEstimator(250, 1, 10, 1, 8)
// SlowEstimator for slower reading.
var SlowEstimator = NewEstimator(150, 3, 15, 2, 12)
// CalculateReadingTime calculates the total reading time.
func (e *Estimator) CalculateReadingTime(totalWords, totalImages, totalLinesOfCode int) time.Duration {
return e.CalculateWordsTime(totalWords) +
e.CalculateImagesTime(totalImages) +
e.CalculateCodeTime(totalLinesOfCode)
}
// CalculateWordsTime calculates reading time based on word count.
func (e *Estimator) CalculateWordsTime(totalWords int) time.Duration {
if e.WordsPerMinute <= 0 {
return 0
}
wordsPerSecond := 60.0 / float64(e.WordsPerMinute)
return roundDurationToNearestSecond(float64(totalWords) * wordsPerSecond)
}
// CalculateImagesTime calculates additional time based on the number of images.
func (e *Estimator) CalculateImagesTime(totalImages int) time.Duration {
var adjustmentTime float64
for i := 0; i < totalImages; i++ {
decayedTime := float64(e.BaseImageSeconds) - float64(i)*float64(e.ImageSecondsDecay)
if i >= e.ImageThreshold {
decayedTime = float64(e.BaseImageSeconds) - float64(e.ImageThreshold-1)*float64(e.ImageSecondsDecay)
}
if decayedTime > 0 {
adjustmentTime += decayedTime
}
}
return roundDurationToNearestSecond(adjustmentTime)
}
// CalculateCodeTime calculates additional time based on lines of code.
func (e *Estimator) CalculateCodeTime(totalLinesOfCode int) time.Duration {
return roundDurationToNearestSecond(float64(totalLinesOfCode) * float64(e.SecondsPerLine))
}
// roundDurationToNearestSecond rounds the duration to the nearest second.
func roundDurationToNearestSecond(seconds float64) time.Duration {
if seconds < 0 {
return 0
}
return time.Duration(math.Round(seconds)) * time.Second
}