edu.stanford.nlp.stats
Class IntCounter<E>

java.lang.Object
  extended by edu.stanford.nlp.stats.AbstractCounter<E>
      extended by edu.stanford.nlp.stats.IntCounter<E>
All Implemented Interfaces:
Counter<E>, Serializable

public class IntCounter<E>
extends AbstractCounter<E>
implements Serializable

A specialized kind of hash table (or map) for storing numeric counts for objects. It works like a Map, but with different methods for easily getting/setting/incrementing counts for objects and computing various functions with the counts. The Counter constructor and addAll method can be used to copy another Counter's contents over. This class also provides access to Comparators that can be used to sort the keys or entries of this Counter by the counts, in either ascending or descending order.

Author:
Dan Klein (klein@cs.stanford.edu), Joseph Smarr (jsmarr@stanford.edu), Teg Grenager (grenager@stanford.edu), Galen Andrew, Christopher Manning
See Also:
Serialized Form

Constructor Summary
IntCounter()
          Constructs a new (empty) Counter.
IntCounter(IntCounter<E> c)
          Constructs a new Counter with the contents of the given Counter.
IntCounter(MapFactory<E,MutableInteger> mapFactory)
          Pass in a MapFactory and the map it vends will back your counter.
 
Method Summary
 void addAll(IntCounter<E> counter)
          Adds the counts in the given Counter to the counts in this Counter.
 E argmax()
          Finds and returns the key in this Counter with the largest count.
 E argmax(Comparator tieBreaker)
          Finds and returns the key in this Counter with the largest count.
 E argmin()
          Finds and returns the key in this Counter with the smallest count.
 E argmin(Comparator tieBreaker)
          Finds and returns the key in this Counter with the smallest count.
 double averageCount()
          Returns the mean of all the counts (totalCount/size).
 void clear()
          Removes all counts from this Counter.
 Object clone()
           
 boolean containsKey(E key)
          Returns whether a Counter contains a key.
 double decrementCount(E key)
          Subtracts 1 from the count for the given key.
 int decrementCount(E key, int count)
          Subtracts the given count from the current count for the given key.
 void decrementCounts(Collection<E> keys)
          Subtracts 1 from the counts of each of the given keys.
 void decrementCounts(Collection<E> keys, int count)
          Subtracts the given count from the current counts for each of the given keys.
 double defaultReturnValue()
          Returns the default return value.
 double doubleMax()
           
 Set<Map.Entry<E,Double>> entrySet()
          Returns a view of the doubles in this map.
 boolean equals(Object o)
           
 double getCount(E key)
          Returns the current count for the given key, which is 0 if it hasn't been seen before.
 String getCountAsString(E key)
           
 Factory<Counter<E>> getFactory()
          Returns a factory that can create new instances of this kind of Counter.
 int getIntCount(E key)
          Returns the current count for the given key, which is 0 if it hasn't been seen before.
 MapFactory getMapFactory()
           
 double getNormalizedCount(E key)
          This has been de-deprecated in order to reduce compilation warnings, but really you should create a edu.stanford.nlp.stats.Distribution instead of using this method.
 int hashCode()
           
 double incrementCount(E key)
          Adds 1 to the count for the given key.
 double incrementCount(E key, double value)
          Increments the count for the given key by the given value.
 int incrementCount(E key, int count)
          Adds the given count to the current count for the given key.
 void incrementCounts(Collection<E> keys)
          Adds 1 to the counts for each of the given keys.
 void incrementCounts(Collection<E> keys, int count)
          Adds the given count to the current counts for each of the given keys.
 boolean isEmpty()
           
 Iterator<E> iterator()
           
 Set<E> keysAbove(int countThreshold)
          Returns the set of keys whose counts are at or above the given threshold.
 Set keysAt(int count)
          Returns the set of keys that have exactly the given count.
 Set keysBelow(int countThreshold)
          Returns the set of keys whose counts are at or below the given threshold.
 Set<E> keySet()
          Returns the Set of keys in this counter.
 int max()
          Finds and returns the largest count in this Counter.
 int min()
          Finds and returns the smallest count in this Counter.
 double remove(E key)
          Removes the given key from this Counter.
 void removeAll(Collection<E> c)
          Removes all the given keys from this Counter.
 void removeZeroCounts()
          Removes all keys whose count is 0.
 void setCount(E key, double value)
          Sets the count for the given key to be the given value.
 void setCount(E key, int count)
          Sets the current count for the given key.
 void setCount(E key, String s)
           
 void setCounts(Collection<E> keys, int count)
          Sets the current count for each of the given keys.
 void setDefaultReturnValue(double rv)
          Sets the default return value.
 int size()
          Returns the number of entries stored in this counter.
 void subtractAll(IntCounter<E> counter)
          Subtracts the counts in the given Counter from the counts in this Counter.
 String toString()
           
 String toString(NumberFormat nf)
           
 String toString(NumberFormat nf, String preAppend, String postAppend, String keyValSeparator, String itemSeparator)
           
 double totalCount()
          Computes the total of all counts in this counter, and returns it as a double.
 double totalCount(Filter<E> filter)
           
 double totalDoubleCount()
           
 double totalDoubleCount(Filter<E> filter)
           
 int totalIntCount()
          Returns the current total count for all objects in this Counter.
 int totalIntCount(Filter<E> filter)
          Returns the total count for all objects in this Counter that pass the given Filter.
 Collection<Double> values()
          Returns a copy of the values currently in this counter.
 
