List of usage examples for edu.stanford.nlp.stats Counters toSortedList
public static <E> List<E> toSortedList(Counter<E> c, boolean ascending)
From source file:ilcc.ccgparser.nnparser.IncNNParser.java
public Dataset genTrainExamples(List<CCGJSentence> sents, List<CCGJTreeNode> trees) throws IOException { int numTrans = actsList.size(); Dataset ret = new Dataset(config.numTokens, numTrans); Counter<Integer> tokPosCount = new IntCounter<>(); System.err.println(Config.SEPARATOR); System.err.println("Generate training examples..."); System.err.println("With #transitions: " + numTrans); double start = (long) (System.currentTimeMillis()), end; System.err.println("Started at: " + new Date(System.currentTimeMillis())); for (int i = 0; i < sents.size(); ++i) { if (i > 0) { //System.err.print(i + " "); if (i % 1000 == 0) System.err.print(i + " "); if (i % 10000 == 0 || i == sents.size() - 1) System.err.println(); }//from w ww . j ava2s .c om CCGJSentence sent = sents.get(i); if (sent == null) continue; srparser.initVars(sent); List<ArcJAction> gActList = goldDetails.get(i + 1).getarcActs(); for (ArcJAction gAct : gActList) { ArrayList<ArcJAction> acts = getAction(srparser); ArrayList<Integer> rightPerList = null; int stacksize = srparser.stack.size(); if (srparser.incalgo && stacksize > 1) { CCGJTreeNode left = srparser.stack.get(stacksize - 2); Integer lvertex = left.getConllNode().getNodeId(); rightPerList = srparser.depGraph.getRightPer(lvertex); } List<Integer> feature = getFeatures(srparser, rightPerList, sent); List<Integer> label = new ArrayList<>(Collections.nCopies(numTrans, -1)); for (ArcJAction act : acts) { Integer id = actsMap.get(act); if (id != null) { if (act.equals(gAct)) label.set(id, 1); else label.set(id, 0); } } ret.addExample(feature, label); for (int j = 0; j < feature.size(); ++j) tokPosCount.incrementCount(feature.get(j) * feature.size() + j); srparser.applyAction(gAct); } } System.err.println("#Train Examples: " + ret.n); end = (long) System.currentTimeMillis(); System.err.println("Ended at : " + new Date(System.currentTimeMillis()) + " taking " + 0.001 * (end - start) + " secs"); List<Integer> sortedTokens = Counters.toSortedList(tokPosCount, false); preComputed = new ArrayList<>( sortedTokens.subList(0, Math.min(config.numPreComputed, sortedTokens.size()))); return ret; }