/** * Converts from the format printed by the toString method back into * a Counter<String>. The toString() doesn't escape, so this only * works providing the keys of the Counter do not have commas or equals signs * in them. * * @param s A String representation of a Counter * @return The Counter */ public static ClassicCounter<String> fromString(String s) { ClassicCounter<String> result = new ClassicCounter<>(); if (!s.startsWith("{") || !s.endsWith("}")) { throw new RuntimeException("invalid format: ||"+s+"||"); } s = s.substring(1, s.length()-1); String[] lines = s.split(", "); for (String line : lines) { String[] fields = line.split("="); if (fields.length!=2) throw new RuntimeException("Got unsplittable line: \"" + line + '\"'); result.setCount(fields[0], Double.parseDouble(fields[1])); } return result; }
result.setCount(fields[0], Double.parseDouble(fields[1]));
/** */ @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; }
f1.setCount("humidity", 5.0); f1.setCount("temperature", 35.0); trainingInstances.add(new RVFDatum<>(f1, "rain")); f1.setCount("humidity", 4.0); f1.setCount("temperature", 32.0); trainingInstances.add(new RVFDatum<>(f1, "rain")); f1.setCount("humidity", 6.0); f1.setCount("temperature", 30.0); trainingInstances.add(new RVFDatum<>(f1, "rain")); f1.setCount("humidity", 2.0); f1.setCount("temperature", 33.0); trainingInstances.add(new RVFDatum<>(f1, "dry")); f1.setCount("humidity", 1.0); f1.setCount("temperature", 34.0); trainingInstances.add(new RVFDatum<>(f1, "dry")); f1.setCount("humidity", 2.0); f1.setCount("temperature", 33.0); RVFDatum<String, String> testVec = new RVFDatum<>(f1); System.out.println(classifier.scoresOf(testVec));
private static ClassicCounter<List<String>> removeLowCountPaths(ClassicCounter<List<String>> paths, double thresh) { ClassicCounter<List<String>> result = new ClassicCounter<>(); int numRetained = 0; for (List<String> path : paths.keySet()) { double count = paths.getCount(path); if (count >= thresh) { result.setCount(path, count); numRetained++; } } System.out.println("retained " + numRetained); return result; }
@Override public UnknownWordModel finishTraining() { if (useGT) { unknownGTTrainer.finishTraining(); } for (Map.Entry<Label, ClassicCounter<String>> entry : c.entrySet()) { /* outer iteration is over tags */ Label key = entry.getKey(); ClassicCounter<String> wc = entry.getValue(); // counts for words given a tag if (!tagHash.containsKey(key)) { tagHash.put(key, new ClassicCounter<>()); } /* the UNKNOWN sequence is assumed to be seen once in each tag */ // This is sort of broken, but you can regard it as a Dirichlet prior. tc.incrementCount(key); wc.setCount(unknown, 1.0); /* inner iteration is over words */ for (String end : wc.keySet()) { double prob = Math.log((wc.getCount(end)) / (tc.getCount(key))); // p(sig|tag) tagHash.get(key).setCount(end, prob); //if (Test.verbose) //EncodingPrintWriter.out.println(tag + " rewrites as " + end + " endchar with probability " + prob,encoding); } } return model; }
@Override public UnknownWordModel finishTraining() { // Map<String,Float> unknownGT = null; if (useGT) { unknownGTTrainer.finishTraining(); // unknownGT = unknownGTTrainer.unknownGT; } for (Label tagLab : c.keySet()) { // outer iteration is over tags as Labels ClassicCounter<String> wc = c.get(tagLab); // counts for words given a tag if ( ! tagHash.containsKey(tagLab)) { tagHash.put(tagLab, new ClassicCounter<>()); } // the UNKNOWN first character is assumed to be seen once in // each tag // this is really sort of broken! (why??) tc.incrementCount(tagLab); wc.setCount(unknown, 1.0); // inner iteration is over words as strings for (String first : wc.keySet()) { double prob = Math.log(((wc.getCount(first))) / tc.getCount(tagLab)); tagHash.get(tagLab).setCount(first, prob); //if (Test.verbose) //EncodingPrintWriter.out.println(tag + " rewrites as " + first + " first char with probability " + prob,encoding); } } return model; }
Set nodes = graph.getNodes(); for (Object node : nodes) { lambda.setCount(node, 0); length.setCount(node, Double.POSITIVE_INFINITY); lambda.setCount(o, 0); length.setCount(o, 0); queue.addLast(o); length.setCount(newNode, oldLen + 1); // this may already be the case lambda.setCount(newNode, k + lambda.getCount(node));
private void displayHelper(Set<OUT> keys, PrintWriter pw, ClassicCounter<OUT> guessed, ClassicCounter<OUT> guessedCorrect, ClassicCounter<OUT> gold, ClassicCounter<OUT> goldCorrect) { Map<OUT, String> pads = getPads(keys); for (OUT key : keys) { double thisGuessed = guessed.getCount(key); double thisGuessedCorrect = guessedCorrect.getCount(key); double precision = (thisGuessed == 0.0) ? 0.0 : thisGuessedCorrect / thisGuessed; lastPrecision.setCount(key, precision); double thisGold = gold.getCount(key); double thisGoldCorrect = goldCorrect.getCount(key); double recall = (thisGold == 0.0) ? 0.0 : thisGoldCorrect / thisGold; lastRecall.setCount(key, recall); double f1 = f1(precision, recall); lastF1.setCount(key, f1); String pad = pads.get(key); pw.println(key + pad + "\t" + "P: " + formatNumber(precision) + "\ton " + formatCount(thisGuessed) + " objects\tR: " + formatNumber(recall) + "\ton " + formatCount(thisGold) + " objects\tF1: " + formatNumber(f1)); } }
/ (productSizes[pos] / tagNum[pos - leftWindow])]; kBestWithScores.setCount(Arrays.copyOf(tempTags, tempTags.length), bestFinalScores[k1]);
/** * Read a Counter from a serialized file * @param filename The file to read from * * @see Counters#serializeStringCounter(Counter, String, double) */ public static ClassicCounter<String> deserializeStringCounter(String filename) throws IOException { String[] fields = new String[4]; try (BufferedReader reader = IOUtils.readerFromString(filename)) { String line; ClassicCounter<String> counts = new ClassicCounter<>(1000000); while ((line = reader.readLine()) != null) { StringUtils.splitOnChar(fields, line, '\t'); long mantissa = SloppyMath.parseInt(fields[2]); int exponent = (int) SloppyMath.parseInt(fields[3]); double value = SloppyMath.parseDouble(fields[1].equals("-"), mantissa, exponent); counts.setCount(fields[0], value); } return counts; } }
public ClassicCounter<OUT> lastF1() { ClassicCounter<OUT> result = new ClassicCounter<>(); Set<OUT> keys = Sets.union(previousGuessed.keySet(),previousGold.keySet()); for(OUT key : keys) { result.setCount(key,lastF1(key)); } return result; }
c.setCount(i, Math.round(p / rank)); s.add(i);
private RVFDatum<String,String> makeRVFDatumFromStrings(String[] strings) { if (globalFlags.featureFormat) { ClassicCounter<String> theFeatures = new ClassicCounter<>(); for (int i = 0; i < strings.length; i++) { if (i != globalFlags.goldAnswerColumn) { if (isRealValued(flags[i])) { addFeatureValue(strings[i], flags[i], theFeatures); } else { theFeatures.setCount(strings[i], 1.0); } } } return new RVFDatum<>(theFeatures, strings[globalFlags.goldAnswerColumn]); } else { //logger.info("Read in " + strings); return makeRVFDatum(strings); } }
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; }
scores.setCount(instance, Counters.cosine(vec.asFeaturesCounter(), instance)); // set entry, for given instance and score
/** * Calculate sister annotation statistics suitable for doing * selective sister splitting in the PCFGParser inside the * FactoredParser. * * @param args One argument: path to the Treebank */ public static void main(String[] args) { ClassicCounter<String> c = new ClassicCounter<>(); c.setCount("A", 0); c.setCount("B", 1); double d = Counters.klDivergence(c, c); System.out.println("KL Divergence: " + d); String encoding = "UTF-8"; if (args.length > 1) { encoding = args[1]; } if (args.length < 1) { System.out.println("Usage: ParentAnnotationStats treebankPath"); } else { SisterAnnotationStats pas = new SisterAnnotationStats(); Treebank treebank = new DiskTreebank(in -> new PennTreeReader(in, new LabeledScoredTreeFactory(new StringLabelFactory()), new BobChrisTreeNormalizer()), encoding); treebank.loadPath(args[0]); treebank.apply(pas); pas.printStats(); } }
/** * Extracts all the features from a certain input array and makes * a real valued feature datum; those features that are not real valued * are given value 1.0. * * @param strs The data String[] to extract features from * @return The constructed RVFDatum */ private RVFDatum<String,String> makeRVFDatum(String[] strs) { String goldAnswer = globalFlags.goldAnswerColumn < strs.length ? strs[globalFlags.goldAnswerColumn]: ""; ClassicCounter<String> theFeatures = new ClassicCounter<>(); ClassicCounter<String> globalFeatures = new ClassicCounter<>(); if (globalFlags.useClassFeature) { globalFeatures.setCount("CLASS", 1.0); } addAllInterningAndPrefixingRVF(theFeatures, globalFeatures, ""); for (int i = 0; i < flags.length; i++) { ClassicCounter<String> featuresC = new ClassicCounter<>(); makeDatum(strs[i], flags[i], featuresC, goldAnswer); addAllInterningAndPrefixingRVF(theFeatures, featuresC, i + "-"); } if (globalFlags.printFeatures != null) { printFeatures(strs, theFeatures); } //System.out.println("Features are: " + theFeatures); return new RVFDatum<>(theFeatures, goldAnswer); }