/** * Insures that object is in keyset (with possibly zero value) * * @param o object to put in keyset */ public void addToKeySet(E o) { if (!counter.containsKey(o)) { counter.setCount(o, 0); } }
public void addFeature(String s, double v) { if(features == null){ features = new ClassicCounter<>(); } features.setCount(s, v); }
public static <T> void powInPlace(Counter<T> c, double temp) { for (T t : c.keySet()) { c.setCount(t, Math.pow(c.getCount(t), temp)); } }
public static <T> void expInPlace(Counter<T> c) { for (T t : c.keySet()) { c.setCount(t, Math.exp(c.getCount(t))); } }
public static <E> void logInPlace(Counter<E> target) { for (E key : target.keySet()) { target.setCount(key, Math.log(target.getCount(key))); } }
public static<E> Counter<E> getCounts(Counter<E> c, Collection<E> keys){ Counter<E> newcounter = new ClassicCounter<>(); for(E k : keys) newcounter.setCount(k, c.getCount(k)); return newcounter; }
public static void loadDomainNGrams() { assert(domainNGramsFile != null); if (domainNGramRawFreq == null || domainNGramRawFreq.size() == 0) { for (String line : IOUtils.readLines(domainNGramsFile)) { String[] t = line.split("\t"); domainNGramRawFreq.setCount(t[0], Double.valueOf(t[1])); } Redwood.log(ConstantsAndVariables.minimaldebug, "Data", "loading freq from domain ngram file " + domainNGramsFile); } } }
public static <T> Counter<T> toCounter(double[] counts, Index<T> index) { if (index.size() < counts.length) throw new IllegalArgumentException("Index not large enough to name all the array elements!"); Counter<T> c = new ClassicCounter<>(); for (int i = 0; i < counts.length; i++) { if (counts[i] != 0.0) c.setCount(index.get(i), counts[i]); } return c; }
public Counter<L> probabilityOf(int [] features) { Counter<L> scores = logProbabilityOf(features); for (L label : scores.keySet()) { scores.setCount(label, Math.exp(scores.getCount(label))); } return scores; }
/** * Returns the counter with keys modified according to function F. Eager * evaluation. If two keys are same after the transformation, one of the values is randomly chosen (depending on how the keyset is traversed) */ public static <T1, T2> Counter<T2> transform(Counter<T1> c, Function<T1, T2> f) { Counter<T2> c2 = new ClassicCounter<>(); for (T1 key : c.keySet()) { c2.setCount(f.apply(key), c.getCount(key)); } return c2; }
/** * Multiplies every count in target by the corresponding value in the term * Counter. */ public static <E> void dotProductInPlace(Counter<E> target, Counter<E> term) { for (E key : target.keySet()) { target.setCount(key, target.getCount(key) * term.getCount(key)); } }
private static <F, L> Counter<Pair<F, L>> makeWeightCounter(double[] weights, Index<Pair<F, L>> weightIndex) { Counter<Pair<F,L>> weightCounter = new ClassicCounter<>(); for (int i = 0; i < weightIndex.size(); i++) { if (weights[i] == 0) { continue; // no need to save 0 weights } weightCounter.setCount(weightIndex.get(i), weights[i]); } return weightCounter; }
/** * Places the minimum of first and second keys values in the first counter. * @param <E> */ public static <E> void minInPlace(Counter<E> target, Counter<E> other){ for(E e: CollectionUtils.union(other.keySet(), target.keySet())){ target.setCount(e, Math.min(target.getCount(e), other.getCount(e))); } }
/** * Places the maximum of first and second keys values in the first counter. * @param <E> */ public static <E> void maxInPlace(Counter<E> target, Counter<E> other) { for(E e: CollectionUtils.union(other.keySet(), target.keySet())){ target.setCount(e, Math.max(target.getCount(e), other.getCount(e))); } }
/** * increments every key in the counter by value */ public static <E> Counter<E> add(Counter<E> c1, double value) { Counter<E> result = c1.getFactory().create(); for (E key : c1.keySet()) { result.setCount(key, c1.getCount(key) + value); } return result; }
@Override public Counter<CandidatePhrase> scorePhrases(String label, Set<CandidatePhrase> terms, boolean forLearningPatterns) throws IOException, ClassNotFoundException { getAllLabeledWordsCluster(); Counter<CandidatePhrase> scores = new ClassicCounter<>(); edu.stanford.nlp.classify.Classifier classifier = learnClassifier(label, forLearningPatterns, null, null); for (CandidatePhrase en : terms) { double score = this.scoreUsingClassifer(classifier, en, label, forLearningPatterns,null, null); scores.setCount(en, score); } return scores; }
/** * Returns the product of c1 and c2. * * @return The product of c1 and c2. */ public static <E> Counter<E> product(Counter<E> c1, Counter<E> c2) { Counter<E> result = c1.getFactory().create(); for (E key : Sets.intersection(c1.keySet(), c2.keySet())) { result.setCount(key, c1.getCount(key) * c2.getCount(key)); } return result; }
public static <E> Counter<E> add(Counter<E> c1, Counter<E> c2) { Counter<E> result = c1.getFactory().create(); for (E key : Sets.union(c1.keySet(), c2.keySet())) { result.setCount(key, c1.getCount(key) + c2.getCount(key)); } retainNonZeros(result); return result; }
public static <T> Counter<T> diff(Counter<T> goldFeatures, Counter<T> guessedFeatures) { Counter<T> result = goldFeatures.getFactory().create(); for (T key : Sets.union(goldFeatures.keySet(), guessedFeatures.keySet())) { result.setCount(key, goldFeatures.getCount(key) - guessedFeatures.getCount(key)); } retainNonZeros(result); return result; }