Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

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

    java.lang.Object
      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.

Implementation notes: You shouldn't casually add further methods to this interface. Rather, they should be added to the `Counters` class. 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. This class is not threadsafe: If multiple threads are accessing the same counter, then access should be synchronized externally to this class.

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 backed by a HashMap.
`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(Counter<E> counter)` Adds the counts in the given Counter to the counts in this Counter.
`void` | `clear()` Removes all entries from the counter.
`boolean` | `containsKey(E key)` Returns whether a Counter contains a key.
`double` | `decrementCount(E key)` Decrements the count for this key by 1.0.
`double` | `decrementCount(E key, double count)` Decrements the count for this key by the given value.
`double` | `defaultReturnValue()` Returns the default return value.
`Set<Map.Entry<E,Double>>` | `entrySet()` Returns a view of the entries in this counter.
`boolean` | `equals(Object o)` Equality is defined over all Counter implementations.
`static ClassicCounter<String>` | `fromString(String s)` Converts from the format printed by the toString method back into a Counter<String>.
`double` | `getCount(E key)` Returns the count for this key as a double.
`Factory<Counter<E>>` | `getFactory()` Returns a factory that can create new instances of this kind of Counter.
`int` | `hashCode()` Returns a hashCode which is the underlying Map's hashCode.
`double` | `incrementCount(E key)` Increments the count for this key by 1.0.
`double` | `incrementCount(E key, double count)` Increments the count for the given key by the given value.
`boolean` | `isEmpty()` Returns whether a Counter has no keys in it.
`Iterator<E>` | `iterator()` This is a shorthand for keySet.iterator().
`Set<E>` | `keySet()` Returns the Set of keys in this counter.
`double` | `logIncrementCount(E key, double count)` Increments the count stored in log space for this key by the given log-transformed value.
`double` | `remove(E key)` Removes the given key and its associated value from this Counter.
`void` | `removeAll(Collection<E> keys)` Removes all the given keys from this Counter.
`void` | `setCount(E key, double count)` Sets the count for the given key to be the given value.
`void` | `setDefaultReturnValue(double rv)` Sets the default return value.
`int` | `size()` Returns the number of entries stored in this counter.
`String` | `toString()` Returns a String representation of the Counter, as formatted by the underlying Map.
`double` | `totalCount()` Computes the total of all counts in this counter, and returns it as a double.
`static ClassicCounter<String>` | `valueOfIgnoreComments(String s)` 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
`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

Constructor Detail

### ClassicCounter

    public ClassicCounter()

  Constructs a new (empty) Counter backed by a HashMap.

### ClassicCounter

    public ClassicCounter(MapFactory<E,MutableDouble> mapFactory)

  Pass in a MapFactory and the map it vends will back your Counter.

  Parameters:
    `mapFactory` - The Map this factory vends will back your Counter.

### ClassicCounter

    public ClassicCounter(Counter<E> c)

  Constructs a new Counter with the contents of the given Counter. Implementation note: A new Counter is allocated with its own counts, but keys will be shared and should be an immutable class.

  Parameters:
    `c` - The Counter which will be copied.

### ClassicCounter

    public ClassicCounter(Collection<E> collection)

  Constructs a new Counter by counting the elements in the given Collection. The Counter is backed by a HashMap.

  Parameters:
    `collection` - Each item in the Collection is made a key in the Counter with count being its multiplicity in the Collection.

Method Detail

### getFactory

    public Factory<Counter<E>> getFactory()

  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.

### setDefaultReturnValue

    public final void setDefaultReturnValue(double rv)

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

  Returns the default return value.

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

### getCount

    public double getCount(E key)

  Returns the count for this key as a double. This is the defaultReturnValue (0.0, if it hasn't been set) if the key hasn't previously been seen.

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

### setCount

    public void setCount(E key,
                         double count)

  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
    `count` - The count

### incrementCount

    public double incrementCount(E key,
                                 double count)

  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>`
  Parameters:
    `key` - The key to increment
    `count` - The amount to increment it by
  Returns:
    The value associated with they key, post-increment.

