@Override public Set<Entry<E, Double>> entrySet() { return counter.entrySet(); }
private void writePatternsToFile(Counter<E> pattern, BufferedWriter outFile) throws IOException { for (Entry<E, Double> en : pattern.entrySet()) outFile.write(en.getKey() + "\t" + en.getValue() + "\n"); }
private static <E> void validateCounter(Counter<E> counts) { for (Map.Entry<E, Double> entry : counts.entrySet()) { E item = entry.getKey(); Double dblCount = entry.getValue(); if (dblCount == null) { throw new IllegalArgumentException("ERROR: null count for item " + item + "!"); } if (dblCount < 0) { throw new IllegalArgumentException("ERROR: negative count " + dblCount + " for item " + item + "!"); } } }
private void writeWordsToFile(Map<Integer, Counter<CandidatePhrase>> words, BufferedWriter outFile) throws IOException { for (Entry<Integer, Counter<CandidatePhrase>> en2 : words.entrySet()) { outFile.write("###Iteration " + en2.getKey()+"\n"); for (Entry<CandidatePhrase, Double> en : en2.getValue().entrySet()) outFile.write(en.getKey() + "\t" + en.getValue() + "\n"); } }
/** * Sets each value of target to be target[k]+arg[k] for all keys k in arg. */ public static <E> void addInPlace(Counter<E> target, Counter<E> arg) { for (Map.Entry<E, Double> entry : arg.entrySet()) { double count = entry.getValue(); if (count != 0) { target.incrementCount(entry.getKey(), count); } } }
public static<E> void retainKeys(Counter<E> counter, Predicate<E> retainFunction) { Set<E> remove = new HashSet<>(); for(Entry<E, Double> en: counter.entrySet()){ if(!retainFunction.test(en.getKey())){ remove.add(en.getKey()); } } Counters.removeKeys(counter, remove); }
/** * Sets each value of double[] target to be * target[idx.indexOf(k)]+a.getCount(k) for all keys k in arg */ public static <E> void addInPlace(double[] target, Counter<E> arg, Index<E> idx) { for (Map.Entry<E, Double> entry : arg.entrySet()) { target[idx.indexOf(entry.getKey())] += entry.getValue(); } }
/** * Sets each value of double[] target to be * target[idx.indexOf(k)]-a.getCount(k) for all keys k in arg */ public static <E> void subtractInPlace(double[] target, Counter<E> arg, Index<E> idx) { for (Map.Entry<E, Double> entry : arg.entrySet()) { target[idx.indexOf(entry.getKey())] -= entry.getValue(); } }
public double weightFeatureProduct(Counter<String> features) { double product = 0; for (Map.Entry<String, Double> feature : features.entrySet()) { product += feature.getValue() * weights.getCount(feature.getKey()); } return product; }
/** * Multiplies each value in target by the given multiplier, in place. * * @param target The values in this Counter will be multiplied by the * multiplier * @param multiplier The number by which to change each number in the Counter */ public static <E> Counter<E> multiplyInPlace(Counter<E> target, double multiplier) { for (Entry<E, Double> entry : target.entrySet()) { target.setCount(entry.getKey(), entry.getValue() * multiplier); } return target; }
/** * Divides each value in target by the given divisor, in place. * * @param target The values in this Counter will be changed throughout by the * multiplier * @param divisor The number by which to change each number in the Counter * @return The target Counter is returned (for easier method chaining) */ public static <E> Counter<E> divideInPlace(Counter<E> target, double divisor) { for (Entry<E, Double> entry : target.entrySet()) { target.setCount(entry.getKey(), entry.getValue() / divisor); } return target; }
public SortedMap<String, Double> getWeightVector() { SortedMap<String, Double> m = new TreeMap<>((f1, f2) -> { double weightDifference = Math.abs(weights.getCount(f2)) - Math.abs(weights.getCount(f1)); return weightDifference == 0 ? f1.compareTo(f2) : (int) Math.signum(weightDifference); }); weights.entrySet().stream().forEach(e -> m.put(e.getKey(), e.getValue())); return m; }
/** Returns the score of the RVFDatum for the specified label. * Ignores the true label of the RVFDatum. */ private double scoreOfRVFDatum(Counter<Integer> features, L label) { int iLabel = labelIndex.indexOf(label); double score = 0.0; for (Map.Entry<Integer, Double> entry : features.entrySet()) { score += weight(entry.getKey(), iLabel) * entry.getValue(); } return score + thresholds[iLabel]; }
private static Counter<String> addSuffix(Counter<String> features, String suffix) { Counter<String> withSuffix = new ClassicCounter<>(); for (Map.Entry<String, Double> e : features.entrySet()) { withSuffix.incrementCount(e.getKey() + suffix, e.getValue()); } return withSuffix; }
private static <E> Counter<Integer> collectCountCounts(Counter<E> counts) { Counter<Integer> cc = new ClassicCounter<>(); // counts of counts for (Map.Entry<E, Double> entry : counts.entrySet()) { //E item = entry.getKey(); Integer count = (int) Math.round(entry.getValue()); cc.incrementCount(count); } return cc; }
private static Counter<String> getConjunction(Counter<String> original, String suffix) { Counter<String> conjuction = new ClassicCounter<>(); for (Map.Entry<String, Double> e : original.entrySet()) { conjuction.incrementCount(e.getKey() + suffix, e.getValue()); } return conjuction; }
/** * Multiplies each value in target by the count of the key in mult, in place. Returns non zero entries * * @param target The counter * @param mult The counter you want to multiply with target */ public static <E> Counter<E> multiplyInPlace(Counter<E> target, Counter<E> mult) { for (Entry<E, Double> entry : target.entrySet()) { target.setCount(entry.getKey(), entry.getValue() * mult.getCount(entry.getKey())); } Counters.retainNonZeros(target); return target; }
public static <E> double standardDeviation(Counter<E> c) { double std = 0; double mean = c.totalCount() / c.size(); for (Map.Entry<E, Double> en : c.entrySet()) { std += (en.getValue() - mean) * (en.getValue() - mean); } return Math.sqrt(std / c.size()); }
@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())); }
void getAllLabeledWordsCluster(){ for(String label: constVars.getLabels()){ for(Map.Entry<CandidatePhrase, Double> p : constVars.getLearnedWords(label).entrySet()){ wordClassClustersForPhrase.put(p.getKey(), wordClass(p.getKey().getPhrase(), p.getKey().getPhraseLemma())); } for(CandidatePhrase p : constVars.getSeedLabelDictionary().get(label)){ wordClassClustersForPhrase.put(p, wordClass(p.getPhrase(), p.getPhraseLemma())); } } }