40 lines
741 B
Go
40 lines
741 B
Go
|
|
package reader
|
||
|
|
|
||
|
|
import "time"
|
||
|
|
|
||
|
|
const (
|
||
|
|
MinWPM = 100
|
||
|
|
MaxWPM = 1000
|
||
|
|
)
|
||
|
|
|
||
|
|
// WordDuration calculates how long a word should be displayed
|
||
|
|
// at the specified reading speed.
|
||
|
|
func WordDuration(word string, wpm int) time.Duration {
|
||
|
|
// Enforce supported reading speed limits.
|
||
|
|
if wpm < MinWPM {
|
||
|
|
wpm = MinWPM
|
||
|
|
}
|
||
|
|
|
||
|
|
if wpm > MaxWPM {
|
||
|
|
wpm = MaxWPM
|
||
|
|
}
|
||
|
|
|
||
|
|
// Base duration for one word.
|
||
|
|
duration := time.Minute / time.Duration(wpm)
|
||
|
|
|
||
|
|
// Add pauses for punctuation.
|
||
|
|
switch {
|
||
|
|
case IsSentenceEnding(word):
|
||
|
|
duration += 300 * time.Millisecond
|
||
|
|
case IsPausePunctuation(word):
|
||
|
|
duration += 150 * time.Millisecond
|
||
|
|
}
|
||
|
|
|
||
|
|
// Allow slightly more time for longer words.
|
||
|
|
if RuneLength(word) >= 10 {
|
||
|
|
duration += 50 * time.Millisecond
|
||
|
|
}
|
||
|
|
|
||
|
|
return duration
|
||
|
|
}
|