Package edu.berkeley.nlp.lm.collections
Class Counter<E>
java.lang.Object
edu.berkeley.nlp.lm.collections.Counter<E>
- All Implemented Interfaces:
Serializable
A map from objects to doubles. Includes convenience methods for getting,
setting, and incrementing element counts. Objects not in the counter will
return a count of zero. The counter is backed by a HashMap .(unless specified
otherwise with the MapFactory constructor).
- Author:
- lots of people
- See Also:
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <L> Counter<L> booleanapproxEquals(Counter<E> other, double tol) argMax()Finds the key with maximum count.voidclear()booleancontainsKey(E key) Returns whether the counter contains the given key.difference(Counter<E> counter) doubledotProduct(Counter<E> other) entrySet()doubleGet the count of the element, or zero if the element is not in the counter.doubledoublegetProbability(E key) I know, I know, this should be wrapped in a Distribution class, but it's such a common use...why not.<T extends E>
voidincrementAll(Counter<T> counter) <T extends E>
voidincrementAll(Counter<T> counter, double scale) voidincrementAll(Collection<? extends E> collection, double count) Increment each element in a given collection by a given amount.doubleincrementCount(E key, double increment) Increment a key's count by the given amount.booleanisEmpty()True if there are no entries in the counter (false does not mean totalCount > 0)booleankeySet()The elements in the counter.static voiddoublemax()doublemin()voidDestructively normalize this Counter in place.voidvoidpruneExcept(Set<E> toKeep) voidpruneKeysBelowThreshold(double cutoff) voidSet the count for the given key if it is larger than the previous one;voidputAll(double d) voidprotected voidremoveKeyFromEntries(E key) sample()Will return a sample from the counter, will throw exception if any of the counts are invalid input: '<' 0.0 or if the totalCount() invalid input: '<'= 0.0Will return a sample from the counter, will throw exception if any of the counts are invalid input: '<' 0.0 or if the totalCount() invalid input: '<'= 0.0voidscale(double c) scaledClone(double c) voidsetAllCounts(double val) Sets all counts to the given value, but does not remove any keysvoidSet the count for the given key, clobbering any previous count.voidsetDefaultCount(double deflt) voidsetDirty(boolean dirty) voidsetMaxCount(E key, double val) Set's the key's count to the maximum of the current count and val.voidsetMinCount(E key, double val) Set's the key's count to the minimum of the current count and val.intsize()The number of entries in the counter (not the total count -- use totalCount() instead).toString()Returns a string representation with the keys ordered by decreasing counts.doubleFinds the total of all counts in the counter.values()
-
Constructor Details
-
Counter
public Counter() -
Counter
-
Counter
-
-
Method Details
-
getDefaultCount
public double getDefaultCount() -
setDefaultCount
public void setDefaultCount(double deflt) -
keySet
The elements in the counter.- Returns:
- set of keys
-
entrySet
-
size
public int size()The number of entries in the counter (not the total count -- use totalCount() instead). -
isEmpty
public boolean isEmpty()True if there are no entries in the counter (false does not mean totalCount > 0) -
containsKey
Returns whether the counter contains the given key. Note that this is the way to distinguish keys which are in the counter with count zero, and those which are not in the counter (and will therefore return count zero from getCount().- Parameters:
key-- Returns:
- whether the counter contains the key
-
getCount
Get the count of the element, or zero if the element is not in the counter.- Parameters:
key-- Returns:
-
getProbability
I know, I know, this should be wrapped in a Distribution class, but it's such a common use...why not. Returns the MLE prob. Assumes all the counts are >= 0.0 and totalCount > 0.0. If the latter is false, return 0.0 (i.e. 0/0 == 0)- Parameters:
key-- Returns:
- MLE prob of the key
-
normalize
public void normalize()Destructively normalize this Counter in place. -
setCount
Set the count for the given key, clobbering any previous count.- Parameters:
key-count-
-
put
Set the count for the given key if it is larger than the previous one;- Parameters:
key-count-
-
sample
Will return a sample from the counter, will throw exception if any of the counts are invalid input: '<' 0.0 or if the totalCount() invalid input: '<'= 0.0- Returns:
-
sample
Will return a sample from the counter, will throw exception if any of the counts are invalid input: '<' 0.0 or if the totalCount() invalid input: '<'= 0.0- Returns:
-
removeKey
-
removeKeyFromEntries
- Parameters:
key-
-
setMaxCount
Set's the key's count to the maximum of the current count and val. Always sets to val if key is not yet present.- Parameters:
key-val-
-
setMinCount
Set's the key's count to the minimum of the current count and val. Always sets to val if key is not yet present.- Parameters:
key-val-
-
incrementCount
Increment a key's count by the given amount.- Parameters:
key-increment-
-
incrementAll
Increment each element in a given collection by a given amount. -
incrementAll
-
incrementAll
-
totalCount
public double totalCount()Finds the total of all counts in the counter. This implementation iterates through the entire counter every time this method is called.- Returns:
- the counter's total
-
getEntriesSortedByIncreasingCount
-
getEntriesSortedByDecreasingCount
-
argMax
Finds the key with maximum count. This is a linear operation, and ties are broken arbitrarily.- Returns:
- a key with minumum count
-
min
public double min() -
max
public double max() -
toString
Returns a string representation with the keys ordered by decreasing counts. -
toStringSortedByKeys
-
pruneKeysBelowThreshold
public void pruneKeysBelowThreshold(double cutoff) -
getEntrySet
-
isEqualTo
-
main
-
clear
public void clear() -
setAllCounts
public void setAllCounts(double val) Sets all counts to the given value, but does not remove any keys -
dotProduct
-
scale
public void scale(double c) -
scaledClone
-
difference
-
toLogSpace
-
approxEquals
-
setDirty
public void setDirty(boolean dirty) -
values
-
prune
-
pruneExcept
-
absCounts
-
putAll
public void putAll(double d)
-