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

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

public class ClassicCounter<E>
extends Object
implements Serializable, Counter<E>, Iterable<E>

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.

Implementation note: Note that this class stores a totalCount field as well as the map. This makes certain operations much more efficient, but means that any methods that change the map must also update totalCount appropriately. If you use the setCount method, then you cannot go wrong.

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

Constructor Summary
ClassicCounter()
          Constructs a new (empty) Counter.
ClassicCounter(Collection<E> collection)
          Constructs a new Counter by counting the elements in the given Collection.
ClassicCounter(Counter<E> c)
          Constructs a new Counter with the contents of the given Counter.
ClassicCounter(MapFactory<E,MutableDouble> mapFactory)
          Pass in a MapFactory and the map it vends will back your counter.
 
Method Summary
 void addAll(Collection<E> collection)
          Deprecated. Use own loop.
 void addAll(Counter<E> counter)
          Deprecated. Use Counters.addInPlace(Counter, Counter).
 void addMultiple(Counter<E> counter, double d)
          Deprecated. Use Counters.addInPlace(Counter, Counter, double)
 PriorityQueue<E> asPriorityQueue()
          Deprecated. Use Counters.toPriorityQueue(Counter)
 double averageCount()
          Deprecated. Use Counters.mean(Counter).
 void clear()
          Removes all counts from this Counter.
 Object clone()
           
 Comparator<E> comparator()
          Deprecated. Use Counters.toComparator(edu.stanford.nlp.stats.Counter)
 Comparator<E> comparator(boolean ascending)
          Deprecated. Use Counters.toComparator(edu.stanford.nlp.stats.Counter) and Collections.reverseOrder(Comparator) if necessary.
 Comparator<E> comparator(boolean ascending, boolean useMagnitude)
          Deprecated. Use Counters.toComparator(edu.stanford.nlp.stats.Counter)
 boolean containsKey(E key)
           
 double decrementCount(E key)
          Subtracts 1.0 from the count for the given key.
 double decrementCount(E key, double count)
          Subtracts the given count from the current count for the given key.
 void decrementCounts(Collection<E> keys)
          Deprecated. Use your own loop
 void decrementCounts(Collection<E> keys, double count)
          Deprecated. Use your own loop
 double defaultReturnValue()
          Returns the default return value.
 void defaultReturnValue(double rv)
          Sets the default return value.
 void divideBy(ClassicCounter<E> counter)
          Deprecated. Use Counters.divideInPlace(Counter, Counter)
 Counter<E> divideBy(double divisor)
          Deprecated. Use Counters.scaleInPlace(Counter, double)
 Set<Map.Entry<E,Double>> entrySet()
          Returns a view of the doubles in this map.
 boolean equals(Object o)
           
static ClassicCounter<String> fromString(String s)
          converts from format printed by toString method back into a Counter.
 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)
          Deprecated. Use Double.toString
 Factory<Counter<E>> getFactory()
          Returns a factory that can create new instances of this kind of Counter.
 double getNormalizedCount(E key)
          Deprecated. Use a Distribution or divide getCount by totalCount
 int hashCode()
           
 void incrementAll(double count)
          Adds the same amount to every count, that is to every key currently stored in the counter (with no lookups).
 double incrementCount(E key)
          Adds 1.0 to the count for the given key.
 double incrementCount(E key, double count)
          Adds the given count to the current count for the given key.
 void incrementCounts(Collection<E> keys)
          Deprecated. Use your own loop
 void incrementCounts(Collection<E> keys, double count)
          Deprecated. Use your own loop
 boolean isEmpty()
           
 Iterator<E> iterator()
           
 Set<E> keysAbove(double countThreshold)
          Deprecated. Use Counters.keysAbove(Counter, double)
 Set<E> keysAt(double count)
          Deprecated. Use Counters.keysAt(Counter, double)
 Set<E> keysBelow(double countThreshold)
          Deprecated. Use Counters.keysBelow(Counter, double)
 Set<E> keySet()
          Returns the Set of keys in this counter.
 double logIncrementCount(E key, double count)
          If the current count for the object is c1, and you call logIncrementCount with a value of c2, then the new value will be log(e^c1 + e^c2).
 void logNormalize()
          Deprecated. Use Counters.logNormalize(Counter).
 double logSum()
          Deprecated. Use Counters.logSum(Counter).