### incrementCount

    public final double incrementCount(E key)

  Increments the count for this key by 1.0. 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 1.0. Note that this is true regardless of the setting of defaultReturnValue. To increment the count by a value other than 1.0, use `Counter.incrementCount(Object,double)`. To set a count to a specifc value instead of incrementing it, use `Counter.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.

### decrementCount

    public double decrementCount(E key,
                                 double count)

  Decrements the count for this 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 negative of the given amount. Note that this is true regardless of the setting of defaultReturnValue. Negative increments are equivalent to calling `incrementCount`. To more conviently decrement the count by 1.0, use `Counter.decrementCount(Object)`. To set a count to a specifc value instead of decrementing it, use `Counter.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)

  Decrements the count for this key by 1.0. 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 -1.0. Note that this is true regardless of the setting of defaultReturnValue. To decrement the count by a value other than 1.0, use `Counter.decrementCount(Object,double)`. To set a count to a specifc value instead of decrementing it, use `Counter.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.

### logIncrementCount

    public double logIncrementCount(E key,
                                    double count)

  Increments the count stored in log space for this key by the given log-transformed value. If the current count for the key is v1, and you call logIncrementCount with a value of v2, then the new value will be log(e^v1 + e^v2). 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. Note that this is true regardless of the setting of defaultReturnValue. To set a count to a specifc value instead of incrementing it, you need to first take the log yourself and then to call `Counter.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:
    The value associated with they key, post-increment, in log space

### addAll

    public void addAll(Counter<E> counter)

  Adds the counts in the given Counter to the counts in this Counter. This is identical in effect to calling Counters.addInPlace(this, counter).

  Specified by:
    `addAll` in interface `Counter<E>`
  Parameters:
    `counter` - The Counter whose counts will be added. For each key in counter, if it is not in this, then it will be added with value `counter.getCount(key)`. Otherwise, it will have value `this.getCount(key) + counter.getCount(key)`.

### remove

    public double remove(E key)

  Removes the given key and its associated value from this Counter. Its count will now be returned as the defaultReturnValue 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 is returned. 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>`
  Parameters:
    `key` - The key
  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)

  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.

### keySet

    public Set<E> keySet()

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

  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.

### entrySet

    public Set<Map.Entry<E,Double>> entrySet()

  Returns a view of the entries in this counter. The values can be safely modified with setValue().

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

### clear

    public void clear()

  Removes all entries from the counter.

  Specified by:
    `clear` in interface `Counter<E>`

### size

    public int size()

  Returns the number of entries stored in this counter.

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

### totalCount

    public double totalCount()

  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.

### iterator

    public Iterator<E> iterator()

  This is a shorthand for keySet.iterator(). It's not really clear that this method should be here, as the Map interface has no such shortcut, but it's used in a number of places, and I've left it in for now. Use is discouraged.

  Specified by:
    `iterator` in interface `Iterable<E>`
  Returns:
    An Iterator over the keys in the Counter.

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

  Parameters:
    `keys` - The keys to remove from the Counter. Their values are subtracted from the total count mass of the Counter.

### isEmpty

    public boolean isEmpty()

  Returns whether a Counter has no keys in it.

  Returns:
    true iff a Counter has no keys in it.

### equals

    public boolean equals(Object o)

  Equality is defined over all Counter implementations. Two Counters are equal if they have the same keys explicitly stored with the same values.

  Note that a Counter with a key with value defaultReturnValue will not be judged equal to a Counter that is lacking that key. In order for two Counters to be correctly judged equal in such cases, you should call Counters.retainNonDefaultValues() on both Counters first.

  Overrides:
    `equals` in class `Object`
  Parameters:
    `o` - Object to compare for equality
  Returns:
    Whether this is equal to o

### hashCode

    public int hashCode()

  Returns a hashCode which is the underlying Map's hashCode.

  Overrides:
    `hashCode` in class `Object`
  Returns:
    A hashCode.

### toString

    public String toString()

  Returns a String representation of the Counter, as formatted by the underlying Map.

  Overrides:
    `toString` in class `Object`
  Returns:
    A String representation of the Counter.

### valueOfIgnoreComments

    public static ClassicCounter<String> valueOfIgnoreComments(String s)

  Returns the Counter over Strings specified by this String. The String is often the whole contents of a file. The file can include comments if each line of comment starts with a hash (#) symbol, and does not contain any TAB characters. Otherwise, the format is one entry per line. Each line must contain precisely one tab separating a key and a value, giving a format of:

  > StringKey\tdoubleValue\n

  Parameters:
    `s` - String representation of a Counter, 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 the format printed by the toString method back into a Counter<String>. The toString() doesn't escape, so this only works providing the keys of the Counter do not have commas or equals signs in them.

  Parameters:
    `s` - A String representation of a Counter
  Returns:
    The Counter

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Stanford NLP Group