GeneralizedCounter<String> gc = new GeneralizedCounter<>(3); gc.incrementCount(Arrays.asList(new String[]{"a", "j", "x"}), 3.0); gc.incrementCount(Arrays.asList(new String[]{"a", "l", "x"}), 3.0); gc.incrementCount(Arrays.asList(new String[]{"b", "k", "y"}), 3.0); gc.incrementCount(Arrays.asList(new String[]{"b", "k", "z"}), 3.0); System.out.println(gc.dumpKeys()); System.out.println(gc.toString()); gc.printKeySet(); System.out.println("entry set:\n" + gc.entrySet()); arrayPrintDouble(gc.getCounts(Arrays.asList(new String[]{"a", "j", "x"}))); arrayPrintDouble(gc.getCounts(Arrays.asList(new String[]{"a", "j", "z"}))); arrayPrintDouble(gc.getCounts(Arrays.asList(new String[]{"b", "k", "w"}))); arrayPrintDouble(gc.getCounts(Arrays.asList(new String[]{"b", "k", "z"}))); GeneralizedCounter<String> gc1 = gc.conditionalize(Arrays.asList(new String[]{"a"})); gc1.incrementCount(Arrays.asList(new String[]{"j", "x"})); gc1.incrementCount2D("j", "z"); GeneralizedCounter<String> gc2 = gc1.conditionalize(Arrays.asList(new String[]{"j"})); gc2.incrementCount1D("x"); System.out.println("Pretty-printing gc after incrementing gc1:"); gc.prettyPrint(); System.out.println("Total: " + gc.totalCount()); gc1.printKeySet();
/** * Equivalent to incrementCount( new Object[] { first, second }, count ). * Makes the special case easier, and also more efficient. */ public void incrementCount2D(K first, K second, double count) { if (depth != 2) { wrongDepth(); //throws exception } this.addToTotal(count); GeneralizedCounter<K> next = this.conditionalizeHelper(first); next.incrementCount1D(second, count); }
/** * returns the total count of objects in the GeneralizedCounter. */ public double totalCount() { if (depth() == 1) { return total; // I think this one is always OK. Not very principled here, though. } else { double result = 0.0; for (K o: topLevelKeySet()) { result += conditionalizeOnce(o).totalCount(); } return result; } }
/** * Equivalent to incrementCount( new Object[] { first, second, third }, count ). * Makes the special case easier, and also more efficient. */ public void incrementCount3D(K first, K second, K third, double count) { if (depth != 3) { wrongDepth(); //throws exception } this.addToTotal(count); GeneralizedCounter<K> next = this.conditionalizeHelper(first); next.incrementCount2D(second, third, count); }
private void prettyPrint(PrintWriter pw, String buffer, String bufferIncrement) { if (depth == 1) { for (Map.Entry<?, Double> e: entrySet()) { Object key = e.getKey(); double count = e.getValue(); pw.println(buffer + key + "\t" + count); } } else { for (K key: topLevelKeySet()) { GeneralizedCounter<K> gc1 = conditionalize(Arrays.asList(ErasureUtils.<K[]>uncheckedCast(new Object[]{key}))); pw.println(buffer + key + "\t" + gc1.totalCount()); gc1.prettyPrint(pw, buffer + bufferIncrement, bufferIncrement); } } }
public GeneralizedCounter<K> reverseKeys() { GeneralizedCounter<K> result = new GeneralizedCounter<>(); Set<Map.Entry<List<K>,Double>> entries = entrySet(); for (Map.Entry<List<K>,Double> entry: entries) { List<K> list = entry.getKey(); double count = entry.getValue(); Collections.reverse(list); result.incrementCount(list, count); } return result; }
public String toString(String param) { switch (param) { case "contingency": { StringBuilder sb = new StringBuilder(); for (K obj : ErasureUtils.sortedIfPossible(topLevelKeySet())) { sb.append(obj); sb.append(" = "); GeneralizedCounter<K> gc = conditionalizeOnce(obj); sb.append(gc); sb.append("\n"); } return sb.toString(); } case "sorted": { StringBuilder sb = new StringBuilder(); sb.append("{\n"); for (K obj : ErasureUtils.sortedIfPossible(topLevelKeySet())) { sb.append(obj); sb.append(" = "); GeneralizedCounter<K> gc = conditionalizeOnce(obj); sb.append(gc); sb.append("\n"); } sb.append("}\n"); return sb.toString(); } default: return toString(); } }
GeneralizedCounter[] POSspecificCharNGrams = new GeneralizedCounter[CONTEXT_LENGTH + 1]; for (int i = 0; i <= CONTEXT_LENGTH; i++) { POSspecificCharNGrams[i] = new GeneralizedCounter(i + 2); sym = Symbol.END_WORD; POSspecificCharNGrams[0].incrementCount(context, sym); // POS-specific 1-gram if (unknownCharClass != null) { POSspecificCharNGrams[0].incrementCount(context, unknownCharClass); // for unknown ch model if (i - j < 0) { context.add(Symbol.BEGIN_WORD); POSspecificCharNGrams[j].incrementCount(context, sym); if (unknownCharClass != null) { POSspecificCharNGrams[j].incrementCount(context, unknownCharClass); // for unknown ch model context.add(prev); POSspecificCharNGrams[j].incrementCount(context, sym); if (unknownCharClass != null) { POSspecificCharNGrams[j].incrementCount(context, unknownCharClass); // for unknown ch model Set<Map.Entry<List<Serializable>, ClassicCounter<Symbol>>> counterEntries = POSspecificCharNGrams[i].lowestLevelCounterEntrySet(); Timing.tick("Creating " + counterEntries.size() + " character " + (i + 1) + "-gram distributions..."); for (Map.Entry<List<Serializable>, ClassicCounter<Symbol>> entry : counterEntries) {
/** * Returns a GeneralizedCounter conditioned on the given top level object. * This is just shorthand (and more efficient) for <code>conditionalize(new Object[] { o })</code>. */ public GeneralizedCounter<K> conditionalizeOnce(K o) { if (depth() < 1) { throw new RuntimeException("Error -- attempted to conditionalize a GeneralizedCounter of depth " + depth()); } else { return conditionalizeHelper(o); } }
public GeneralizedCounter<E> probs(int position, int window) { GeneralizedCounter<E> gc = new GeneralizedCounter<>(window); int[] labels = new int[window]; // cdm july 2005: below array initialization isn't necessary: JLS (3rd ed.) // 4.12.5 // Arrays.fill(labels, 0); OUTER: while (true) { List<E> labelsList = intArrayToListE(labels); gc.incrementCount(labelsList, prob(position, labels)); for (int i = 0; i < labels.length; i++) { labels[i]++; if (labels[i] < numClasses) { break; } if (i == labels.length - 1) { break OUTER; } labels[i] = 0; } } return gc; }
/** * returns a {@code double[]} array of length * {@code depth+1}, containing the conditional counts on a * {@code depth}-length list given each level of conditional * distribution from 0 to {@code depth}. */ public double[] getCounts(List<K> l) { if (l.size() != depth) { wrongDepth(); //throws exception } double[] counts = new double[depth + 1]; GeneralizedCounter<K> next = this; counts[0] = next.totalCount(); Iterator<K> i = l.iterator(); int j = 1; K o = i.next(); while (i.hasNext()) { next = next.conditionalizeHelper(o); counts[j] = next.totalCount(); o = i.next(); j++; } counts[depth] = next.getCount(o); return counts; }
/** * Equivalent to <code>{@link #incrementCount}({o}, count)</code>; * only works for a depth 1 GeneralizedCounter. */ public void incrementCount1D(K o, double count) { if (depth > 1) { wrongDepth(); } addToTotal(count); if (tempMDouble == null) { tempMDouble = new MutableDouble(); } tempMDouble.set(count); MutableDouble oldMDouble = (MutableDouble) map.put(o, tempMDouble); if (oldMDouble != null) { tempMDouble.set(count + oldMDouble.doubleValue()); } tempMDouble = oldMDouble; }
conditionalizeHelper(o).entrySet(s, newKey, true);
ClassicCounter<K> c = conditionalizeHelper(finalKey).oneDimensionalCounterView(); if (useLists) { s.add(new Entry<>(Arrays.asList(newKey), c)); conditionalizeHelper(o).lowestLevelCounterEntrySet(s, newKey, true);
private GeneralizedCounter<K> conditionalizeHelper(K o) { if (depth > 1) { GeneralizedCounter<K> next = ErasureUtils.uncheckedCast(map.get(o)); if (next == null) // adds a new GeneralizedCounter if needed { map.put(o, (next = new GeneralizedCounter<>(depth - 1))); } return next; } else { throw new RuntimeException("Error -- can't conditionalize a distribution of depth 1"); } }
conditionalizeHelper(o).keySet(s, newKey, true);
/** * Returns the set of entries in the GeneralizedCounter. * Here, each key is a read-only {@link * List} of size equal to the depth of the GeneralizedCounter, and * each value is a {@link Double}. Each entry is a {@link java.util.Map.Entry} object, * but these objects * do not support the {@link java.util.Map.Entry#setValue} method; attempts to call * that method with result * in an {@link UnsupportedOperationException} being thrown. */ public Set<Map.Entry<List<K>,Double>> entrySet() { return ErasureUtils.<Set<Map.Entry<List<K>,Double>>>uncheckedCast(entrySet(new HashSet<>(), zeroKey, true)); }
/** * Like {@link ClassicCounter}, this currently returns true if the count is * explicitly 0.0 for something */ public boolean containsKey(List<K> key) { // if(! (key instanceof Object[])) // return false; // Object[] o = (Object[]) key; GeneralizedCounter<K> next = this; for (int i=0; i<key.size()-1; i++) { next = next.conditionalizeHelper(key.get(i)); if (next==null) return false; } return next.map.containsKey(key.get(key.size()-1)); }
private void prettyPrint(PrintWriter pw, String buffer, String bufferIncrement) { if (depth == 1) { for (Map.Entry<?, Double> e: entrySet()) { Object key = e.getKey(); double count = e.getValue(); pw.println(buffer + key + "\t" + count); } } else { for (K key: topLevelKeySet()) { GeneralizedCounter<K> gc1 = conditionalize(Arrays.asList(ErasureUtils.<K[]>uncheckedCast(new Object[]{key}))); pw.println(buffer + key + "\t" + gc1.totalCount()); gc1.prettyPrint(pw, buffer + bufferIncrement, bufferIncrement); } } }
/** * Equivalent to incrementCount( new Object[] { first, second, third }, count ). * Makes the special case easier, and also more efficient. */ public void incrementCount3D(K first, K second, K third, double count) { if (depth != 3) { wrongDepth(); //throws exception } this.addToTotal(count); GeneralizedCounter<K> next = this.conditionalizeHelper(first); next.incrementCount2D(second, third, count); }