public class Counters
extends java.lang.Object
Counter
.
All methods that change their arguments change the first argument
(only), and have "InPlace" in their name. 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.Modifier and Type | Method and Description |
---|---|
static <E> Counter<E> |
absoluteDifference(Counter<E> c1,
Counter<E> c2)
Returns |c1 - c2|.
|
static <E> Counter<E> |
add(Counter<E> c1,
java.util.Collection<E> c2) |
static <E> Counter<E> |
add(Counter<E> c1,
Counter<E> c2) |
static <E> Counter<E> |
add(Counter<E> c1,
double value)
increments every key in the counter by value
|
static <T1,T2> TwoDimensionalCounter<T1,T2> |
add(TwoDimensionalCounter<T1,T2> arg1,
TwoDimensionalCounter<T1,T2> arg2)
For all keys (u,v) in arg1 and arg2, sets return[u,v] to be summation of both.
|
static <E> void |
addInPlace(Counter<E> target,
java.util.Collection<E> arg)
Sets each value of target to be target[k]+
num-of-times-it-occurs-in-collection if the key is present in the arg
collection.
|
static <E> void |
addInPlace(Counter<E> target,
java.util.Collection<E> arg,
double value)
Sets each value of target to be target[k]+
value*(num-of-times-it-occurs-in-collection) if the key is present in the arg
collection.
|
static <E> void |
addInPlace(Counter<E> target,
Counter<E> arg)
Sets each value of target to be target[k]+arg[k] for all keys k in arg.
|
static <E> void |
addInPlace(Counter<E> target,
Counter<E> arg,
double scale)
Sets each value of target to be target[k]+scale*arg[k] for all keys k in
target.
|
static <E> void |
addInPlace(Counter<E> target,
double value)
Increments all keys in a Counter by a specific value.
|
static <E> void |
addInPlace(double[] target,
Counter<E> arg,
Index<E> idx)
Sets each value of double[] target to be
target[idx.indexOf(k)]+a.getCount(k) for all keys k in arg
|
static <T1,T2> void |
addInPlace(TwoDimensionalCounter<T1,T2> target,
double value)
For all keys (u,v) in target, sets target[u,v] to be target[u,v] + value
|
static <T1,T2> void |
addInPlace(TwoDimensionalCounter<T1,T2> target,
TwoDimensionalCounter<T1,T2> arg)
For all keys (u,v) in arg, sets target[u,v] to be target[u,v] + arg[u,v].
|
static <T1,T2> void |
addInPlace(TwoDimensionalCounter<T1,T2> target,
TwoDimensionalCounter<T1,T2> arg,
double scale)
For all keys (u,v) in arg, sets target[u,v] to be target[u,v] + scale *
arg[u,v].
|
static <E> E |
argmax(Counter<E> c)
Finds and returns the key in the Counter with the largest count.
|
static <E> E |
argmax(Counter<E> c,
java.util.Comparator<E> tieBreaker)
Finds and returns the key in the Counter with the largest count.
|
static <E> E |
argmax(Counter<E> c,
java.util.Comparator<E> tieBreaker,
E defaultIfEmpty)
Finds and returns the key in the Counter with the largest count.
|
static <E> E |
argmin(Counter<E> c)
Finds and returns the key in this Counter with the smallest count.
|
static <E> E |
argmin(Counter<E> c,
java.util.Comparator<E> tieBreaker)
Finds and returns the key in this Counter with the smallest count.
|
static <E> double[] |
asArray(Counter<E> counter)
Convert a counter to an array, the order of the array is random
|
static <E> double[] |
asArray(Counter<E> counter,
Index<E> index)
Convert a counter to an array using a specified key index.
|
static <E> double[] |
asArray(Counter<E> counter,
Index<E> index,
int dimension)
Convert a counter to an array using a specified key index.
|
static <E> Counter<E> |
asCounter(java.util.Collection<E> c)
Takes in a Collection of something and makes a counter, incrementing once
for each object in the collection.
|
static <E> Counter<E> |
asCounter(FixedPrioritiesPriorityQueue<E> p)
Returns a counter whose keys are the elements in this priority queue, and
whose counts are the priorities in this queue.
|
static <E> java.util.Map<E,java.lang.Double> |
asMap(Counter<E> counter)
Returns a map view of the given counter.
|
static <E,C extends Counter<E>> |
asNormalizedCounter(C c)
L1 normalize a counter.
|
static <E> Counter<E> |
average(Counter<E> c1,
Counter<E> c2)
Returns a new Counter with counts averaged from the two given Counters.
|
static <E> double |
cosine(Counter<E> c1,
Counter<E> c2) |
static <E> double |
crossEntropy(Counter<E> from,
Counter<E> to)
Note that this implementation doesn't normalize the "from" Counter.
|
static <E> java.util.List<E> |
deleteOutofRange(Counter<E> c,
int top,
int bottom)
Delete 'top' and 'bottom' number of elements from the top and bottom
respectively
|
static <T> ClassicCounter<T> |
deserializeCounter(java.lang.String filename) |
static ClassicCounter<java.lang.String> |
deserializeStringCounter(java.lang.String filename)
Read a Counter from a serialized file
|
static <T> Counter<T> |
diff(Counter<T> goldFeatures,
Counter<T> guessedFeatures) |
static <E> void |
divideInPlace(Counter<E> target,
Counter<E> denominator)
Divides every non-zero count in target by the corresponding value in the
denominator Counter.
|
static <E> Counter<E> |
divideInPlace(Counter<E> target,
double divisor)
Divides each value in target by the given divisor, in place.
|
static <A,B> void |
divideInPlace(TwoDimensionalCounter<A,B> counter,
double divisor) |
static <E> Counter<E> |
division(Counter<E> c1,
Counter<E> c2)
Returns c1 divided by c2.
|
static <E> Counter<E> |
divisionNonNaN(Counter<E> c1,
Counter<E> c2)
Returns c1 divided by c2.
|
static <E> double |
dotProduct(Counter<E> c1,
Counter<E> c2)
Returns the product of c1 and c2.
|
static <E> double |
dotProduct(Counter<E> c,
double[] a,
Index<E> idx)
Returns the product of Counter c and double[] a, using Index idx to map
entries in C onto a.
|
static <E> void |
dotProductInPlace(Counter<E> target,
Counter<E> term)
Multiplies every count in target by the corresponding value in the term
Counter.
|
static <E> void |
ensureKeys(Counter<E> t,
java.util.Collection<E> keys,
double value)
ensures that counter t has all keys in keys.
|
static <E> double |
entropy(Counter<E> c)
Calculates the entropy of the given counter (in bits).
|
static <E> boolean |
equals(Counter<E> o1,
Counter<E> o2)
Default equality comparison for two counters potentially backed by
alternative implementations.
|
static <E> boolean |
equals(Counter<E> o1,
Counter<E> o2,
double tolerance)
Equality comparison between two counters, allowing for a tolerance fudge factor.
|
static <T> Counter<T> |
exp(Counter<T> c) |
static <T> void |
expInPlace(Counter<T> c) |
static <E,E2> Counter<E> |
flatten(java.util.Map<E2,Counter<E>> hier) |
static <E,N extends java.lang.Number> |
fromMap(java.util.Map<E,N> map)
Returns a counter view of the given map.
|
static <E,N extends java.lang.Number> |
fromMap(java.util.Map<E,N> map,
java.lang.Class<N> type)
Returns a counter view of the given map.
|
static <E> Counter<E> |
getCopy(Counter<E> originalCounter) |
static <E> Counter<java.lang.Double> |
getCountCounts(Counter<E> c) |
static <E> Counter<E> |
getCounts(Counter<E> c,
java.util.Collection<E> keys) |
static <E> Counter<E> |
getFCounter(Counter<E> precision,
Counter<E> recall,
double beta) |
static <E> int |
hIndex(Counter<E> citationCounts)
Calculate h-Index (Hirsch, 2005) of an author.
|
static <E> Counter<E> |
intersection(Counter<E> c1,
Counter<E> c2)
Returns a counter that is the intersection of c1 and c2.
|
static <E> boolean |
isFinite(Counter<E> counts)
Returns true if the given counter contains only finite, non-NaN values.
|
static <E> boolean |
isUniformDistribution(Counter<E> distribution,
double tolerance)
Check if this counter is a uniform distribution.
|
static <E> double |
jaccardCoefficient(Counter<E> c1,
Counter<E> c2)
Returns the Jaccard Coefficient of the two counters.
|
static <E> double |
jensenShannonDivergence(Counter<E> c1,
Counter<E> c2)
Calculates the Jensen-Shannon divergence between the two counters.
|
static <E> java.util.Set<E> |
keysAbove(Counter<E> c,
double countThreshold)
Returns the set of keys whose counts are at or above the given threshold.
|
static <E> java.util.Set<E> |
keysAt(Counter<E> c,
double count)
Returns the set of keys that have exactly the given count.
|
static <E> java.util.Set<E> |
keysBelow(Counter<E> c,
double countThreshold)
Returns the set of keys whose counts are at or below the given threshold.
|
static <E> double |
klDivergence(Counter<E> from,
Counter<E> to)
Calculates the KL divergence between the two counters.
|
static <E,C extends Counter<E>> |
L1Norm(C c)
Return the L1 norm of a counter.
|
static <E,C extends Counter<E>> |
L2Norm(C c)
Return the l2 norm (Euclidean vector length) of a Counter.
|
static <E,C extends Counter<E>> |
L2Normalize(C c)
L2 normalize a counter.
|
static <E> Counter<E> |
L2NormalizeInPlace(Counter<E> c)
L2 normalize a counter in place.
|
static <E> Counter<E> |
linearCombination(Counter<E> c1,
double w1,
Counter<E> c2,
double w2)
Returns a Counter which is a weighted average of c1 and c2.
|
static <T1,T2> TwoDimensionalCounter<T1,T2> |
load2DCounter(java.lang.String filename,
java.lang.Class<T1> t1,
java.lang.Class<T2> t2) |
static <E> ClassicCounter<E> |
loadCounter(java.lang.String filename,
java.lang.Class<E> c)
Loads a Counter from a text file.
|
static <T1,T2> void |
loadIncInto2DCounter(java.lang.String filename,
java.lang.Class<T1> t1,
java.lang.Class<T2> t2,
TwoDimensionalCounterInterface<T1,T2> tdc) |
static <E> IntCounter<E> |
loadIntCounter(java.lang.String filename,
java.lang.Class<E> c)
Loads a Counter from a text file.
|
static <T1,T2> void |
loadInto2DCounter(java.lang.String filename,
java.lang.Class<T1> t1,
java.lang.Class<T2> t2,
TwoDimensionalCounter<T1,T2> tdc) |
static <E> void |
logInPlace(Counter<E> target) |
static <E> void |
logNormalizeInPlace(Counter<E> c)
Transform log space values into a probability distribution in place.
|
static <E> double |
logSum(Counter<E> c)
Returns ArrayMath.logSum of the values in this counter.
|
static <E> double |
max(Counter<E> c)
Returns the value of the maximum entry in this counter.
|
static <E> double |
max(Counter<E> c,
double valueIfEmpty)
Returns the value of the maximum entry in this counter.
|
static <E> void |
maxInPlace(Counter<E> target,
Counter<E> other)
Places the maximum of first and second keys values in the first counter.
|
static <E> double |
mean(Counter<E> c)
Returns the mean of all the counts (totalCount/size).
|
static <E> double |
min(Counter<E> c)
Returns the value of the smallest entry in this counter.
|
static <E> void |
minInPlace(Counter<E> target,
Counter<E> other)
Places the minimum of first and second keys values in the first counter.
|
static <E> Counter<E> |
multiplyInPlace(Counter<E> target,
Counter<E> mult)
Multiplies each value in target by the count of the key in mult, in place.
|
static <E> Counter<E> |
multiplyInPlace(Counter<E> target,
double multiplier)
Multiplies each value in target by the given multiplier, in place.
|
static <E> void |
normalize(Counter<E> target)
Normalizes the target counter in-place, so the sum of the resulting values
equals 1.
|
static <E,F> void |
normalize(TwoDimensionalCounter<E,F> target)
Normalizes the target counter in-place, so the sum of the resulting values
equals 1.
|
static <E> double |
optimizedDotProduct(Counter<E> c1,
Counter<E> c2)
This method does not check entries for NAN or INFINITY values in the
doubles returned.
|
static <E> double |
pearsonsCorrelationCoefficient(Counter<E> x,
Counter<E> y) |
static <E,C extends Counter<E>> |
perturbCounts(C c,
java.util.Random random,
double p) |
static <T1,T2> double |
pointwiseMutualInformation(Counter<T1> var1Distribution,
Counter<T2> var2Distribution,
Counter<Pair<T1,T2>> jointDistribution,
Pair<T1,T2> values) |
static <T> Counter<T> |
pow(Counter<T> c,
double temp) |
static <T> void |
powInPlace(Counter<T> c,
double temp) |
static <E> Counter<E> |
powNormalized(Counter<E> c,
double temp)
Returns a counter where each element corresponds to the normalized count of
the corresponding element in c raised to the given power.
|
static <E> void |
printCounterComparison(Counter<E> a,
Counter<E> b)
Great for debugging.
|
static <E> void |
printCounterComparison(Counter<E> a,
Counter<E> b,
java.io.PrintStream out)
Great for debugging.
|
static <E> void |
printCounterComparison(Counter<E> a,
Counter<E> b,
java.io.PrintWriter out)
Prints one or more lines (with a newline at the end) describing the
difference between the two Counters.
|
static <E extends java.lang.Comparable<E>> |
printCounterSortedByKeys(Counter<E> c) |
static <E> Counter<E> |
product(Counter<E> c1,
Counter<E> c2)
Returns the product of c1 and c2.
|
static <E> void |
removeKeys(Counter<E> counter,
java.util.Collection<E> removeKeysCollection)
Removes all entries with keys in the given collection
|
static <E,F> void |
removeKeys(TwoDimensionalCounter<E,F> counter,
java.util.Collection<E> removeKeysCollection)
Removes all entries with keys (first key set) in the given collection
|
static <E> E |
restrictedArgMax(Counter<E> c,
java.util.Collection<E> restriction) |
static <E> java.util.Set<E> |
retainAbove(Counter<E> counter,
double countThreshold)
Removes all entries with counts below the given threshold, returning the
set of removed entries.
|
static <E1,E2> java.util.Set<Pair<E1,E2>> |
retainAbove(TwoDimensionalCounter<E1,E2> counter,
double countThreshold)
Removes all entries with counts below the given threshold, returning the
set of removed entries.
|
static <E> Counter<E> |
retainBelow(Counter<E> counter,
double countMaxThreshold)
Removes all entries with counts above the given threshold, returning the
set of removed entries.
|
static <E> java.util.List<E> |
retainBottom(Counter<E> c,
int num)
Removes all entries from c except for the bottom
num . |
static <E> java.util.Set<E> |
retainKeys(Counter<E> counter,
java.util.Collection<E> matchKeys)
Removes all entries with keys that does not match the given set of keys.
|
static <E> void |
retainKeys(Counter<E> counter,
java.util.function.Predicate<E> retainFunction) |
static java.util.Set<java.lang.String> |
retainMatchingKeys(Counter<java.lang.String> counter,
java.util.List<java.util.regex.Pattern> matchPatterns)
Removes all entries with keys that does not match one of the given patterns.
|
static <E> java.util.Set<E> |
retainNonZeros(Counter<E> counter)
Removes all entries with 0 count in the counter, returning the set of
removed entries.
|
static <E> void |
retainTop(Counter<E> c,
int num)
Removes all entries from c except for the top
num . |
static <E extends java.lang.Comparable<E>> |
retainTopKeyComparable(Counter<E> c,
int num)
Removes all entries from c except for the top
num . |
static <E> void |
retainTopMass(Counter<E> counter,
double thresholdCount)
Retains the minimal set of top keys such that their count sum is more than thresholdCount.
|
static <E,C extends Counter<E>> |
saferL2Norm(C c)
For counters with large # of entries, this scales down each entry in the
sum, to prevent an extremely large sum from building up and overwhelming
the max double.
|
static <E,C extends Counter<E>> |
saferL2Normalize(C c)
L2 normalize a counter, using the "safer" L2 normalizer.
|
static <T> T |
sample(Counter<T> c)
Does not assumes c is normalized.
|
static <T> T |
sample(Counter<T> c,
java.util.Random rand)
Does not assumes c is normalized.
|
static <T1,T2> void |
save2DCounter(TwoDimensionalCounter<T1,T2> tdc,
java.lang.String filename) |
static <T1,T2> void |
save2DCounterSorted(TwoDimensionalCounterInterface<T1,T2> tdc,
java.lang.String filename) |
static <E> void |
saveCounter(Counter<E> c,
java.io.OutputStream stream)
Saves a Counter as one key/count pair per line separated by white space to
the given OutputStream.
|
static <E> void |
saveCounter(Counter<E> c,
java.lang.String filename)
Saves a Counter to a text file.
|
static <E,C extends Counter<E>> |
scale(C c,
double s)
Returns a new Counter which is scaled by the given scale factor.
|
static <T1,T2> TwoDimensionalCounter<T1,T2> |
scale(TwoDimensionalCounter<T1,T2> c,
double d)
Creates a new TwoDimensionalCounter where all the counts are scaled by d.
|
static <T> void |
serializeCounter(Counter<T> c,
java.lang.String filename) |
static void |
serializeStringCounter(Counter<java.lang.String> c,
java.lang.String filename) |
static void |
serializeStringCounter(Counter<java.lang.String> c,
java.lang.String filename,
double minMagnitude)
Serialize a counter into an efficient string TSV
|
static <E> double |
skewDivergence(Counter<E> c1,
Counter<E> c2,
double skew)
Calculates the skew divergence between the two counters.
|
static <E> double |
spearmanRankCorrelation(Counter<E> x,
Counter<E> y) |
static <E> double |
standardDeviation(Counter<E> c) |
static <E> void |
subtractInPlace(Counter<E> target,
Counter<E> arg)
Sets each value of target to be target[k]-arg[k] for all keys k in target.
|
static <E> void |
subtractInPlace(double[] target,
Counter<E> arg,
Index<E> idx)
Sets each value of double[] target to be
target[idx.indexOf(k)]-a.getCount(k) for all keys k in arg
|
static <E> double |
sumEntries(Counter<E> c1,
java.util.Collection<E> entries) |
static <E,C extends Counter<E>> |
sumSquares(C c)
Return the sum of squares (squared L2 norm).
|
static <E,C extends Counter<E>> |
tfLogScale(C c,
double base)
Returns a new Counter which is the input counter with log tf scaling
|
static <E> java.lang.String |
toBiggestValuesFirstString(Counter<E> c) |
static <E> java.lang.String |
toBiggestValuesFirstString(Counter<E> c,
int k) |
static <T> java.lang.String |
toBiggestValuesFirstString(Counter<java.lang.Integer> c,
int k,
Index<T> index) |
static <E> java.util.Comparator<E> |
toComparator(Counter<E> counter)
Returns a comparator backed by this counter: two objects are compared by
their associated values stored in the counter.
|
static <E> java.util.Comparator<E> |
toComparator(Counter<E> counter,
boolean ascending,
boolean useMagnitude)
Returns a comparator suitable for sorting this Counter's keys or entries by
their respective value or magnitude (by absolute value).
|
static <E> java.util.Comparator<E> |
toComparatorDescending(Counter<E> counter)
Returns a comparator backed by this counter: two objects are compared by
their associated values stored in the counter.
|
static <E extends java.lang.Comparable<E>> |
toComparatorWithKeys(Counter<E> counter)
Returns a comparator backed by this counter: two objects are compared by
their associated values stored in the counter.
|
static <T> Counter<T> |
toCounter(double[] counts,
Index<T> index) |
static <E> Counter<E> |
toCounter(java.util.Map<java.lang.Integer,? extends java.lang.Number> counts,
Index<E> index)
Turns the given map and index into a counter instance.
|
static <E> java.util.List<Pair<E,java.lang.Double>> |
toDescendingMagnitudeSortedListWithCounts(Counter<E> c) |
static <E> java.util.List<E> |
topKeys(Counter<E> t,
int topNum) |
static <E> java.util.List<Pair<E,java.lang.Double>> |
topKeysWithCounts(Counter<E> t,
int topNum) |
static <E> PriorityQueue<E> |
toPriorityQueue(Counter<E> c)
Returns a
PriorityQueue whose elements are
the keys of Counter c, and the score of each key in c becomes its priority. |
static <E> IntCounter<E> |
toRankCounter(Counter<E> c)
Converts a counter to ranks; ranks start from 0
|
static <T extends java.lang.Comparable<T>> |
toSortedByKeysString(Counter<T> counter,
java.lang.String itemFormat,
java.lang.String joiner,
java.lang.String wrapperFormat)
Returns a string representation of a Counter, where (key, value) pairs are
sorted by key, and formatted as specified.
|
static <E> java.util.List<E> |
toSortedList(Counter<E> c)
A List of the keys in c, sorted from highest count to lowest.
|
static <E> java.util.List<E> |
toSortedList(Counter<E> c,
boolean ascending)
A List of the keys in c, sorted from highest count to lowest.
|
static <E extends java.lang.Comparable<E>> |
toSortedListKeyComparable(Counter<E> c)
A List of the keys in c, sorted from highest count to lowest.
|
static <E> java.util.List<Pair<E,java.lang.Double>> |
toSortedListWithCounts(Counter<E> c)
A List of the keys in c, sorted from highest count to lowest, paired with
counts
|
static <E> java.util.List<Pair<E,java.lang.Double>> |
toSortedListWithCounts(Counter<E> c,
java.util.Comparator<Pair<E,java.lang.Double>> comparator)
A List of the keys in c, sorted by the given comparator, paired with
counts.
|
static <T> java.lang.String |
toSortedString(Counter<T> counter,
int k,
java.lang.String itemFormat,
java.lang.String joiner)
Returns a string representation of a Counter, displaying the keys and their
counts in decreasing order of count.
|
static <T> java.lang.String |
toSortedString(Counter<T> counter,
int k,
java.lang.String itemFormat,
java.lang.String joiner,
java.lang.String wrapperFormat)
Returns a string representation of a Counter, displaying the keys and their
counts in decreasing order of count.
|
static <E> java.lang.String |
toString(Counter<E> counter,
int maxKeysToPrint)
Returns a string representation which includes no more than the
maxKeysToPrint elements with largest counts.
|
static <E> java.lang.String |
toString(Counter<E> counter,
java.text.NumberFormat nf) |
static <E> java.lang.String |
toString(Counter<E> counter,
java.text.NumberFormat nf,
java.lang.String preAppend,
java.lang.String postAppend,
java.lang.String keyValSeparator,
java.lang.String itemSeparator)
Pretty print a Counter.
|
static <E> Counter<E> |
toTiedRankCounter(Counter<E> c)
Converts a counter to tied ranks; ranks start from 1
|
static <E> java.lang.String |
toVerticalString(Counter<E> c) |
static <E> java.lang.String |
toVerticalString(Counter<E> c,
int k) |
static <E> java.lang.String |
toVerticalString(Counter<E> c,
int k,
java.lang.String fmt) |
static <E> java.lang.String |
toVerticalString(Counter<E> c,
int k,
java.lang.String fmt,
boolean swap)
Returns a
String representation of the k keys
with the largest counts in the given Counter , using the given
format string. |
static <E> java.lang.String |
toVerticalString(Counter<E> c,
java.lang.String fmt) |
static <T1,T2> Counter<T2> |
transform(Counter<T1> c,
java.util.function.Function<T1,T2> f)
Returns the counter with keys modified according to function F.
|
static <E> void |
transformValuesInPlace(Counter<E> counter,
java.util.function.DoubleUnaryOperator func) |
static <T1,T2> Counter<T2> |
transformWithValuesAdd(Counter<T1> c,
java.util.function.Function<T1,T2> f)
Returns the counter with keys modified according to function F.
|
static <E,C extends Counter<E>> |
union(C c1,
C c2)
Returns a Counter that is the union of the two Counters passed in (counts
are added).
|
static <T> Counter<T> |
unmodifiableCounter(Counter<T> counter)
Returns unmodifiable view of the counter.
|
public static <E> double logSum(Counter<E> c)
c
- Argument counter (which is not modified)public static <E> void logNormalizeInPlace(Counter<E> c)
c
- The Counter to log normalize in placepublic static <E> double max(Counter<E> c)
c
- The Counter to find the max ofpublic static <E> double max(Counter<E> c, double valueIfEmpty)
c
- The Counter to find the max ofvalueIfEmpty
- The value to return if this counter is empty (i.e., the maximum is not well defined.public static <E> Counter<E> asCounter(java.util.Collection<E> c)
c
- The Collection to turn into a counterpublic static <E> double min(Counter<E> c)
c
- The Counter (not modified)public static <E> E argmax(Counter<E> c)
c
- The Counterpublic static <E> E argmin(Counter<E> c)
c
- The Counterpublic static <E> E argmax(Counter<E> c, java.util.Comparator<E> tieBreaker)
c
- The CountertieBreaker
- the tie breaker for when elements have the same value.public static <E> E argmax(Counter<E> c, java.util.Comparator<E> tieBreaker, E defaultIfEmpty)
c
- The CountertieBreaker
- the tie breaker for when elements have the same value.defaultIfEmpty
- The value to return if the counter is empty.public static <E> E argmin(Counter<E> c, java.util.Comparator<E> tieBreaker)
c
- The Counterpublic static <E> double mean(Counter<E> c)
c
- The Counter to find the mean of.public static <E> double standardDeviation(Counter<E> c)
public static <E> void addInPlace(Counter<E> target, Counter<E> arg, double scale)
target
- A Counter that is modifiedarg
- The Counter whose contents are added to targetscale
- How the arg Counter is scaled before being addedpublic static <E> void addInPlace(Counter<E> target, Counter<E> arg)
public static <E> void addInPlace(double[] target, Counter<E> arg, Index<E> idx)
public static <T1,T2> TwoDimensionalCounter<T1,T2> add(TwoDimensionalCounter<T1,T2> arg1, TwoDimensionalCounter<T1,T2> arg2)
T1
- T2
- public static <T1,T2> void addInPlace(TwoDimensionalCounter<T1,T2> target, TwoDimensionalCounter<T1,T2> arg, double scale)
T1
- T2
- public static <T1,T2> void addInPlace(TwoDimensionalCounter<T1,T2> target, TwoDimensionalCounter<T1,T2> arg)
T1
- T2
- public static <E> void addInPlace(Counter<E> target, java.util.Collection<E> arg, double value)
public static <T1,T2> void addInPlace(TwoDimensionalCounter<T1,T2> target, double value)
T1
- T2
- public static <E> void addInPlace(Counter<E> target, java.util.Collection<E> arg)
public static <E> void addInPlace(Counter<E> target, double value)
public static <E> void subtractInPlace(Counter<E> target, Counter<E> arg)
public static <E> void subtractInPlace(double[] target, Counter<E> arg, Index<E> idx)
public static <E> void divideInPlace(Counter<E> target, Counter<E> denominator)
public static <E> void dotProductInPlace(Counter<E> target, Counter<E> term)
public static <E> Counter<E> divideInPlace(Counter<E> target, double divisor)
target
- The values in this Counter will be changed throughout by the
multiplierdivisor
- The number by which to change each number in the Counterpublic static <E> Counter<E> multiplyInPlace(Counter<E> target, double multiplier)
target
- The values in this Counter will be multiplied by the
multipliermultiplier
- The number by which to change each number in the Counterpublic static <E> Counter<E> multiplyInPlace(Counter<E> target, Counter<E> mult)
target
- The countermult
- The counter you want to multiply with targetpublic static <E> void normalize(Counter<E> target)
E
- Type of elements in Counterpublic static <E,C extends Counter<E>> C asNormalizedCounter(C c)
c
- The Counter
to be L1 normalized. This counter is not
modified.public static <E,F> void normalize(TwoDimensionalCounter<E,F> target)
E
- Type of elements in TwoDimensionalCounterF
- Type of elements in TwoDimensionalCounterpublic static <E> void logInPlace(Counter<E> target)
public static <E> java.util.List<E> deleteOutofRange(Counter<E> c, int top, int bottom)
public static <E> void retainTop(Counter<E> c, int num)
num
.public static <E extends java.lang.Comparable<E>> void retainTopKeyComparable(Counter<E> c, int num)
num
.public static <E> java.util.List<E> retainBottom(Counter<E> c, int num)
num
.public static <E> java.util.Set<E> retainNonZeros(Counter<E> counter)
public static <E> java.util.Set<E> retainAbove(Counter<E> counter, double countThreshold)
counter
- The counter.countThreshold
- The minimum count for an entry to be kept. Entries (strictly) less
than this threshold are discarded.public static <E1,E2> java.util.Set<Pair<E1,E2>> retainAbove(TwoDimensionalCounter<E1,E2> counter, double countThreshold)
counter
- The counter.countThreshold
- The minimum count for an entry to be kept. Entries (strictly) less
than this threshold are discarded.public static <E> Counter<E> retainBelow(Counter<E> counter, double countMaxThreshold)
counter
- The counter.countMaxThreshold
- The maximum count for an entry to be kept. Entries (strictly) more
than this threshold are discarded.public static java.util.Set<java.lang.String> retainMatchingKeys(Counter<java.lang.String> counter, java.util.List<java.util.regex.Pattern> matchPatterns)
counter
- The counter.matchPatterns
- pattern for key to matchpublic static <E> java.util.Set<E> retainKeys(Counter<E> counter, java.util.Collection<E> matchKeys)
counter
- The countermatchKeys
- Keys to matchpublic static <E> void removeKeys(Counter<E> counter, java.util.Collection<E> removeKeysCollection)
E
- counter
- removeKeysCollection
- public static <E,F> void removeKeys(TwoDimensionalCounter<E,F> counter, java.util.Collection<E> removeKeysCollection)
E
- counter
- removeKeysCollection
- public static <E> java.util.Set<E> keysAbove(Counter<E> c, double countThreshold)
c
- The Counter to examinecountThreshold
- Items equal to or above this number are keptpublic static <E> java.util.Set<E> keysBelow(Counter<E> c, double countThreshold)
public static <E> java.util.Set<E> keysAt(Counter<E> c, double count)
public static <T1,T2> Counter<T2> transform(Counter<T1> c, java.util.function.Function<T1,T2> f)
public static <T1,T2> Counter<T2> transformWithValuesAdd(Counter<T1> c, java.util.function.Function<T1,T2> f)
public static <E> java.util.Comparator<E> toComparator(Counter<E> counter)
counter
- The Counter whose values are used for ordering the keyspublic static <E extends java.lang.Comparable<E>> java.util.Comparator<E> toComparatorWithKeys(Counter<E> counter)
counter
- The Counter whose values are used for ordering the keyspublic static <E> java.util.Comparator<E> toComparatorDescending(Counter<E> counter)
counter
- The Counter whose values are used for ordering the keyspublic static <E> java.util.Comparator<E> toComparator(Counter<E> counter, boolean ascending, boolean useMagnitude)
Sample usage:
Counter c = new Counter(); // add to the counter... List biggestAbsKeys = new ArrayList(c.keySet()); Collections.sort(biggestAbsKeys, Counters.comparator(c, false, true)); List smallestEntries = new ArrayList(c.entrySet()); Collections.sort(smallestEntries, Counters.comparator(c, true, false));
public static <E> java.util.List<E> toSortedList(Counter<E> c)
public static <E> java.util.List<E> toSortedList(Counter<E> c, boolean ascending)
public static <E extends java.lang.Comparable<E>> java.util.List<E> toSortedListKeyComparable(Counter<E> c)
public static <E> IntCounter<E> toRankCounter(Counter<E> c)
public static <E> Counter<E> toTiedRankCounter(Counter<E> c)
public static <E> java.util.List<Pair<E,java.lang.Double>> toDescendingMagnitudeSortedListWithCounts(Counter<E> c)
public static <E> java.util.List<Pair<E,java.lang.Double>> toSortedListWithCounts(Counter<E> c)
public static <E> java.util.List<Pair<E,java.lang.Double>> toSortedListWithCounts(Counter<E> c, java.util.Comparator<Pair<E,java.lang.Double>> comparator)
public static <E> PriorityQueue<E> toPriorityQueue(Counter<E> c)
PriorityQueue
whose elements are
the keys of Counter c, and the score of each key in c becomes its priority.c
- Input Counterpublic static <E,C extends Counter<E>> C union(C c1, C c2)
public static <E> Counter<E> intersection(Counter<E> c1, Counter<E> c2)
public static <E> double jaccardCoefficient(Counter<E> c1, Counter<E> c2)
public static <E> Counter<E> product(Counter<E> c1, Counter<E> c2)
public static <E> double dotProduct(Counter<E> c1, Counter<E> c2)
public static <E> double dotProduct(Counter<E> c, double[] a, Index<E> idx)
public static <E> double sumEntries(Counter<E> c1, java.util.Collection<E> entries)
public static <E> Counter<E> add(Counter<E> c1, double value)
public static <E> double optimizedDotProduct(Counter<E> c1, Counter<E> c2)
E
- c1
- c2
- public static <E> Counter<E> absoluteDifference(Counter<E> c1, Counter<E> c2)
public static <E> Counter<E> division(Counter<E> c1, Counter<E> c2)
public static <E> Counter<E> divisionNonNaN(Counter<E> c1, Counter<E> c2)
public static <E> double entropy(Counter<E> c)
public static <E> double crossEntropy(Counter<E> from, Counter<E> to)
public static <E> double klDivergence(Counter<E> from, Counter<E> to)
public static <E> double jensenShannonDivergence(Counter<E> c1, Counter<E> c2)
public static <E> double skewDivergence(Counter<E> c1, Counter<E> c2, double skew)
public static <E,C extends Counter<E>> double L2Norm(C c)
c
- The Counterpublic static <E,C extends Counter<E>> double sumSquares(C c)
c
- The Counterpublic static <E,C extends Counter<E>> double L1Norm(C c)
c
- The Counterpublic static <E,C extends Counter<E>> C L2Normalize(C c)
c
- The Counter
to be L2 normalized. This counter is not
modified.public static <E> Counter<E> L2NormalizeInPlace(Counter<E> c)
c
- The Counter
to be L2 normalized. This counter is modifiedpublic static <E,C extends Counter<E>> double saferL2Norm(C c)
E
- C
- public static <E,C extends Counter<E>> C saferL2Normalize(C c)
c
- The Counter
to be L2 normalized. This counter is not
modified.public static <E> Counter<E> average(Counter<E> c1, Counter<E> c2)
public static <E> Counter<E> linearCombination(Counter<E> c1, double w1, Counter<E> c2, double w2)
public static <T1,T2> double pointwiseMutualInformation(Counter<T1> var1Distribution, Counter<T2> var2Distribution, Counter<Pair<T1,T2>> jointDistribution, Pair<T1,T2> values)
public static <E> int hIndex(Counter<E> citationCounts)
citationCounts
- Citation counts for each of the articles written by the author.
The keys can be anything, but the values should be integers.public static <E,C extends Counter<E>> C perturbCounts(C c, java.util.Random random, double p)
public static <E> void printCounterComparison(Counter<E> a, Counter<E> b)
public static <E> void printCounterComparison(Counter<E> a, Counter<E> b, java.io.PrintStream out)
public static <E> void printCounterComparison(Counter<E> a, Counter<E> b, java.io.PrintWriter out)
public static <E,C extends Counter<E>> C scale(C c, double s)
c
- The counter to scale. It is not changeds
- The constant to scale the counter bypublic static <E,C extends Counter<E>> C tfLogScale(C c, double base)
c
- The counter to scale. It is not changedbase
- The base of the logarithm used for tf scaling by 1 + log tfpublic static <E extends java.lang.Comparable<E>> void printCounterSortedByKeys(Counter<E> c)
public static <E> ClassicCounter<E> loadCounter(java.lang.String filename, java.lang.Class<E> c) throws java.lang.RuntimeException
filename
- The path to the file to load the Counter fromc
- The Class to instantiate each member of the set. Must have a
String constructor.java.lang.RuntimeException
public static <E> IntCounter<E> loadIntCounter(java.lang.String filename, java.lang.Class<E> c) throws java.lang.Exception
filename
- The path to the file to load the Counter fromc
- The Class to instantiate each member of the set. Must have a
String constructor.java.lang.Exception
public static <E> void saveCounter(Counter<E> c, java.io.OutputStream stream)
public static <E> void saveCounter(Counter<E> c, java.lang.String filename) throws java.io.IOException
java.io.IOException
public static <T1,T2> TwoDimensionalCounter<T1,T2> load2DCounter(java.lang.String filename, java.lang.Class<T1> t1, java.lang.Class<T2> t2) throws java.lang.RuntimeException
java.lang.RuntimeException
public static <T1,T2> void loadInto2DCounter(java.lang.String filename, java.lang.Class<T1> t1, java.lang.Class<T2> t2, TwoDimensionalCounter<T1,T2> tdc) throws java.lang.RuntimeException
java.lang.RuntimeException
public static <T1,T2> void loadIncInto2DCounter(java.lang.String filename, java.lang.Class<T1> t1, java.lang.Class<T2> t2, TwoDimensionalCounterInterface<T1,T2> tdc) throws java.lang.RuntimeException
java.lang.RuntimeException
public static <T1,T2> void save2DCounter(TwoDimensionalCounter<T1,T2> tdc, java.lang.String filename) throws java.io.IOException
java.io.IOException
public static <T1,T2> void save2DCounterSorted(TwoDimensionalCounterInterface<T1,T2> tdc, java.lang.String filename) throws java.io.IOException
java.io.IOException
public static void serializeStringCounter(Counter<java.lang.String> c, java.lang.String filename, double minMagnitude) throws java.io.IOException
c
- The counter to serializefilename
- The file to serialize tominMagnitude
- Ignore values under this magnitudejava.io.IOException
deserializeStringCounter(String)
public static void serializeStringCounter(Counter<java.lang.String> c, java.lang.String filename) throws java.io.IOException
java.io.IOException
serializeStringCounter(Counter, String, double)
public static ClassicCounter<java.lang.String> deserializeStringCounter(java.lang.String filename) throws java.io.IOException
filename
- The file to read fromjava.io.IOException
serializeStringCounter(Counter, String, double)
public static <T> void serializeCounter(Counter<T> c, java.lang.String filename) throws java.io.IOException
java.io.IOException
public static <T> ClassicCounter<T> deserializeCounter(java.lang.String filename) throws java.lang.Exception
java.lang.Exception
public static <T> java.lang.String toSortedString(Counter<T> counter, int k, java.lang.String itemFormat, java.lang.String joiner, java.lang.String wrapperFormat)
counter
- A Counter.k
- The number of keys to include. Use Integer.MAX_VALUE to include
all keys.itemFormat
- The format string for key/count pairs, where the key is first and
the value is second. To display the value first, use argument
indices, e.g. "%2$f %1$s".joiner
- The string used between pairs of key/value strings.wrapperFormat
- The format string for wrapping text around the joined items, where
the joined item string value is "%s".public static <T> java.lang.String toSortedString(Counter<T> counter, int k, java.lang.String itemFormat, java.lang.String joiner)
counter
- A Counter.k
- The number of keys to include. Use Integer.MAX_VALUE to include
all keys.itemFormat
- The format string for key/count pairs, where the key is first and
the value is second. To display the value first, use argument
indices, e.g. "%2$f %1$s".joiner
- The string used between pairs of key/value strings.public static <T extends java.lang.Comparable<T>> java.lang.String toSortedByKeysString(Counter<T> counter, java.lang.String itemFormat, java.lang.String joiner, java.lang.String wrapperFormat)
counter
- The Counter.itemFormat
- The format string for key/count pairs, where the key is first and
the value is second. To display the value first, use argument
indices, e.g. "%2$f %1$s".joiner
- The string used between pairs of key/value strings.wrapperFormat
- The format string for wrapping text around the joined items, where
the joined item string value is "%s".public static <E> java.lang.String toString(Counter<E> counter, int maxKeysToPrint)
counter
- The CountermaxKeysToPrint
- Max keys to printpublic static <E> java.lang.String toString(Counter<E> counter, java.text.NumberFormat nf)
public static <E> java.lang.String toString(Counter<E> counter, java.text.NumberFormat nf, java.lang.String preAppend, java.lang.String postAppend, java.lang.String keyValSeparator, java.lang.String itemSeparator)
public static <E> java.lang.String toBiggestValuesFirstString(Counter<E> c)
public static <E> java.lang.String toBiggestValuesFirstString(Counter<E> c, int k)
public static <T> java.lang.String toBiggestValuesFirstString(Counter<java.lang.Integer> c, int k, Index<T> index)
public static <E> java.lang.String toVerticalString(Counter<E> c)
public static <E> java.lang.String toVerticalString(Counter<E> c, int k)
public static <E> java.lang.String toVerticalString(Counter<E> c, java.lang.String fmt)
public static <E> java.lang.String toVerticalString(Counter<E> c, int k, java.lang.String fmt)
public static <E> java.lang.String toVerticalString(Counter<E> c, int k, java.lang.String fmt, boolean swap)
String
representation of the k
keys
with the largest counts in the given Counter
, using the given
format string.c
- A Counterk
- How many keys to printfmt
- A format string, such as "%.0f\t%s" (do not include final "%n").
If swap is false, you will get val, key as arguments, if true, key, val.swap
- Whether the count should appear after the keypublic static <E> E restrictedArgMax(Counter<E> c, java.util.Collection<E> restriction)
public static <E> Counter<E> toCounter(java.util.Map<java.lang.Integer,? extends java.lang.Number> counts, Index<E> index)
public static <E> double[] asArray(Counter<E> counter, Index<E> index)
public static <E> double[] asArray(Counter<E> counter, Index<E> index, int dimension)
public static <E> double[] asArray(Counter<E> counter)
public static <T1,T2> TwoDimensionalCounter<T1,T2> scale(TwoDimensionalCounter<T1,T2> c, double d)
public static <T> T sample(Counter<T> c, java.util.Random rand)
public static <T> T sample(Counter<T> c)
public static <E> Counter<E> powNormalized(Counter<E> c, double temp)
public static <T> void powInPlace(Counter<T> c, double temp)
public static <T> void expInPlace(Counter<T> c)
public static <E> boolean equals(Counter<E> o1, Counter<E> o2)
public static <E> boolean equals(Counter<E> o1, Counter<E> o2, double tolerance)
public static <T> Counter<T> unmodifiableCounter(Counter<T> counter)
counter
- The counterpublic static <E> Counter<E> asCounter(FixedPrioritiesPriorityQueue<E> p)
public static <E,N extends java.lang.Number> Counter<E> fromMap(java.util.Map<E,N> map)
public static <E,N extends java.lang.Number> Counter<E> fromMap(java.util.Map<E,N> map, java.lang.Class<N> type)
public static <E> java.util.Map<E,java.lang.Double> asMap(Counter<E> counter)
public static <E> boolean isUniformDistribution(Counter<E> distribution, double tolerance)
E
- The type of the counter.distribution
- The distribution to check.tolerance
- The tolerance for floating point error, in both the equality and total count checks.public static <E> Counter<E> getCopy(Counter<E> originalCounter)
E
- originalCounter
- public static <E> void maxInPlace(Counter<E> target, Counter<E> other)
E
- public static <E> void minInPlace(Counter<E> target, Counter<E> other)
E
- public static <E> void retainTopMass(Counter<E> counter, double thresholdCount)
counter
- thresholdCount
- public static <A,B> void divideInPlace(TwoDimensionalCounter<A,B> counter, double divisor)
public static <E> double pearsonsCorrelationCoefficient(Counter<E> x, Counter<E> y)
public static <E> double spearmanRankCorrelation(Counter<E> x, Counter<E> y)
public static <E> void ensureKeys(Counter<E> t, java.util.Collection<E> keys, double value)
public static <E> java.util.List<E> topKeys(Counter<E> t, int topNum)
public static <E> java.util.List<Pair<E,java.lang.Double>> topKeysWithCounts(Counter<E> t, int topNum)
public static <E> Counter<E> getFCounter(Counter<E> precision, Counter<E> recall, double beta)
public static <E> void transformValuesInPlace(Counter<E> counter, java.util.function.DoubleUnaryOperator func)
public static <E> void retainKeys(Counter<E> counter, java.util.function.Predicate<E> retainFunction)
public static <E> boolean isFinite(Counter<E> counts)
E
- The parameterized type of the counter.counts
- The counter to validate.