/** */ @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; }
/** * Given an ordering of the first (row) and second (column) keys, will produce * a double matrix. * */ @Override public double[][] toMatrix(List<K1> firstKeys, List<K2> secondKeys) { double[][] counts = new double[firstKeys.size()][secondKeys.size()]; for (int i = 0; i < firstKeys.size(); i++) { for (int j = 0; j < secondKeys.size(); j++) { counts[i][j] = getCount(firstKeys.get(i), secondKeys.get(j)); } } return counts; }
@Override public double remove(K1 o1, K2 o2) { ClassicCounter<K2> c = getCounter(o1); double oldCount = getCount(o1, o2); total -= oldCount; c.remove(o2); if (c.size() == 0) { map.remove(o1); } return oldCount; }
public static <T1, T2> void save2DCounter(TwoDimensionalCounter<T1, T2> tdc, String filename) throws IOException { PrintWriter out = new PrintWriter(new FileWriter(filename)); for (T1 outer : tdc.firstKeySet()) { for (T2 inner : tdc.secondKeySet()) { out.println(outer + "\t" + inner + '\t' + tdc.getCount(outer, inner)); } } out.close(); }
public static void printCounter(TwoDimensionalCounter<String,String> cnt, String fname) { try { PrintWriter pw = new PrintWriter(new PrintStream(new FileOutputStream(new File(fname)),false,"UTF-8")); for(String key : cnt.firstKeySet()) { for(String val : cnt.getCounter(key).keySet()) { pw.printf("%s\t%s\t%d%n", key, val, (int) cnt.getCount(key, val)); } } pw.close(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
@Override @SuppressWarnings( { "unchecked" }) public String toCSVString(NumberFormat nf) { List<K1> firstKeys = new ArrayList<>(firstKeySet()); List<K2> secondKeys = new ArrayList<>(secondKeySet()); Collections.sort((List<? extends Comparable>) firstKeys); Collections.sort((List<? extends Comparable>) secondKeys); StringBuilder b = new StringBuilder(); String[] headerRow = new String[secondKeys.size() + 1]; headerRow[0] = ""; for (int j = 0; j < secondKeys.size(); j++) { headerRow[j + 1] = secondKeys.get(j).toString(); } b.append(StringUtils.toCSVString(headerRow)).append('\n'); for (K1 rowLabel : firstKeys) { String[] row = new String[secondKeys.size() + 1]; row[0] = rowLabel.toString(); for (int j = 0; j < secondKeys.size(); j++) { K2 colLabel = secondKeys.get(j); row[j + 1] = nf.format(getCount(rowLabel, colLabel)); } b.append(StringUtils.toCSVString(row)).append('\n'); } return b.toString(); }
public String apply(TregexMatcher m) { if(mwCounter == null) throw new RuntimeException("Cannot enable POSSequence features without POS sequence map. Use option -frenchMWMap."); Tree t = m.getMatch(); StringBuilder sb = new StringBuilder(); for(Tree kid : t.children()) { if( ! kid.isPreTerminal()) throw new RuntimeException("Not POS sequence for tree: " + t.toString()); String tag = doBasicCat ? tlp.basicCategory(kid.value()) : kid.value(); sb.append(tag).append(" "); } if(mwCounter.getCount(t.value(), sb.toString().trim()) > cutoff) return annotationMark + sb.toString().replaceAll("\\s+", "").toLowerCase(); else return ""; }
public double remove(K1 o1, K2 o2) { ClassicCounter<K2> c = getCounter(o1); double oldCount = getCount(o1, o2); total -= oldCount; c.remove(o2); if (c.size() == 0) { map.remove(o1); } return oldCount; }
/** * For all keys (u,v) in arg, sets target[u,v] to be target[u,v] + arg[u,v]. * * @param <T1> * @param <T2> */ public static <T1, T2> void addInPlace(TwoDimensionalCounter<T1, T2> target, TwoDimensionalCounter<T1, T2> arg) { for (T1 outer : arg.firstKeySet()) for (T2 inner : arg.secondKeySet()) { target.incrementCount(outer, inner, arg.getCount(outer, inner)); } }
public double remove(K1 o1, K2 o2) { ClassicCounter<K2> c = getCounter(o1); double oldCount = getCount(o1, o2); total -= oldCount; c.remove(o2); if (c.size() == 0) { map.remove(o1); } return oldCount; }
/** * For all keys (u,v) in arg, sets target[u,v] to be target[u,v] + scale * * arg[u,v]. * * @param <T1> * @param <T2> */ public static <T1, T2> void addInPlace(TwoDimensionalCounter<T1, T2> target, TwoDimensionalCounter<T1, T2> arg, double scale) { for (T1 outer : arg.firstKeySet()) for (T2 inner : arg.secondKeySet()) { target.incrementCount(outer, inner, scale * arg.getCount(outer, inner)); } }
@Override public double remove(K1 o1, K2 o2) { ClassicCounter<K2> c = getCounter(o1); double oldCount = getCount(o1, o2); total -= oldCount; c.remove(o2); if (c.size() == 0) { map.remove(o1); } return oldCount; }
/** * Removes all entries with counts below the given threshold, returning the * set of removed entries. * * @param counter The counter. * @param countThreshold * The minimum count for an entry to be kept. Entries (strictly) less * than this threshold are discarded. * @return The set of discarded entries. */ public static <E1, E2> Set<Pair<E1, E2>> retainAbove( TwoDimensionalCounter<E1, E2> counter, double countThreshold) { Set<Pair<E1, E2>> removed = new HashSet<>(); for (Entry<E1, ClassicCounter<E2>> en : counter.entrySet()) { for (Entry<E2, Double> en2 : en.getValue().entrySet()) { if (counter.getCount(en.getKey(), en2.getKey()) < countThreshold) { removed.add(new Pair<>(en.getKey(), en2.getKey())); } } } for (Pair<E1, E2> key : removed) { counter.remove(key.first(), key.second()); } return removed; }
if (patternScoring.equals(PatternScoring.SqrtAllRatio)) useFreqPhraseExtractedByPat = true; ToDoubleFunction<Pair<E, CandidatePhrase>> numeratorScore = x -> patternsandWords4Label.getCount(x.first(), x.second()); denoScore = x -> negPatternsandWords4Label.getCount(x.first(), x.second()) + unLabeledPatternsandWords4Label.getCount(x.first(), x.second()); externalWordWeightsNormalized, useFreqPhraseExtractedByPat); } else if (patternScoring.equals(PatternScoring.RatioAll)) { denoScore = x -> negPatternsandWords4Label.getCount(x.first(), x.second()) + unLabeledPatternsandWords4Label.getCount(x.first(), x.second()) + patternsandWords4Label.getCount(x.first(), x.second()); denominatorPatWt = this.convert2OneDim(label, denoScore,allCandidatePhrases, patternsandWords4Label, constVars.sqrtPatScore, false, externalWordWeightsNormalized, useFreqPhraseExtractedByPat); } else if (patternScoring.equals(PatternScoring.PosNegOdds)) { denoScore = x -> negPatternsandWords4Label.getCount(x.first(), x.second()); denominatorPatWt = this.convert2OneDim(label, denoScore, allCandidatePhrases, patternsandWords4Label, constVars.sqrtPatScore, false, externalWordWeightsNormalized, || patternScoring.equals(PatternScoring.LOGREG) || patternScoring.equals(PatternScoring.LOGREGlogP)) { denoScore = x -> negPatternsandWords4Label.getCount(x.first(), x.second()) + unLabeledPatternsandWords4Label.getCount(x.first(), x.second()); denominatorPatWt = this.convert2OneDim(label, denoScore, allCandidatePhrases, patternsandWords4Label, constVars.sqrtPatScore, true, externalWordWeightsNormalized, useFreqPhraseExtractedByPat); } else if (patternScoring.equals(PatternScoring.SqrtAllRatio)) { denoScore = x -> negPatternsandWords4Label.getCount(x.first(), x.second()) + unLabeledPatternsandWords4Label.getCount(x.first(), x.second());
for (IndexedWord outer : vertexSet()) { for (IndexedWord inner : vertexSet()) { dominatedEdgeCount.incrementCount(outer, nodeDists.getCount(outer, inner));
L label = labelIndex.get(j); double featureLabelCount = condCounter.getCount(feature, label); double notFeatureLabelCount = size() - featureLabelCount;
public static <T1, T2> void save2DCounter(TwoDimensionalCounter<T1, T2> tdc, String filename) throws IOException { PrintWriter out = new PrintWriter(new FileWriter(filename)); for (T1 outer : tdc.firstKeySet()) { for (T2 inner : tdc.secondKeySet()) { out.println(outer + "\t" + inner + '\t' + tdc.getCount(outer, inner)); } } out.close(); }
public static <T1, T2> void save2DCounter(TwoDimensionalCounter<T1, T2> tdc, String filename) throws IOException { PrintWriter out = new PrintWriter(new FileWriter(filename)); for (T1 outer : tdc.firstKeySet()) { for (T2 inner : tdc.secondKeySet()) { out.println(outer + "\t" + inner + '\t' + tdc.getCount(outer, inner)); } } out.close(); }
/** * For all keys (u,v) in arg, sets target[u,v] to be target[u,v] + arg[u,v]. * * @param <T1> * @param <T2> */ public static <T1, T2> void addInPlace(TwoDimensionalCounter<T1, T2> target, TwoDimensionalCounter<T1, T2> arg) { for (T1 outer : arg.firstKeySet()) for (T2 inner : arg.secondKeySet()) { target.incrementCount(outer, inner, arg.getCount(outer, inner)); } }