/** */ @Override public void incrementCount(K1 o1, K2 o2, double count) { ClassicCounter<K2> c = getCounter(o1); c.incrementCount(o2, count); total += count; }
/** */ @Override public double totalCount(K1 k1) { ClassicCounter<K2> c = getCounter(k1); return c.totalCount(); }
public static void printCounter(TwoDimensionalCounter<String,String> cnt, String fname) { try { PrintWriter pw = new PrintWriter(new PrintStream(new FileOutputStream(new File(fname)),false,"UTF-8")); for(String key : cnt.firstKeySet()) { for(String val : cnt.getCounter(key).keySet()) { pw.printf("%s\t%s\t%d%n", key, val, (int) cnt.getCount(key, val)); } } pw.close(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
/** * A simple String representation of this TwoDimensionalCounter, which has the * String representation of each key pair on a separate line, followed by the * count for that pair. The items are tab separated, so the result is a * tab-separated value (TSV) file. Iff none of the keys contain spaces, it * will also be possible to treat this as whitespace separated fields. */ @Override public String toString() { StringBuilder buff = new StringBuilder(); for (K1 key1 : map.keySet()) { ClassicCounter<K2> c = getCounter(key1); for (K2 key2 : c.keySet()) { double score = c.getCount(key2); buff.append(key1).append('\t').append(key2).append('\t').append(score).append('\n'); } } return buff.toString(); }
/** */ @Override public void setCount(K1 o1, K2 o2, double count) { ClassicCounter<K2> c = getCounter(o1); double oldCount = getCount(o1, o2); total -= oldCount; c.setCount(o2, count); total += count; }
public void subtractAll(K1 key, Counter<K2> c) { ClassicCounter<K2> myInner = getCounter(key); Counters.subtractInPlace(myInner, c); total -= c.totalCount(); }
/** * For all keys (u,v) in target, sets target[u,v] to be target[u,v] + value * * @param <T1> * @param <T2> */ public static <T1, T2> void addInPlace(TwoDimensionalCounter<T1, T2> target, double value) { for (T1 outer : target.firstKeySet()){ addInPlace(target.getCounter(outer), value); } }
public void addAll(K1 key, Counter<K2> c) { ClassicCounter<K2> myInner = getCounter(key); Counters.addInPlace(myInner, c); total += c.totalCount(); }
@Override public void printReasonForChoosing(Counter<CandidatePhrase> phrases){ Redwood.log(Redwood.DBG, "Features of selected phrases"); for(Entry<CandidatePhrase, Double> pEn: phrases.entrySet()) Redwood.log(Redwood.DBG, pEn.getKey().getPhrase() + "\t" + pEn.getValue() + "\t" + phraseScoresRaw.getCounter(pEn.getKey())); }
/** * replace the counter for K1-index o by new counter c */ public ClassicCounter<K2> setCounter(K1 o, Counter<K2> c) { ClassicCounter<K2> old = getCounter(o); total -= old.totalCount(); if (c instanceof ClassicCounter) { map.put(o, (ClassicCounter<K2>) c); } else { map.put(o, new ClassicCounter<>(c)); } total += c.totalCount(); return old; }
@Override public double remove(K1 o1, K2 o2) { ClassicCounter<K2> c = getCounter(o1); double oldCount = getCount(o1, o2); total -= oldCount; c.remove(o2); if (c.size() == 0) { map.remove(o1); } return oldCount; }
@Override public Set<K2> secondKeySet() { Set<K2> result = Generics.newHashSet(); for (K1 k1 : firstKeySet()) { for (K2 k2 : getCounter(k1).keySet()) { result.add(k2); } } return result; }
/** */ @Override public double getCount(K1 o1, K2 o2) { ClassicCounter<K2> c = getCounter(o1); if (c.totalCount() == 0.0 && !c.keySet().contains(o2)) { return defaultReturnValue(); } return c.getCount(o2); }
public void removeZeroCounts() { Set<K1> firstKeySet = Generics.newHashSet(firstKeySet()); for (K1 k1 : firstKeySet) { ClassicCounter<K2> c = getCounter(k1); Counters.retainNonZeros(c); if (c.size() == 0) map.remove(k1); // it's empty, get rid of it! } }
public void addAll(TwoDimensionalCounterInterface<K1, K2> c) { for (K1 key : c.firstKeySet()) { Counter<K2> inner = c.getCounter(key); ClassicCounter<K2> myInner = getCounter(key); Counters.addInPlace(myInner, inner); total += inner.totalCount(); } }
/** * Returns the counters with keys as the first key and count as the * total count of the inner counter for that key * * @return counter of type K1 */ public Counter<K1> sumInnerCounter() { Counter<K1> summed = new ClassicCounter<>(); for (K1 key : this.firstKeySet()) { summed.incrementCount(key, this.getCounter(key).totalCount()); } return summed; }
private static void countTaggings(Treebank tb, final PrintWriter pw) { final TwoDimensionalCounter<String,String> wtc = new TwoDimensionalCounter<>(); tb.apply(tree -> { List<TaggedWord> tags = tree.taggedYield(); for (TaggedWord tag : tags) wtc.incrementCount(tag.word(), tag.tag()); }); for (String key : wtc.firstKeySet()) { pw.print(key); pw.print('\t'); Counter<String> ctr = wtc.getCounter(key); for (String k2 : ctr.keySet()) { pw.print(k2 + '\t' + ctr.getCount(k2) + '\t'); } pw.println(); } }
public void subtractAll(TwoDimensionalCounterInterface<K1, K2> c, boolean removeKeys) { for (K1 key : c.firstKeySet()) { Counter<K2> inner = c.getCounter(key); ClassicCounter<K2> myInner = getCounter(key); Counters.subtractInPlace(myInner, inner); if (removeKeys) Counters.retainNonZeros(myInner); total -= inner.totalCount(); } }
/** * Creates a new TwoDimensionalCounter where all the counts are scaled by d. * Internally, uses Counters.scale(); * * @return The TwoDimensionalCounter */ public static <T1, T2> TwoDimensionalCounter<T1, T2> scale(TwoDimensionalCounter<T1, T2> c, double d) { TwoDimensionalCounter<T1, T2> result = new TwoDimensionalCounter<>(c.getOuterMapFactory(), c.getInnerMapFactory()); for (T1 key : c.firstKeySet()) { ClassicCounter<T2> ctr = c.getCounter(key); result.setCounter(key, scale(ctr, d)); } return result; }
public ClassicCounter<Pair<K1, K2>> flatten() { ClassicCounter<Pair<K1, K2>> result = new ClassicCounter<>(); result.setDefaultReturnValue(defaultValue); for (K1 key1 : firstKeySet()) { ClassicCounter<K2> inner = getCounter(key1); for (K2 key2 : inner.keySet()) { result.setCount(new Pair<>(key1, key2), inner.getCount(key2)); } } return result; }