Methods inherited from class edu.stanford.nlp.stats.AbstractCounter
addAll, decrementCount, logIncrementCount
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IntCounter

public IntCounter()
Constructs a new (empty) Counter.


IntCounter

public IntCounter(MapFactory<E,MutableInteger> mapFactory)
Pass in a MapFactory and the map it vends will back your counter.


IntCounter

public IntCounter(IntCounter<E> c)
Constructs a new Counter with the contents of the given Counter.

Method Detail

getMapFactory

public MapFactory getMapFactory()

setDefaultReturnValue

public void setDefaultReturnValue(double rv)
Description copied from interface: Counter
Sets the default return value. This value is returned when you get the value for keys that are not in the Counter. It is zero by default, but can sometimes usefully by set to other values like Double.NaN or Double.NEGATIVE_INFINITY.

Specified by:
setDefaultReturnValue in interface Counter<E>
Parameters:
rv - The default value

defaultReturnValue

public double defaultReturnValue()
Description copied from interface: Counter
Returns the default return value.

Specified by:
defaultReturnValue in interface Counter<E>
Returns:
The default return value.

totalIntCount

public int totalIntCount()
Returns the current total count for all objects in this Counter. All counts are summed each time, so cache it if you need it repeatedly.

Returns:
The current total count for all objects in this Counter.

totalDoubleCount

public double totalDoubleCount()

totalIntCount

public int totalIntCount(Filter<E> filter)
Returns the total count for all objects in this Counter that pass the given Filter. Passing in a filter that always returns true is equivalent to calling totalCount().


totalDoubleCount

public double totalDoubleCount(Filter<E> filter)

totalCount

public double totalCount(Filter<E> filter)

averageCount

public double averageCount()
Returns the mean of all the counts (totalCount/size).


getCount

public double getCount(E key)
Returns the current count for the given key, which is 0 if it hasn't been seen before. This is a convenient version of get that casts and extracts the primitive value.

Specified by:
getCount in interface Counter<E>
Parameters:
key - The key
Returns:
The count

getCountAsString

public String getCountAsString(E key)

getIntCount

public int getIntCount(E key)
Returns the current count for the given key, which is 0 if it hasn't been seen before. This is a convenient version of get that casts and extracts the primitive value.


getNormalizedCount

public double getNormalizedCount(E key)
This has been de-deprecated in order to reduce compilation warnings, but really you should create a edu.stanford.nlp.stats.Distribution instead of using this method.


setCount

public void setCount(E key,
                     int count)
Sets the current count for the given key. This will wipe out any existing count for that key.

To add to a count instead of replacing it, use incrementCount(Object,int).


setCount

public void setCount(E key,
                     String s)

setCounts

public void setCounts(Collection<E> keys,
                      int count)
Sets the current count for each of the given keys. This will wipe out any existing counts for these keys.

To add to the counts of a collection of objects instead of replacing them, use incrementCounts(Collection,int).


incrementCount

public int incrementCount(E key,
                          int count)
Adds the given count to the current count for the given key. If the key hasn't been seen before, it is assumed to have count 0, and thus this method will set its count to the given amount. Negative increments are equivalent to calling decrementCount.

To more conviently increment the count by 1, use incrementCount(Object).

To set a count to a specifc value instead of incrementing it, use setCount(Object,int).


incrementCount

public double incrementCount(E key)
Adds 1 to the count for the given key. If the key hasn't been seen before, it is assumed to have count 0, and thus this method will set its count to 1.

To increment the count by a value other than 1, use incrementCount(Object,int).

To set a count to a specifc value instead of incrementing it, use setCount(Object,int).

