public Object aggregate(Class key, List<? extends CoreMap> in) { if (in == null) return null; IntCounter<Object> counter = new IntCounter<>(); for (CoreMap cm:in) { Object obj = cm.get(key); if (obj != null && (ignoreSet == null || !ignoreSet.contains(obj))) { counter.incrementCount(obj); } } if (counter.size() > 0) { return counter.argmax(); } else { return null; } } }
/** * Splits the state counts. Root states and the boundary tag do not * get their counts increased, and all others are doubled. Betas * and transition weights are handled later. */ private void splitStateCounts() { // double the count of states... IntCounter<String> newStateSplitCounts = new IntCounter<>(); newStateSplitCounts.addAll(stateSplitCounts); newStateSplitCounts.addAll(stateSplitCounts); // root states should only have 1 for (String root : startSymbols) { if (newStateSplitCounts.getCount(root) > 1) { newStateSplitCounts.setCount(root, 1); } } if (newStateSplitCounts.getCount(Lexicon.BOUNDARY_TAG) > 1) { newStateSplitCounts.setCount(Lexicon.BOUNDARY_TAG, 1); } stateSplitCounts = newStateSplitCounts; }
/** * Finds and returns the key in this Counter with the smallest count. * Ties are broken according to the natural ordering of the objects. * This will prefer smaller numeric keys and lexicographically earlier * String keys. To use a different tie-breaking Comparator, use * {@link #argmin(Comparator)}. Returns null if this Counter is empty. */ public E argmin() { return argmin(ErasureUtils.<Comparator<E>>uncheckedCast(naturalComparator)); }
@Override public double incrementCount(E key, double value) { incrementCount(key, (int)value); return getCount(key); }
/** * Adds the counts in the given Counter to the counts in this Counter. * <p> * To copy the values from another Counter rather than adding them, use */ public void addAll(IntCounter<E> counter) { for (E key : counter.keySet()) { int count = counter.getIntCount(key); incrementCount(key, count); } }
/** * This has been de-deprecated in order to reduce compilation warnings, but * really you should create a {@link edu.stanford.nlp.stats.Distribution} instead of using this method. */ public double getNormalizedCount(E key) { return getCount(key) / (totalCount()); }
public IntCounter<Pair<K1, K2>> flatten() { IntCounter<Pair<K1, K2>> result = new IntCounter<>(); result.setDefaultReturnValue(defaultValue); for (K1 key1 : firstKeySet()) { IntCounter<K2> inner = getCounter(key1); for (K2 key2 : inner.keySet()) { result.setCount(new Pair<>(key1, key2), inner.getIntCount(key2)); } } return result; }
public void unsetVar(Object var) { if(numVarsSet.getCount(var) > 0) numVarsSet.decrementCount(var); if(numVarsSet.getCount(var)==0) varsToStrings.put(var,null); }
public void applyFeatureCountThreshold(Collection<String> data, int thresh) { IntCounter c = new IntCounter(); for (String datum : data) { for (String feat : makeFeatures(datum)) { c.incrementCount(feat); } } threshedFeatures = c.keysAbove(thresh); log.info((c.size() - threshedFeatures.size()) + " word features removed due to thresholding."); }
public void clearCounts() { if (foundCorrect != null) { foundCorrect.clear(); } else { foundCorrect = new IntCounter<>(); } if (foundGuessed != null) { foundGuessed.clear(); } else { foundGuessed = new IntCounter<>(); } if (correctGuesses != null) { correctGuesses.clear(); } else { correctGuesses = new IntCounter<>(); } if (tpCount != null) { Arrays.fill(tpCount, 0); } if (fnCount != null) { Arrays.fill(fnCount, 0); } if (fpCount != null) { Arrays.fill(fpCount, 0); } tokensCount = 0; tokensCorrect = 0; }
/** * Subtracts the counts in the given Counter from the counts in this Counter. * <p> * To copy the values from another Counter rather than subtracting them, use */ public void subtractAll(IntCounter<E> counter) { for (E key : map.keySet()) { decrementCount(key, counter.getIntCount(key)); } }
/** * Adds 1 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. * <p> * To increment the count by a value other than 1, use * {@link #incrementCount(Object,int)}. * <p> * To set a count to a specific value instead of incrementing it, use * {@link #setCount(Object,int)}. */ @Override public double incrementCount(E key) { return incrementCount(key, 1); }
public IntCounter<K1> totalCounts() { IntCounter<K1> tc = new IntCounter<>(); for (K1 k1:map.keySet()) { tc.setCount(k1, map.get(k1).totalCount()); } return tc; }
public void clean() { for (K1 key1 : Generics.newHashSet(map.keySet())) { IntCounter<K2> c = map.get(key1); for (K2 key2 : Generics.newHashSet(c.keySet())) { if (c.getIntCount(key2) == 0) { c.remove(key2); } } if (c.keySet().isEmpty()) { map.remove(key1); } } }
/** */ public int getCount(K1 o1, K2 o2) { IntCounter<K2> c = getCounter(o1); if (c.totalCount() == 0 && !c.keySet().contains(o2)) { return defaultReturnValue(); } return c.getIntCount(o2); }
@Override public Object clone() { return new IntCounter<>(this); }
public Counter<Integer> lengthAccuracies() { Set<Integer> keys = Generics.newHashSet(); keys.addAll(lengthLabelsCorrect.keySet()); keys.addAll(lengthLabelsIncorrect.keySet()); Counter<Integer> results = new ClassicCounter<>(); for (Integer key : keys) { results.setCount(key, lengthLabelsCorrect.getCount(key) / (lengthLabelsCorrect.getCount(key) + lengthLabelsIncorrect.getCount(key))); } return results; }
TagCount(IntCounter<String> tagCounts) { for (String tag : tagCounts.keySet()) { map.put(tag, tagCounts.getIntCount(tag)); } getTagsCache = map.keySet().toArray(new String[map.keySet().size()]); sumCache = calculateSumCache(); }
private static <E> void appendIntCountStats(StringBuilder sb, String label, IntCounter<E> counts) { sb.append(label).append("\n"); List<E> sortedKeys = Counters.toSortedList(counts); int total = counts.totalIntCount(); for (E key:sortedKeys) { int count = counts.getIntCount(key); appendFrac(sb, key.toString(), count, total); sb.append("\n"); } }
/** * Converts a counter to ranks; ranks start from 0 * * @return A counter where the count is the rank in the original counter */ public static <E> IntCounter<E> toRankCounter(Counter<E> c) { IntCounter<E> rankCounter = new IntCounter<>(); List<E> sortedList = toSortedList(c); for (int i = 0; i < sortedList.size(); i++) { rankCounter.setCount(sortedList.get(i), i); } return rankCounter; }