static void main(String[] args)
          For internal debugging purposes only.
 ClassicCounter<E> multiplyBy(double multiplier)
          Deprecated. Use Counters.scaleInPlace(Counter, double)
 void normalize()
          Deprecated. Use Counters#normalize(Counter)
 double remove(E key)
          Removes the given key from this Counter.
 void removeAll(Collection<E> keys)
          Removes all the given keys from this Counter.
 void removeZeroCounts()
          Deprecated. Use Counters.retainNonZeros(Counter)
 void retainTop(int num)
          Deprecated. Use Counters.retainTop(edu.stanford.nlp.stats.Counter, int)
 void setCount(E key, double count)
          Sets the current count for the given key.
 void setCount(E key, String s)
          Deprecated. Use setCount after Double.parseDouble
 int size()
          Returns the number of keys stored in the counter.
 void subtractAll(Counter<E> counter)
          Deprecated. Use Counters.subtractInPlace(Counter, Counter)
 void subtractAll(Counter<E> counter, boolean removeZeroKeys)
          Deprecated. Use Counters.subtractInPlace(Counter, Counter) and then Counters.retainNonZeros(Counter)
 void subtractMultiple(Counter<E> counter, double d)
          Deprecated. Use Counters.addInPlace(Counter, Counter, double)
 String toString()
           
 String toString(int maxKeysToPrint)
          Returns a string representation which includes no more than the maxKeysToPrint elements with largest counts.
 String toString(NumberFormat nf)
          Deprecated. Use Counters.toString(Counter, NumberFormat)
 String toString(NumberFormat nf, String preAppend, String postAppend, String keyValSeparator, String itemSeparator)
          Pretty print a Counter.
 double totalCount()
          Returns the current total count for all objects in this Counter.
 double totalCount(Filter<E> filter)
          Deprecated. Use own loop?
 double totalDoubleCount()
          Deprecated. Use totalCount().
static ClassicCounter<String> valueOf(String s)
          Returns the Counter over Strings specified by this String.
static ClassicCounter<String> valueOfIgnoreComments(String s)
          Similar to valueOf in that it returns the Counter over Strings specified by this String.
 Collection<Double> values()
          Returns a copy of the values currently in this counter.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ClassicCounter

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


ClassicCounter

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


ClassicCounter

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


ClassicCounter

public ClassicCounter(Collection<E> collection)
Constructs a new Counter by counting the elements in the given Collection.

Method Detail

defaultReturnValue

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

Specified by:
defaultReturnValue 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.

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.

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

setCount

public void setCount(E key,
                     double 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,double).

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

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. If a key not contained in the Counter is given, no action is performed on the Counter and Double.NaN. This behavior echoes that of HashMap, but differs since a HashMap returns a Double (rather than double) and thus returns null if a key is not present. Any future revisions of Counter should preserve the ability to "remove" a key that is not present in the Counter.

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

containsKey

public boolean containsKey(E key)
Specified by:
containsKey in interface Counter<E>
Returns:
true iff key is a key in this GenericCounter.

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.

values

public Collection<Double> values()
Description copied from interface: Counter
Returns a copy of the values currently in this counter.

Specified by:
values in interface Counter<E>

clear

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

Specified by:
clear in interface Counter<E>

size

public int size()
Returns the number of keys stored in the counter.

Specified by:
size in interface Counter<E>
Returns:
The number of keys

totalCount

public double totalCount()
Returns the current total count for all objects in this Counter.

Specified by:
totalCount in interface Counter<E>
Returns:
The total of all counts in this counter

iterator

public Iterator<E> iterator()
Specified by:
iterator in interface Iterable<E>

removeAll

public void removeAll(Collection<E> keys)
Removes all the given keys from this Counter. Keys may be included that are not actually in the Counter - no action is taken in response to those keys. This behavior should be retained in future revisions of Counter (matches HashMap).


isEmpty

public boolean isEmpty()

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>

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(int maxKeysToPrint)
Returns a string representation which includes no more than the maxKeysToPrint elements with largest counts.

Parameters:
maxKeysToPrint -
Returns:
partial string representation

toString

public String toString(NumberFormat nf,
                       String preAppend,
                       String postAppend,
                       String keyValSeparator,
                       String itemSeparator)
Pretty print a Counter. This one has more flexibility in formatting, and doesn't sort the keys.


toString

@Deprecated
public String toString(NumberFormat nf)
Deprecated. Use Counters.toString(Counter, NumberFormat)

Pretty print a Counter. This version tries to sort the keys.


clone

public Object clone()
Overrides:
clone in class Object

totalDoubleCount