Specified by:
incrementCount in interface Counter<E>
Overrides:
incrementCount in class AbstractCounter<E>
Parameters:
key - The key to increment by 1.0
Returns:
The value associated with they key, post-increment.

incrementCounts

public void incrementCounts(Collection<E> keys,
                            int count)
Adds the given count to the current counts for each of the given keys. If any of the keys haven't been seen before, they are assumed to have count 0, and thus this method will set their counts to the given amount. Negative increments are equivalent to calling decrementCounts.

To more conviniently increment the counts of a collection of objects by 1, use incrementCounts(Collection).

To set the counts of a collection of objects to a specific value instead of incrementing them, use setCounts(Collection,int).


incrementCounts

public void incrementCounts(Collection<E> keys)
Adds 1 to the counts for each of the given keys. If any of the keys haven't been seen before, they are assumed to have count 0, and thus this method will set their counts to 1.

To increment the counts of a collection of object by a value other than 1, use incrementCounts(Collection,int).

To set the counts of a collection of objects to a specific value instead of incrementing them, use setCounts(Collection,int).


decrementCount

public int decrementCount(E key,
                          int count)
Subtracts the given count from the current count for the given key. If the key hasn't been seen before, it is assumed to have count 0, and thus this method will set its count to the negative of the given amount. Negative increments are equivalent to calling incrementCount.

To more conviently decrement the count by 1, use decrementCount(Object).

To set a count to a specifc value instead of decrementing it, use setCount(Object,int).


decrementCount

public double decrementCount(E key)
Subtracts 1 from the count for the given key. If the key hasn't been seen before, it is assumed to have count 0, and thus this method will set its count to -1.

To decrement the count by a value other than 1, use decrementCount(Object,int).

To set a count to a specifc value instead of decrementing it, use setCount(Object,int).

Specified by:
decrementCount in interface Counter<E>
Overrides:
decrementCount in class AbstractCounter<E>
Parameters:
key - The key to decrement by 1.0
Returns:
The value of associated with they key, post-decrement.

decrementCounts

public void decrementCounts(Collection<E> keys,
                            int count)
Subtracts the given count from the current counts for each of the given keys. If any of the keys haven't been seen before, they are assumed to have count 0, and thus this method will set their counts to the negative of the given amount. Negative increments are equivalent to calling incrementCount.

To more conviniently decrement the counts of a collection of objects by 1, use decrementCounts(Collection).

To set the counts of a collection of objects to a specific value instead of decrementing them, use setCounts(Collection,int).


decrementCounts

public void decrementCounts(Collection<E> keys)
Subtracts 1 from the counts of each of the given keys. If any of the keys haven't been seen before, they are assumed to have count 0, and thus this method will set their counts to -1.

To decrement the counts of a collection of object by a value other than 1, use decrementCounts(Collection,int).

To set the counts of a collection of objects to a specifc value instead of decrementing them, use setCounts(Collection,int).


addAll

public void addAll(IntCounter<E> counter)
Adds the counts in the given Counter to the counts in this Counter.

To copy the values from another Counter rather than adding them, use


subtractAll

public void subtractAll(IntCounter<E> counter)
Subtracts the counts in the given Counter from the counts in this Counter.

To copy the values from another Counter rather than subtracting them, use


containsKey

public boolean containsKey(E key)
Description copied from interface: Counter
Returns whether a Counter contains a key.

Specified by:
containsKey in interface Counter<E>
Parameters:
key - The key
Returns:
true iff key is a key in this Counter.

remove

public double remove(E key)
Removes the given key from this Counter. Its count will now be 0 and it will no longer be considered previously seen.

Specified by:
remove in interface Counter<E>
Parameters:
key - The key
Returns:
The value removed from the map or the default value if no count was associated with that key.

removeAll

public void removeAll(Collection<E> c)
Removes all the given keys from this Counter.


clear

public void clear()
Removes all counts from this Counter.

Specified by:
clear in interface Counter<E>

size

public int size()
Description copied from interface: Counter
Returns the number of entries stored in this counter.

Specified by:
size in interface Counter<E>
Returns:
The number of entries in this counter.

isEmpty

public boolean isEmpty()

keySet

public Set<E> keySet()
Description copied from interface: Counter
Returns the Set of keys in this counter.

Specified by:
keySet in interface Counter<E>
Returns:
The Set of keys in this counter.

entrySet

public Set<Map.Entry<E,Double>> entrySet()
Returns a view of the doubles in this map. Can be safely modified.

Specified by:
entrySet in interface Counter<E>
Returns:
A view of the entries in this counter

equals

public boolean equals(Object o)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

toString

