Documentation ¶
Overview ¶
Package words provides a means for counting the numbers of words and estimating the reading time of the given content.
Words per minute values based on:
https://irisreading.com/what-is-the-average-reading-speed/ https://www.researchgate.net/publication/332380784_How_many_words_do_we_read_per_minute_A_review_and_meta-analysis_of_reading_rate https://www.sciencedirect.com/science/article/abs/pii/S0749596X19300786
Package words was inspired by:
https://github.com/byn9826/words-count/blob/master/src/globalWordsCount.js
Index ¶
- Constants
- Variables
- func Count(input string) (count int)
- func List(input string) (list []string)
- func Parse(input string) (words []string)
- func Range(input string, fn func(word string))
- func Search(query, content string) (score int, present []string)
- type ReadingMetrics
- type Words
- func (w *Words) Count(input string) (count int)
- func (w *Words) List(input string) (list []string)
- func (w *Words) Metrics(content string) (m ReadingMetrics)
- func (w *Words) Parse(input string) (words []string)
- func (w *Words) Range(input string, fn func(word string))
- func (w *Words) Search(query, content string) (score int, found []string)
Constants ¶
const ( // AverageWordsPerMinute is the words per minute read by average adults AverageWordsPerMinute = 238.0 // RelaxedWordsPerMinute is an estimation of words per minute read by // older children and tired adults looking at monitors and screens all day RelaxedWordsPerMinute = 177.0 )
Variables ¶
var ( // DefaultPunctuation is a hard-coded list of the most common characters // that are not counted as words DefaultPunctuation = []rune{ ',', ',', '.', '。', ':', ':', ';', ';', '[', ']', '【', ']', '】', '{', '{', '}', '}', '(', '(', ')', ')', '<', '《', '>', '》', '$', '¥', '!', '!', '?', '?', '~', '~', '「', '」', '\'', '’', '"', '“', '”', '*', '/', '\\', '&', '%', '@', '#', '^', '、', '、', '、', '、', } )
Functions ¶
func List ¶
List returns a list of words that were separated by spaces using the Default Words configuration
Types ¶
type ReadingMetrics ¶
type ReadingMetrics struct { WordCount int Average struct { Minutes int Duration time.Duration } Relaxed struct { Minutes int Duration time.Duration } }
ReadingMetrics is a data structure returned by the Words.Metrics method
func Metrics ¶
func Metrics(content string) (m ReadingMetrics)
Metrics parses the contents and returns some interesting ReadingMetrics using the Default Words configuration
type Words ¶
type Words struct { // PunctuationAsBreaker specifies that punctuation characters should not // be removed and instead be replaced with a space. For example, "they're" // by default is collapsed to "theyre" for counting purposes. With // PunctuationAsBreaker set to true, "they're" would become "they re" PunctuationAsBreaker bool // DisableDefaultPunctuation specifies that only the Words.Punctuation // runes are to be considered punctuation DisableDefaultPunctuation bool // Punctuation defines the list of punctuation runes to use when parsing // words out of content Punctuation []rune // AverageWPM specifies the average words per minute to use // for calculating Metrics, default is 238.0, see: AverageWordsPerMinute AverageWPM float64 // RelaxedWPM specifies the average words per minute to use // for calculating Metrics, default is 177.0, see: RelaxedWordsPerMinute RelaxedWPM float64 // contains filtered or unexported fields }
Words is the definition for running customized word operations and is the implementation driving the normal package functions
func Default ¶
func Default() (w *Words)
Default returns a new Words instance configured with sane defaults
func (*Words) List ¶
List returns a list of all the words detected within the given input that are separated by spaces, word characters not separated by spaces are clumped within individual items of the list returned. Use Words.Parse to derive a more accurate word list
func (*Words) Metrics ¶
func (w *Words) Metrics(content string) (m ReadingMetrics)
Metrics gets the Words.Count and derives some estimated reading times
func (*Words) Range ¶
Range iterates over all words detected within input, calling the given `fn` for each word found