go-counter: github.com/spatialcurrent/go-counter/pkg/counter Index | Examples | Files

package counter

import "github.com/spatialcurrent/go-counter/pkg/counter"

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

Index

Examples

Package Files

CountFiles.go Counter.go

type Counter Uses

type Counter map[string]int

Counter is used for calculating a frequency histogram of strings.

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

Code:

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]

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

Code:

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[0])
}

Output:

bar

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

Code:

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[0])
}

Output:

foo

func CountFiles Uses

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 Uses

func New() Counter

New returns a new Counter.

func (Counter) All Uses

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 Uses

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 Uses

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 Uses

func (c Counter) Has(value string) bool

Has returns true if the counter contains the value.

func (Counter) Increment Uses

func (c Counter) Increment(value string)

Increment increases the count for a given value by 1.

func (Counter) Len Uses

func (c Counter) Len() int

Len returns the current number of unique values.

func (Counter) Top Uses

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).

Package counter imports 4 packages (graph) and is imported by 2 packages. Updated 2019-08-21. Refresh now. Tools for package owners.