@Deprecated
public double totalDoubleCount()
Deprecated. Use totalCount().

Returns the current total count for all objects in this Counter.


totalCount

@Deprecated
public double totalCount(Filter<E> filter)
Deprecated. Use own loop?

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


logSum

@Deprecated
public double logSum()
Deprecated. Use Counters.logSum(Counter).


logNormalize

@Deprecated
public void logNormalize()
Deprecated. Use Counters.logNormalize(Counter).


averageCount

@Deprecated
public double averageCount()
Deprecated. Use Counters.mean(Counter).

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


getCountAsString

@Deprecated
public String getCountAsString(E key)
Deprecated. Use Double.toString


setCount

@Deprecated
public void setCount(E key,
                                String s)
Deprecated. Use setCount after Double.parseDouble


getNormalizedCount

@Deprecated
public double getNormalizedCount(E key)
Deprecated. Use a Distribution or divide getCount by totalCount

Return the proportion of the Counter mass under this key. This has been de-deprecated in order to reduce compilation warnings, but really you should create a Distribution instead of using this method.


incrementCount

public double incrementCount(E key,
                             double 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 conveniently increment the count by 1.0, use incrementCount(Object).

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

Specified by:
incrementCount in interface Counter<E>
Parameters:
key - The key to increment
count - The amount to increment it by
Returns:
Value of incremented key (post-increment)

logIncrementCount

public double logIncrementCount(E key,
                                double count)
If the current count for the object is c1, and you call logIncrementCount with a value of c2, then the new value will be log(e^c1 + e^c2). If the key hasn't been seen before, it is assumed to have count Double.NEGATIVE_INFINITY, and thus this method will set its count to the given amount. To set a count to a specifc value instead of incrementing it, use setCount(Object,double).

Specified by:
logIncrementCount in interface Counter<E>
Parameters:
key - The key to increment
count - The amount to increment it by, in log space
Returns:
Value of incremented key (post-increment)

incrementCount

public double incrementCount(E key)
Adds 1.0 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.0.

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

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

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

incrementCounts

@Deprecated
public void incrementCounts(Collection<E> keys,
                                       double count)
Deprecated. Use your own loop

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.


incrementCounts

@Deprecated
public void incrementCounts(Collection<E> keys)
Deprecated. Use your own loop

Adds 1.0 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.0.


incrementAll

public void incrementAll(double count)
Adds the same amount to every count, that is to every key currently stored in the counter (with no lookups).

Parameters:
count - The amount to be added

decrementCount

public double decrementCount(E key,
                             double 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.0, use decrementCount(Object).

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

Specified by:
decrementCount in interface Counter<E>
Parameters:
key - The key to decrement
count - The amount to decrement it by
Returns:
The value associated with they key, post-decrement.

decrementCount

public double decrementCount(E key)
Subtracts 1.0 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.0.

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

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

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

decrementCounts

@Deprecated
public void decrementCounts(Collection<E> keys,
                                       double count)
Deprecated. Use your own loop

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.


decrementCounts

@Deprecated
public void decrementCounts(Collection<E> keys)
Deprecated. Use your own loop

Subtracts 1.0 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.0.


addAll

@Deprecated
public void addAll(Counter<E> counter)
Deprecated. Use Counters.addInPlace(Counter, 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


addMultiple

@Deprecated
public void addMultiple(Counter<E> counter,
                                   double d)
Deprecated. Use Counters.addInPlace(Counter, Counter, double)

Adds the counts in the given Counter to the counts in this Counter.


subtractAll

@Deprecated
public void subtractAll(Counter<E> counter)
Deprecated. Use Counters.subtractInPlace(Counter, Counter)

Subtracts the counts in the given Counter to the counts in this Counter.

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


subtractMultiple

@Deprecated
public void subtractMultiple(Counter<E> counter,
                                        double d)
Deprecated. Use Counters.addInPlace(Counter, Counter, double)

Subtracts the counts in the given Counter to the counts in this Counter.


addAll

@Deprecated
public void addAll(Collection<E> collection)
Deprecated. Use own loop.

Calls incrementCount(key) on each key in the given collection.


multiplyBy

@Deprecated
public ClassicCounter<E> multiplyBy(double multiplier)
Deprecated. Use Counters.scaleInPlace(Counter, double)

Multiplies every count by the given multiplier.


divideBy

@Deprecated
public Counter<E> divideBy(double divisor)
Deprecated. Use Counters.scaleInPlace(Counter, double)

Divides every count by the given divisor.


divideBy

@Deprecated
public void divideBy(ClassicCounter<E> counter)
Deprecated. Use Counters.divideInPlace(Counter, Counter)

Divides every non-zero count by the count for the corresponding key in the argument Counter. Beware that this can give NaN values for zero counts in the argument counter!

Parameters:
counter - Entries in argument scale individual keys in this counter

subtractAll

public void subtractAll(Counter<E> counter,
                        boolean removeZeroKeys)
Deprecated. Use Counters.subtractInPlace(Counter, Counter) and then Counters.retainNonZeros(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


normalize

@Deprecated
public void normalize()
Deprecated. Use Counters#normalize(Counter)

This has been de-deprecated in order to reduce compilation warnings, but really you should create a Distribution instead.


removeZeroCounts

@Deprecated
public void removeZeroCounts()
Deprecated. Use Counters.retainNonZeros(Counter)

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


asPriorityQueue

@Deprecated
public PriorityQueue<E> asPriorityQueue()
Deprecated. Use Counters.toPriorityQueue(Counter)

Builds a priority queue whose elements are the counter's elements, and whose priorities are those elements' counts in the counter.


retainTop

@Deprecated
public void retainTop(int num)
Deprecated. Use Counters.retainTop(edu.stanford.nlp.stats.Counter, int)


keysAbove

@Deprecated
public Set<E> keysAbove(double countThreshold)
Deprecated. Use Counters.keysAbove(Counter, double)

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

@Deprecated
public Set<E> keysBelow(double countThreshold)
Deprecated. Use Counters.keysBelow(Counter, double)

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

@Deprecated
public Set<E> keysAt(double count)
Deprecated. Use Counters.keysAt(Counter, double)

Returns the set of keys that have exactly the given count. This set may have 0 elements but will not be null.


comparator

@Deprecated
public Comparator<E> comparator(boolean ascending)
Deprecated. Use Counters.toComparator(edu.stanford.nlp.stats.Counter) and Collections.reverseOrder(Comparator) if necessary.

Returns a comparator suitable for sorting this Counter's keys or entries by their respective counts. If ascending is true, lower counts will be returned first, otherwise higher counts will be returned first.

Sample usage:

 Counter c = new Counter();
 // add to the counter...
 List biggestKeys = new ArrayList(c.keySet());
 Collections.sort(biggestKeys, c.comparator(false));
 List smallestEntries = new ArrayList(c.entrySet());
 Collections.sort(smallestEntries, c.comparator(true))
 


comparator

@Deprecated
public Comparator<E> comparator(boolean ascending,
                                           boolean useMagnitude)
Deprecated. Use Counters.toComparator(edu.stanford.nlp.stats.Counter)

Returns a comparator suitable for sorting this Counter's keys or entries by their respective value or magnitude (unsigned value). If ascending is true, smaller magnitudes will be returned first, otherwise higher magnitudes will be returned first.

Sample usage:

 Counter c = new Counter();
 // add to the counter...
 List biggestKeys = new ArrayList(c.keySet());
 Collections.sort(biggestKeys, c.comparator(false, true));
 List smallestEntries = new ArrayList(c.entrySet());
 Collections.sort(smallestEntries, c.comparator(true))
 


comparator

@Deprecated
public Comparator<E> comparator()
Deprecated. Use Counters.toComparator(edu.stanford.nlp.stats.Counter)

Comparator that sorts objects by (increasing) count. Shortcut for calling comparator(true).


valueOf

public static ClassicCounter<String> valueOf(String s)
Returns the Counter over Strings specified by this String. The String is normally the whole contents of a file. Format is one entry per line, which is "String key\tdouble value".

Parameters:
s - A String representation of a Counter, where the entries are one-per-line ('\n') and each line is key \t value
Returns:
The Counter with String keys

valueOfIgnoreComments

public static ClassicCounter<String> valueOfIgnoreComments(String s)
Similar to valueOf in that it returns the Counter over Strings specified by this String. String is again normally the whole contents of a file, but in this case the file can include comments if each line of comment starts with a hash (#) symbol. Otherwise, format is one entry per line, "String key\tdouble value".

Parameters:
s - String representation of a coounter, where entries are one per line such that each line is either a comment (begins with #) or key \t value
Returns:
The Counter with String keys

fromString

public static ClassicCounter<String> fromString(String s)
converts from format printed by toString method back into a Counter.


main

public static void main(String[] args)
                 throws Exception
For internal debugging purposes only.

Throws:
Exception


Stanford NLP Group