/** * This method allows you to set counter value for a given first/second pair * * @param first * @param second * @param value * @return */ public double setCount(F first, S second, double value) { Counter<S> counter = maps.get(first); if (counter == null) { counter = new Counter<S>(); maps.put(first, counter); } return counter.setCount(second, value); }
/** * 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); }
/** * 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); }
/** * Returns a thread safe counter * * @return */ public static Counter<String> parallelCounter() { return new Counter<>(); }
/** * 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 allows you to set counter value for a given first/second pair * * @param first * @param second * @param value * @return */ public double setCount(F first, S second, double value) { Counter<S> counter = maps.get(first); if (counter == null) { counter = new Counter<S>(); maps.put(first, counter); } return counter.setCount(second, value); }
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(); }
Counter<Character> counter = new Counter<>(); for (int j = 0; j < line.length(); j++) { counter.incrementCount(line.charAt(j), 1.0f);
/** * 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 void sortColumnsByWordLikelihoodIncluded(final int column) { final Counter<String> counter = new Counter<>(); List<String> col = getColumn(column);
/** * 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 StringGrid getRowWithOnlyOneOccurrence(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); Set<String> keys = new HashSet<>(counter.keySet()); for (String key : keys) { if (counter.getCount(key) > 1) { counter.removeKey(key); } } for (List<String> row : this) { for (String key : keys) if (row.get(column).equals(key)) grid.addRow(row); } return grid; }
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(); }