Class Counter<E>

  • All Implemented Interfaces:
    java.io.Serializable

    public class Counter<E>
    extends java.lang.Object
    implements java.io.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:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      Counter()  
      Counter​(Counter<? extends E> counter)  
      Counter​(java.util.Collection<? extends E> collection)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <L> Counter<L> absCounts​(Counter<L> counts)  
      boolean approxEquals​(Counter<E> other, double tol)  
      E argMax()
      Finds the key with maximum count.
      void clear()  
      boolean containsKey​(E key)
      Returns whether the counter contains the given key.
      Counter<E> difference​(Counter<E> counter)  
      double dotProduct​(Counter<E> other)  
      java.util.Set<java.util.Map.Entry<E,​java.lang.Double>> entrySet()  
      double getCount​(E key)
      Get the count of the element, or zero if the element is not in the counter.
      double getDefaultCount()  
      java.util.Collection<java.util.Map.Entry<E,​java.lang.Double>> getEntriesSortedByDecreasingCount()  
      java.util.Collection<java.util.Map.Entry<E,​java.lang.Double>> getEntriesSortedByIncreasingCount()  
      java.util.Set<java.util.Map.Entry<E,​java.lang.Double>> getEntrySet()  
      double getProbability​(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>
      void
      incrementAll​(Counter<T> counter)  
      <T extends E>
      void
      incrementAll​(Counter<T> counter, double scale)  
      void incrementAll​(java.util.Collection<? extends E> collection, double count)
      Increment each element in a given collection by a given amount.
      double incrementCount​(E key, double increment)
      Increment a key's count by the given amount.
      boolean isEmpty()
      True if there are no entries in the counter (false does not mean totalCount > 0)
      boolean isEqualTo​(Counter<E> counter)  
      java.util.Set<E> keySet()
      The elements in the counter.
      static void main​(java.lang.String[] args)  
      double max()  
      double min()  
      void normalize()
      Destructively normalize this Counter in place.
      void prune​(java.util.Set<E> toRemove)  
      void pruneExcept​(java.util.Set<E> toKeep)  
      void pruneKeysBelowThreshold​(double cutoff)  
      void put​(E key, double count, boolean keepHigher)
      Set the count for the given key if it is larger than the previous one;
      void putAll​(double d)  
      void removeKey​(E key)  
      protected void removeKeyFromEntries​(E key)  
      E sample()
      Will return a sample from the counter, will throw exception if any of the counts are < 0.0 or if the totalCount() <= 0.0
      E sample​(java.util.Random rand)
      Will return a sample from the counter, will throw exception if any of the counts are < 0.0 or if the totalCount() <= 0.0
      void scale​(double c)  
      Counter<E> scaledClone​(double c)  
      void setAllCounts​(double val)
      Sets all counts to the given value, but does not remove any keys
      void setCount​(E key, double count)
      Set the count for the given key, clobbering any previous count.
      void setDefaultCount​(double deflt)  
      void setDirty​(boolean dirty)  
      void setMaxCount​(E key, double val)
      Set's the key's count to the maximum of the current count and val.
      void setMinCount​(E key, double val)
      Set's the key's count to the minimum of the current count and val.
      int size()
      The number of entries in the counter (not the total count -- use totalCount() instead).
      Counter<E> toLogSpace()  
      java.lang.String toString()
      Returns a string representation with the keys ordered by decreasing counts.
      java.lang.String toStringSortedByKeys()  
      double totalCount()
      Finds the total of all counts in the counter.
      java.lang.Iterable<java.lang.Double> values()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Counter

        public Counter()
      • Counter

        public Counter​(Counter<? extends E> counter)
      • Counter

        public Counter​(java.util.Collection<? extends E> collection)
    • Method Detail

      • getDefaultCount

        public double getDefaultCount()
      • setDefaultCount

        public void setDefaultCount​(double deflt)
      • keySet

        public java.util.Set<E> keySet()
        The elements in the counter.
        Returns:
        set of keys
      • entrySet

        public java.util.Set<java.util.Map.Entry<E,​java.lang.Double>> 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

        public boolean containsKey​(E key)
        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

        public double getCount​(E key)
        Get the count of the element, or zero if the element is not in the counter.
        Parameters:
        key -
        Returns:
      • getProbability

        public double getProbability​(E key)
        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

        public void setCount​(E key,
                             double count)
        Set the count for the given key, clobbering any previous count.
        Parameters:
        key -
        count -
      • put

        public void put​(E key,
                        double count,
                        boolean keepHigher)
        Set the count for the given key if it is larger than the previous one;
        Parameters:
        key -
        count -
      • sample

        public E sample​(java.util.Random rand)
        Will return a sample from the counter, will throw exception if any of the counts are < 0.0 or if the totalCount() <= 0.0
        Returns:
      • sample

        public E sample()
        Will return a sample from the counter, will throw exception if any of the counts are < 0.0 or if the totalCount() <= 0.0
        Returns:
      • removeKey

        public void removeKey​(E key)
      • removeKeyFromEntries

        protected void removeKeyFromEntries​(E key)
        Parameters:
        key -
      • setMaxCount

        public void setMaxCount​(E key,
                                double val)
        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

        public void setMinCount​(E key,
                                double val)
        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

        public double incrementCount​(E key,
                                     double increment)
        Increment a key's count by the given amount.
        Parameters:
        key -
        increment -
      • incrementAll

        public void incrementAll​(java.util.Collection<? extends E> collection,
                                 double count)
        Increment each element in a given collection by a given amount.
      • incrementAll

        public <T extends E> void incrementAll​(Counter<T> counter)
      • incrementAll

        public <T extends E> void incrementAll​(Counter<T> counter,
                                               double scale)
      • 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

        public java.util.Collection<java.util.Map.Entry<E,​java.lang.Double>> getEntriesSortedByIncreasingCount()
      • getEntriesSortedByDecreasingCount

        public java.util.Collection<java.util.Map.Entry<E,​java.lang.Double>> getEntriesSortedByDecreasingCount()
      • argMax

        public E 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

        public java.lang.String toString()
        Returns a string representation with the keys ordered by decreasing counts.
        Overrides:
        toString in class java.lang.Object
        Returns:
        string representation
      • toStringSortedByKeys

        public java.lang.String toStringSortedByKeys()
      • pruneKeysBelowThreshold

        public void pruneKeysBelowThreshold​(double cutoff)
      • getEntrySet

        public java.util.Set<java.util.Map.Entry<E,​java.lang.Double>> getEntrySet()
      • isEqualTo

        public boolean isEqualTo​(Counter<E> counter)
      • main

        public static void main​(java.lang.String[] args)
      • clear

        public void clear()
      • setAllCounts

        public void setAllCounts​(double val)
        Sets all counts to the given value, but does not remove any keys
      • dotProduct

        public double dotProduct​(Counter<E> other)
      • scale

        public void scale​(double c)
      • scaledClone

        public Counter<E> scaledClone​(double c)
      • toLogSpace

        public Counter<E> toLogSpace()
      • approxEquals

        public boolean approxEquals​(Counter<E> other,
                                    double tol)
      • setDirty

        public void setDirty​(boolean dirty)
      • values

        public java.lang.Iterable<java.lang.Double> values()
      • prune

        public void prune​(java.util.Set<E> toRemove)
      • pruneExcept

        public void pruneExcept​(java.util.Set<E> toKeep)
      • absCounts

        public static <L> Counter<L> absCounts​(Counter<L> counts)
      • putAll

        public void putAll​(double d)