/** * This method will increment all elements in collection * * @param elements * @param inc */ public void incrementAll(Collection<T> elements, double inc) { for (T element: elements) { incrementCount(element, inc); } }
/** * This method will increment counts for a given first/second pair * * @param first * @param second * @param inc */ public void incrementCount(F first, S second, double inc) { Counter<S> counter = maps.get(first); if (counter == null) { counter = new Counter<S>(); maps.put(first, counter); } counter.incrementCount(second, inc); }
/** * This method will increment counts of this counter by counts from other counter * @param other */ public <T2 extends T> void incrementAll(Counter<T2> other) { for (T2 element: other.keySet()) { double cnt = other.getCount(element); incrementCount(element, cnt); } }
/** * This method removes all elements except of top N by counter values * @param N */ public void keepTopNElements(int N){ PriorityQueue<Pair<T, Double>> queue = asPriorityQueue(); clear(); for (int e = 0; e < N; e++) { Pair<T, Double> pair = queue.poll(); if (pair != null) incrementCount(pair.getFirst(), pair.getSecond()); } }
/** * Calculate string similarity with tfidf weights relative to each character * frequency and how many times a character appears in a given string * @param strings the strings to calculate similarity for * @return the cosine similarity between the strings */ public static double stringSimilarity(String... strings) { if (strings == null) return 0; Counter<String> counter = new Counter<>(); Counter<String> counter2 = new Counter<>(); for (int i = 0; i < strings[0].length(); i++) counter.incrementCount(String.valueOf(strings[0].charAt(i)), 1.0f); for (int i = 0; i < strings[1].length(); i++) counter2.incrementCount(String.valueOf(strings[1].charAt(i)), 1.0f); Set<String> v1 = counter.keySet(); Set<String> v2 = counter2.keySet(); Set<String> both = SetUtils.intersection(v1, v2); double sclar = 0, norm1 = 0, norm2 = 0; for (String k : both) sclar += counter.getCount(k) * counter2.getCount(k); for (String k : v1) norm1 += counter.getCount(k) * counter.getCount(k); for (String k : v2) norm2 += counter2.getCount(k) * counter2.getCount(k); return sclar / Math.sqrt(norm1 * norm2); }
/** * This method will increment all elements in collection * * @param elements * @param inc */ public void incrementAll(Collection<T> elements, double inc) { for (T element: elements) { incrementCount(element, inc); } }
public void incrementTruePositives(Integer classLabel) { truePositives.incrementCount(classLabel, 1.0f); }
public void incrementFalseNegatives(Integer classLabel) { falseNegatives.incrementCount(classLabel, 1.0f); }
@Override public void incrementDocCount(String word, double by) { docFrequencies.incrementCount(word, by); }
public void incrementTrueNegatives(Integer classLabel) { trueNegatives.incrementCount(classLabel, 1.0f); }
public void incrementFalsePositives(Integer classLabel) { falsePositives.incrementCount(classLabel, 1.0f); }
/** * This method will increment counts for a given first/second pair * * @param first * @param second * @param inc */ public void incrementCount(F first, S second, double inc) { Counter<S> counter = maps.get(first); if (counter == null) { counter = new Counter<S>(); maps.put(first, counter); } counter.incrementCount(second, inc); }
private String maximalValue(Map<String, Integer> map) { Counter<String> counter = new Counter<>(); for (Map.Entry<String, Integer> entry : map.entrySet()) { counter.incrementCount(entry.getKey(), entry.getValue()); } return counter.argMax(); }
/** * This method will increment counts of this counter by counts from other counter * @param other */ public <T2 extends T> void incrementAll(Counter<T2> other) { for (T2 element: other.keySet()) { double cnt = other.getCount(element); incrementCount(element, cnt); } }
@Override public void incrementCount(String word, double by) { wordFrequencies.incrementCount(word, by); if (wordFrequencies.getCount(word) >= minWordFrequency && vocabWords.indexOf(word) < 0) vocabWords.add(word); }
/** * This method removes all elements except of top N by counter values * @param N */ public void keepTopNElements(int N){ PriorityQueue<Pair<T, Double>> queue = asPriorityQueue(); clear(); for (int e = 0; e < N; e++) { Pair<T, Double> pair = queue.poll(); if (pair != null) incrementCount(pair.getFirst(), pair.getSecond()); } }
/** * Calculate string similarity with tfidf weights relative to each character * frequency and how many times a character appears in a given string * @param strings the strings to calculate similarity for * @return the cosine similarity between the strings */ public static double stringSimilarity(String... strings) { if (strings == null) return 0; Counter<String> counter = new Counter<>(); Counter<String> counter2 = new Counter<>(); for (int i = 0; i < strings[0].length(); i++) counter.incrementCount(String.valueOf(strings[0].charAt(i)), 1.0f); for (int i = 0; i < strings[1].length(); i++) counter2.incrementCount(String.valueOf(strings[1].charAt(i)), 1.0f); Set<String> v1 = counter.keySet(); Set<String> v2 = counter2.keySet(); Set<String> both = SetUtils.intersection(v1, v2); double sclar = 0, norm1 = 0, norm2 = 0; for (String k : both) sclar += counter.getCount(k) * counter2.getCount(k); for (String k : v1) norm1 += counter.getCount(k) * counter.getCount(k); for (String k : v2) norm2 += counter2.getCount(k) * counter2.getCount(k); return sclar / Math.sqrt(norm1 * norm2); }
public StringGrid getRowsWithDuplicateValuesInColumn(int column) { checkInvalidColumn(column); StringGrid grid = new StringGrid(sep, numColumns); List<String> columns = getColumn(column); Counter<String> counter = new Counter<>(); for (String val : columns) counter.incrementCount(val, 1.0f); counter.dropElementsBelowThreshold(2.0f); Set<String> keys = counter.keySet(); for (List<String> row : this) { for (String key : keys) if (row.get(column).equals(key)) grid.addRow(row); } return grid; }
protected Counter<String> wordFrequenciesForRecord(Collection<Writable> record) { String s = toString(record); Tokenizer tokenizer = tokenizerFactory.create(s); Counter<String> ret = new Counter<>(); while (tokenizer.hasMoreTokens()) ret.incrementCount(tokenizer.nextToken(), 1.0); return ret; }
public Collection<String> getNearestNeighbours(INDArray v, int k) { Counter<String> distances = new Counter<>(); for(Object s : vec.vocab().words()) { String word = (String) s; INDArray otherVec = encode(word); double sim = Transforms.cosineSim(v, otherVec); distances.incrementCount(word, sim); } distances.keepTopNElements(k); return distances.keySetSorted(); }