public String toString(NumberFormat nf,
                       String preAppend,
                       String postAppend,
                       String keyValSeparator,
                       String itemSeparator)

toString

public String toString(NumberFormat nf)

clone

public Object clone()
Overrides:
clone in class Object

removeZeroCounts

public void removeZeroCounts()
Removes all keys whose count is 0. After incrementing and decrementing counts or adding and subtracting Counters, there may be keys left whose count is 0, though normally this is undesirable. This method cleans up the map.

Maybe in the future we should try to do this more on-the-fly, though it's not clear whether a distinction should be made between "never seen" (i.e. null count) and "seen with 0 count". Certainly there's no distinction in getCount() but there is in containsKey().


max

public int max()
Finds and returns the largest count in this Counter.


doubleMax

public double doubleMax()

min

public int min()
Finds and returns the smallest count in this Counter.


argmax

public E argmax(Comparator tieBreaker)
Finds and returns the key in this Counter with the largest count. Ties are broken by comparing the objects using the given tie breaking Comparator, favoring Objects that are sorted to the front. This is useful if the keys are numeric and there is a bias to prefer smaller or larger values, and can be useful in other circumstances where random tie-breaking is not desirable. Returns null if this Counter is empty.


argmax

public E argmax()
Finds and returns the key in this Counter with the largest count. Ties are broken according to the natural ordering of the objects. This will prefer smaller numeric keys and lexicographically earlier String keys. To use a different tie-breaking Comparator, use argmax(Comparator). Returns null if this Counter is empty.


argmin

public E argmin(Comparator tieBreaker)
Finds and returns the key in this Counter with the smallest count. Ties are broken by comparing the objects using the given tie breaking Comparator, favoring Objects that are sorted to the front. This is useful if the keys are numeric and there is a bias to prefer smaller or larger values, and can be useful in other circumstances where random tie-breaking is not desirable. Returns null if this Counter is empty.


argmin

public E argmin()
Finds and returns the key in this Counter with the smallest count. Ties are broken according to the natural ordering of the objects. This will prefer smaller numeric keys and lexicographically earlier String keys. To use a different tie-breaking Comparator, use argmin(Comparator). Returns null if this Counter is empty.


keysAbove

public Set<E> keysAbove(int countThreshold)
Returns the set of keys whose counts are at or above the given threshold. This set may have 0 elements but will not be null.


keysBelow

public Set keysBelow(int countThreshold)
Returns the set of keys whose counts are at or below the given threshold. This set may have 0 elements but will not be null.


keysAt

public Set keysAt(int count)
Returns the set of keys that have exactly the given count. This set may have 0 elements but will not be null.


getFactory

public Factory<Counter<E>> getFactory()
Description copied from interface: Counter
Returns a factory that can create new instances of this kind of Counter.

Specified by:
getFactory in interface Counter<E>
Returns:
A factory that can create new instances of this kind of Counter.

setCount

public void setCount(E key,
                     double value)
Description copied from interface: Counter
Sets the count for the given key to be the given value. This will replace any existing count for the key. To add to a count instead of replacing it, use Counter.incrementCount(Object,double).

Specified by:
setCount in interface Counter<E>
Parameters:
key - The key
value - The count

incrementCount

public double incrementCount(E key,
                             double value)
Description copied from interface: Counter
Increments the count for the given key by the given value. If the key hasn't been seen before, it is assumed to have count 0.0, and thus this method will set its count to the given amount. Note that this is true regardless of the setting of defaultReturnValue. Negative increments are equivalent to calling decrementCount. To more conveniently increment the count by 1.0, use Counter.incrementCount(Object). To set a count to a specifc value instead of incrementing it, use Counter.setCount(Object,double).

Specified by:
incrementCount in interface Counter<E>
Overrides:
incrementCount in class AbstractCounter<E>
Parameters:
key - The key to increment
value - The amount to increment it by
Returns:
The value associated with they key, post-increment.

totalCount

public double totalCount()
Description copied from interface: Counter
Computes the total of all counts in this counter, and returns it as a double. (Existing implementations cache this value, so that this operation is cheap.)

Specified by:
totalCount in interface Counter<E>
Returns:
The total (arithmetic sum) of all counts in this counter.

values

public Collection<Double> values()
Description copied from interface: Counter
Returns a copy of the values currently in this counter. (You should regard this Collection as read-only for forward compatibility; at present implementations differ on how they respond to attempts to change this Collection.)

Specified by:
values in interface Counter<E>
Returns:
A copy of the values currently in this counter.

iterator

public Iterator<E> iterator()


Stanford NLP Group