counter

package
v0.0.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 20, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package counter implements various counter types, analagous to Python's collections.Counter

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pair

type Pair struct {
	Key   string
	Count int
}

Pair represents a Key, Count pair

func (Pair) KeyVal

func (p Pair) KeyVal() (string, int)

type Pairs

type Pairs []Pair

Pairs is a slice of Pairs. It is sortable lexigraphically by count, then key.

func (Pairs) Counts

func (pairs Pairs) Counts() []int

func (Pairs) Keys

func (pairs Pairs) Keys() []string

func (Pairs) Len

func (pairs Pairs) Len() int

func (Pairs) Less

func (pairs Pairs) Less(i, j int) bool

func (Pairs) Swap

func (pairs Pairs) Swap(i, j int)

type String

type String map[string]int

String counts the occurence of strings.

func FromStrings

func FromStrings(strings ...string) String

FromStrings creates a counter of strings from a slice, counting each member once.

func Max

func Max(counters ...String) String

Max returns a counter maxC where maxC[x] = max(c[x] for c in counters)

func Min

func Min(counters ...String) String

Min returns a counter minC where minC[x] = min(max(c[x], 0) for c in counters)

func (String) Add

func (counter String) Add(items ...string)

Add the the following strings to the counter.

func (String) Combine

func (counter String) Combine(counters ...String) String

Combine one or more counters, where the count of each element is the sum of the count of each

func (String) Copy

func (counter String) Copy() String

Copy the counter

func (String) Count

func (counter String) Count(k string) int

Count returns counter[string] if it exists, or 0 otherwise.

func (String) Equal

func (counter String) Equal(other String) bool

Equal returns true if two counters are equal. Two counters are equal if they have the same length and share all keys and values.

func (String) Keys

func (counter String) Keys() []string

Keys returns the keys of the counter. Order is not guaranteed.

func (String) MostCommon

func (counter String) MostCommon() (string, int)

func (String) MostCommonN

func (counter String) MostCommonN(n int) (pairs Pairs, err error)

MostCommonN returns the N most common keys. In the case of a tie, it prioritizes the lexigraphically lowest, so that the keys contained in counter.MostCommonN(n) will be identical for two equivalent counters.

func (String) Neg

func (counter String) Neg() String

Neg returns a copy of the counter with inverted counts: i.e, {k: -v for k, v in counter}

func (String) NonZeroElements

func (counter String) NonZeroElements() String

NonZeroElements returns a copy of the counter with all dummy elements(count zero) removed.

func (String) Pairs

func (counter String) Pairs() Pairs

Pairs returns a slice containing each key-value pair

func (String) PositiveElements

func (counter String) PositiveElements() String

PositiveElements returns a copy of counter containing all the elements where the counter[x] > 0

func (String) Sorted

func (counter String) Sorted() Pairs

Sorted returns the keys and coutns of the strings in the counter. They are sorted by the count, and then by the key. i.e, given {"c": 1, "b": 3, "a":3}, returns {1, 1, 3}, {c, a, b}

func (String) String

func (counter String) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL