/* A conditional node is constructed if its condition is true. All the nodes that have been pushed since the node was opened are made children of the the conditional node, which is then pushed on to the stack. If the condition is false the node is not constructed and they are left on the stack. */ void closeNodeScope(Node n, boolean condition) throws ParseException { SimpleNode sn = (SimpleNode) n; if (condition) { SimpleNode newNode = null; try { newNode = builder.closeNode(sn, nodeArity()); } catch (ParseException exc) { throw exc; } catch (Exception exc) { exc.printStackTrace(); throw new ParseException("Internal error:" + exc); } if (newNode == null) { throw new ParseException("Internal AST builder error"); } mk = marks.pop(); pushNode(newNode); node_created = true; } else { mk = marks.pop(); node_created = false; } }
/* A definite node is constructed from a specified number of children. That number of nodes are popped from the stack and made the children of the definite node. Then the definite node is pushed on to the stack. */ void closeNodeScope(Node n, int num) throws ParseException { SimpleNode sn = (SimpleNode) n; mk = marks.pop(); SimpleNode newNode = null; try { newNode = builder.closeNode(sn, num); } catch (ParseException exc) { throw exc; } catch (Exception exc) { exc.printStackTrace(); throw new ParseException("Internal error:" + exc); } if (newNode == null) { throw new ParseException("Internal AST builder error"); } pushNode(newNode); node_created = true; }
@Test public void testStack() { IntStack s = new IntStack(); assertEquals(0, s.size()); assertTrue(s.isEmpty()); for (int i = 0; i < 100; i++) { s.push(i); } assertEquals(100, s.size()); assertFalse(s.isEmpty()); for (int i = 99; i >= 0; i--) { assertEquals(i, s.pop()); } assertEquals(0, s.size()); assertTrue(s.isEmpty()); }
void clearNodeScope(Node n) { while (sp > mk) { popNode(); } mk = marks.pop(); }
void setNodePos() { SimpleNode n = (SimpleNode) peekNode(); n.beginLine = lines.pop(); n.beginColumn = columns.pop(); }
/* Returns the node on the top of the stack, and remove it from the stack. */ Node popNode() { if (--sp < mk) { mk = marks.pop(); } return (Node) nodes.pop(); }
/** * Create final clusters by merging base clusters and pruning their labels. Cluster merging is a * greedy process of compacting clusters with document sets that overlap by a certain ratio. In * other words, phrases that "cover" nearly identical document sets will be conflated. */ private ArrayList<ClusterCandidate> createMergedClusters( ArrayList<ClusterCandidate> baseClusters) { /* * Calculate overlap between base clusters first, saving adjacency lists for * each base cluster. */ // [i] - next neighbor or END, [i + 1] - neighbor cluster index. final int END = -1; final IntStack neighborList = new IntStack(); neighborList.push(END); final int[] neighbors = new int[baseClusters.size()]; final float m = (float) mergeThreshold; for (int i = 0; i < baseClusters.size(); i++) { for (int j = i + 1; j < baseClusters.size(); j++) { final ClusterCandidate c1 = baseClusters.get(i); final ClusterCandidate c2 = baseClusters.get(j); final float a = c1.cardinality; final float b = c2.cardinality; final float c = BitSet.intersectionCount(c1.documents, c2.documents); if (c / a > m && c / b > m) { neighborList.push(neighbors[i], j); neighbors[i] = neighborList.size() - 2; neighborList.push(neighbors[j], i); neighbors[j] = neighborList.size() - 2; } } } /* * Find connected components in the similarity graph using Tarjan's algorithm * (flattened to use the stack instead of recursion). */ final int NO_INDEX = -1; final int[] merged = new int[baseClusters.size()]; Arrays.fill(merged, NO_INDEX); final ArrayList<ClusterCandidate> mergedClusters = Lists.newArrayListWithCapacity(baseClusters.size()); final IntStack stack = new IntStack(baseClusters.size()); final IntStack mergeList = new IntStack(baseClusters.size()); int mergedIndex = 0; for (int v = 0; v < baseClusters.size(); v++) { if (merged[v] != NO_INDEX) continue; // Recursively mark all connected components from an unmerged cluster. stack.push(v); while (stack.size() > 0) { final int c = stack.pop(); assert merged[c] == NO_INDEX || merged[c] == mergedIndex; if (merged[c] == mergedIndex) continue; merged[c] = mergedIndex; mergeList.push(c); for (int i = neighbors[c]; neighborList.get(i) != END; ) { final int neighbor = neighborList.get(i + 1); if (merged[neighbor] == NO_INDEX) { stack.push(neighbor); } else { assert merged[neighbor] == mergedIndex; } i = neighborList.get(i); } } mergedIndex++; /* * Aggregate documents from each base cluster of the current merge, compute * the score and labels. */ mergedClusters.add(merge(mergeList, baseClusters)); mergeList.clear(); } /* * Sort merged clusters. */ Collections.sort( mergedClusters, new Comparator<ClusterCandidate>() { public int compare(ClusterCandidate c1, ClusterCandidate c2) { if (c1.score < c2.score) return 1; if (c1.score > c2.score) return -1; if (c1.cardinality < c2.cardinality) return 1; if (c1.cardinality > c2.cardinality) return -1; return 0; }; }); if (mergedClusters.size() > maxClusters) { mergedClusters.subList(maxClusters, mergedClusters.size()).clear(); } return mergedClusters; }