|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectedu.stanford.nlp.stats.ClassicCounter<E>
public class ClassicCounter<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 theCounters
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.
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(Object 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 |
---|
public ClassicCounter()
public ClassicCounter(MapFactory<E,MutableDouble> mapFactory)
mapFactory
- The Map this factory vends will back your Counter.public ClassicCounter(Counter<E> c)
c
- The Counter which will be copied.public ClassicCounter(Collection<E> collection)
collection
- Each item in the Collection is made a key in the
Counter with count being its multiplicity in the Collection.Method Detail |
---|
public Factory<Counter<E>> getFactory()
getFactory
in interface Counter<E>
public final void setDefaultReturnValue(double rv)
setDefaultReturnValue
in interface Counter<E>
rv
- The default valuepublic double defaultReturnValue()
defaultReturnValue
in interface Counter<E>
public double getCount(Object key)
getCount
in interface Counter<E>
key
- The key
public void setCount(E key, double count)
Counter.incrementCount(Object,double)
.
setCount
in interface Counter<E>
key
- The keycount
- The countpublic double incrementCount(E key, double count)
Counter.incrementCount(Object)
.
To set a count to a specific value instead of incrementing it, use
Counter.setCount(Object,double)
.
incrementCount
in interface Counter<E>
key
- The key to incrementcount
- The amount to increment it by
public final double incrementCount(E key)
Counter.incrementCount(Object,double)
.
To set a count to a specific value instead of incrementing it, use
Counter.setCount(Object,double)
.
incrementCount
in interface Counter<E>
key
- The key to increment by 1.0
public double decrementCount(E key, double count)
incrementCount
.
To more conveniently decrement the count by 1.0, use
Counter.decrementCount(Object)
.
To set a count to a specific value instead of decrementing it, use
Counter.setCount(Object,double)
.
decrementCount
in interface Counter<E>
key
- The key to decrementcount
- The amount to decrement it by
public double decrementCount(E key)
Counter.decrementCount(Object,double)
.
To set a count to a specific value instead of decrementing it, use
Counter.setCount(Object,double)
.
decrementCount
in interface Counter<E>
key
- The key to decrement by 1.0
public double logIncrementCount(E key, double count)
Counter.setCount(Object,double)
.
logIncrementCount
in interface Counter<E>
key
- The key to incrementcount
- The amount to increment it by, in log space
public void addAll(Counter<E> counter)
addAll
in interface Counter<E>
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)
.public double remove(E key)
remove
in interface Counter<E>
key
- The key
public boolean containsKey(E key)
containsKey
in interface Counter<E>
key
- The key
public Set<E> keySet()
keySet
in interface Counter<E>
public Collection<Double> values()
values
in interface Counter<E>
public Set<Map.Entry<E,Double>> entrySet()
entrySet
in interface Counter<E>
public void clear()
clear
in interface Counter<E>
public int size()
size
in interface Counter<E>
public double totalCount()
totalCount
in interface Counter<E>
public Iterator<E> iterator()
iterator
in interface Iterable<E>
public void removeAll(Collection<E> keys)
keys
- The keys to remove from the Counter. Their values are
subtracted from the total count mass of the Counter.public boolean isEmpty()
public boolean equals(Object o)
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.
equals
in class Object
o
- Object to compare for equality
public int hashCode()
hashCode
in class Object
public String toString()
toString
in class Object
public static ClassicCounter<String> valueOfIgnoreComments(String s)
StringKey\tdoubleValue\n
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
public static ClassicCounter<String> fromString(String s)
s
- A String representation of a Counter
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |