Version: v0.0.0-...-4da7b14 Latest Latest Go to latest
Published: Aug 21, 2019 License: MIT Documentation ¶

Overview ¶

Package counter provides functions to generate a frequency histogram of values.

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type Counter ¶

type Counter map[string]int

Counter is used for calculating a frequency histogram of strings.

Example (All)

This example shows you can setup a counter and get all the values that have occured.

c := New()
for i := 0; i < 10; i++ {
c.Increment("foo")
}
for i := 0; i < 5; i++ {
c.Increment("bar")
}
values := c.All(true) // get all values
fmt.Println(values)
Output:

[bar foo]
Example (Bar)

This example shows you can setup a counter and find the value with the least number of occurences.

c := New()
for i := 0; i < 10; i++ {
c.Increment("foo")
}
for i := 0; i < 5; i++ {
c.Increment("bar")
}
values := c.Bottom(1, -1, true) // get bottom value, no maximum, and in sorted order
if len(values) > 0 {
fmt.Println(values)
}
Output:

bar
Example (Top)

This example shows you can setup a counter and find the value with the most number of occurences.

c := New()
for i := 0; i < 10; i++ {
c.Increment("foo")
}
for i := 0; i < 5; i++ {
c.Increment("bar")
}
values := c.Top(1, 0, true) // get top value, greater than zero, and in sorted order
if len(values) > 0 {
fmt.Println(values)
}
Output:

foo

func CountFiles ¶

func CountFiles(files []string, splitFunc bufio.SplitFunc, skipErrors bool) (Counter, error)

CountFiles generates a frequency distribution for the tokens found in the files files is a list of paths to files. splitFunc is a bufio.SplitFunc, which can be bufio.ScanBytes, bufio.ScanWords, bufio.ScanLines, or a custom function. If skipErrors is set to true, then errors opening or reading files are skipped and not returned.

func New ¶

func New() Counter

New returns a new Counter.

func (Counter) All ¶

func (c Counter) All(s bool) []string

All returns all the values as a slice of strings. If s is set to true, then the values are sorted in alphabetical order.

func (Counter) Bottom ¶

func (c Counter) Bottom(n int, max int, s bool) []string

Top returns at most "n" values that have occured at most "max" times as a slice of strings. If max is less than zero, then ignore "max" as a threshold. If max is set to zero, then the function will return an empty slice of strings. If s is set to true, then the values are sorted in ascending order before the "n" values are chosen.

func (Counter) Count ¶

func (c Counter) Count(value string) int

Count returns the current count for a given value. Returns 0 if the value has not occured.

func (Counter) Has ¶

func (c Counter) Has(value string) bool

Has returns true if the counter contains the value.

func (Counter) Increment ¶

func (c Counter) Increment(value string)

Increment increases the count for a given value by 1.

func (Counter) Len ¶

func (c Counter) Len() int

Len returns the current number of unique values.

func (Counter) Top ¶

func (c Counter) Top(n int, min int, s bool) []string

Top returns at most "n" values that have occured at least "min" times as a slice of strings. If s is set to true, then the values are sorted in descending order before the "n" values are chosen. If you would want to get the single most frequent value then use Top(1, 0, true). If you want 2 values that occured at least ten times, but do not care if they are the 2 most frequent values, then use Top(2, 10, false).