/** Same as semgrexFromGraph except the node traversal is ordered by sorting */ public static String semgrexFromGraphOrderedNodes(SemanticGraph sg, Collection<IndexedWord> wildcardNodes, Map<IndexedWord, String> nodeNameMap, Function<IndexedWord, String> wordTransformation) { IndexedWord patternRoot = sg.getFirstRoot(); StringWriter buf = new StringWriter(); Set<IndexedWord> tabu = Generics.newHashSet(); Set<SemanticGraphEdge> seenEdges = Generics.newHashSet(); buf.append(semgrexFromGraphHelper(patternRoot, sg, tabu, seenEdges, true, true, wildcardNodes, nodeNameMap, true, wordTransformation)); String patternString = buf.toString(); return patternString; }
/** * nodeValuesTranformation is a function that converts a vertex (IndexedWord) to the value. * For an example, see {@code semgrexFromGraph} * function implementations (if useWord and useTag is true, the value is "{word: vertex.word; tag: vertex.tag}"). * @throws Exception */ public static String semgrexFromGraph(SemanticGraph sg, Collection<IndexedWord> wildcardNodes, Map<IndexedWord, String> nodeNameMap, Function<IndexedWord, String> wordTransformation) { IndexedWord patternRoot = sg.getFirstRoot(); StringWriter buf = new StringWriter(); Set<IndexedWord> tabu = Generics.newHashSet(); Set<SemanticGraphEdge> seenEdges = Generics.newHashSet(); buf.append(semgrexFromGraphHelper(patternRoot, sg, tabu, seenEdges, true, true, wildcardNodes, nodeNameMap, false, wordTransformation)); String patternString = buf.toString(); return patternString; }
/** * Get a {@link SemgrexMatcher} for this pattern in this graph, with some * initial conditions on the variable assignments */ public SemgrexMatcher matcher(SemanticGraph sg, Map<String, IndexedWord> variables) { return matcher(sg, sg.getFirstRoot(), variables, Generics.newHashMap(), new VariableStrings(), false); }
/** * Returns a pretty-printed string representation of the given semantic graph, * on one or more lines. */ public String formatSemanticGraph(SemanticGraph sg) { if (sg.vertexSet().isEmpty()) { return "[]"; } out = new StringBuilder(); // not thread-safe!!! used = Generics.newHashSet(); if (sg.getRoots().size() == 1) { formatSGNode(sg, sg.getFirstRoot(), 1); } else { int index = 0; for (IndexedWord root: sg.getRoots()) { index+=1; out.append("root_").append(index).append("> "); formatSGNode(sg, root, 9); out.append("\n"); } } String result = out.toString(); if (!result.startsWith("[")) { result = "[" + result + "]"; } return result; }
/** * Return a Protobuf RelationTriple from a RelationTriple. */ public static CoreNLPProtos.SentenceFragment toProto(SentenceFragment fragment) { return CoreNLPProtos.SentenceFragment.newBuilder() .setAssumedTruth(fragment.assumedTruth) .setScore(fragment.score) .addAllTokenIndex(fragment.words.stream().map(x -> x.index() - 1).collect(Collectors.toList())) .setRoot(fragment.parseTree.getFirstRoot().index() - 1) .build(); }
/** * Strips aux and mark edges when we are splitting into a clause. * * @param toModify The tree we are stripping the edges from. */ private static void stripAuxMark(SemanticGraph toModify) { List<SemanticGraphEdge> toClean = new ArrayList<>(); for (SemanticGraphEdge edge : toModify.outgoingEdgeIterable(toModify.getFirstRoot())) { String rel = edge.getRelation().toString(); if (("aux".equals(rel) || "mark".equals(rel)) && !toModify.outgoingEdgeIterator(edge.getDependent()).hasNext()) { toClean.add(edge); } } for (SemanticGraphEdge edge : toClean) { toModify.removeEdge(edge); toModify.removeVertex(edge.getDependent()); } }
IndexedWord root = sg.getFirstRoot(); Set<IndexedWord> visitedNodes = Generics.newHashSet(); Queue<IndexedWord> queue = Generics.newLinkedList();
/** * Get a {@link SemgrexMatcher} for this pattern in this graph. * * @param sg * the SemanticGraph to match on * @return a SemgrexMatcher */ public SemgrexMatcher matcher(SemanticGraph sg) { return matcher(sg, sg.getFirstRoot(), Generics.newHashMap(), Generics.newHashMap(), new VariableStrings(), false); }
public SemgrexMatcher matcher(SemanticGraph hypGraph, Alignment alignment, SemanticGraph txtGraph) { return matcher(hypGraph, alignment, txtGraph, true, hypGraph.getFirstRoot(), Generics.newHashMap(), Generics.newHashMap(), new VariableStrings(), false); }
public SemgrexMatcher matcher(SemanticGraph hypGraph, Alignment alignment, SemanticGraph txtGraph, boolean ignoreCase) { return matcher(hypGraph, alignment, txtGraph, true, hypGraph.getFirstRoot(), Generics.newHashMap(), Generics.newHashMap(), new VariableStrings(), ignoreCase); }
/** * Get a {@link SemgrexMatcher} for this pattern in this graph. * * @param sg * the SemanticGraph to match on * @param ignoreCase * will ignore case for matching a pattern with a node; not * implemented by Coordination Pattern * @return a SemgrexMatcher */ public SemgrexMatcher matcher(SemanticGraph sg, boolean ignoreCase) { return matcher(sg, sg.getFirstRoot(), Generics.newHashMap(), Generics.newHashMap(), new VariableStrings(), ignoreCase); }
@Override public SemgrexMatcher matcher(SemanticGraph sg, Alignment alignment, SemanticGraph sg_align, boolean hyp, IndexedWord node, Map<String, IndexedWord> namesToNodes, Map<String, String> namesToRelations, VariableStrings variableStrings, boolean ignoreCase) { // log.info("making matcher: " + // ((reln.equals(GraphRelation.ALIGNED_ROOT)) ? false : hyp)); return new NodeMatcher(this, sg, alignment, sg_align, (reln.equals(GraphRelation.ALIGNED_ROOT)) ? false : hyp, (reln.equals(GraphRelation.ALIGNED_ROOT)) ? sg_align.getFirstRoot() : node, namesToNodes, namesToRelations, variableStrings, ignoreCase); }
.filter(vertex -> fragmentTree.getFirstRoot() != vertex && tree.getFirstRoot() != vertex && !fragmentTree.incomingEdgeIterable(vertex).iterator().hasNext()) .forEach(vertex -> { SemanticGraphEdge edge = tree.incomingEdgeIterable(vertex).iterator().next(); fragmentTree.addEdge(fragmentTree.getFirstRoot(), edge.getDependent(), edge.getRelation(), edge.getWeight(), edge.isExtra()); });
tree.removeVertex(entry.getKey()); addSubtree(tree, governor, incomingEdge.getRelation().toString(), this.tree, entry.getValue(), this.tree.incomingEdgeList(tree.getFirstRoot()));
if(enhancedDependency.getFirstRoot().equals(headIndexedWord)) return "root"; if(!enhancedDependency.containsVertex(getHeadParent())) return null; GrammaticalRelation relation = enhancedDependency.reln(getHeadParent(), headIndexedWord);
if(dependency.getFirstRoot().equals(headIndexedWord)) return "root"; if(!dependency.containsVertex(dependency.getParent(headIndexedWord))) return null; GrammaticalRelation relation = dependency.reln(dependency.getParent(headIndexedWord), headIndexedWord);
public SemanticGraph makeSoftCopy() { SemanticGraph newSg = new SemanticGraph(); if ( ! this.roots.isEmpty()) newSg.setRoot(this.getFirstRoot()); for (SemanticGraphEdge edge : this.edgeIterable()) { newSg.addEdge(edge.getSource(), edge.getTarget(), edge.getRelation(), edge.getWeight(), edge.isExtra()); } return newSg; }
IndexedWord root = parse.getFirstRoot(); if ( (root.lemma() != null && root.lemma().equalsIgnoreCase("be")) || (root.lemma() == null && ("is".equalsIgnoreCase(root.word()) ||
IndexedWord rootToken = sg.getFirstRoot(); if (rootToken == null || !rootToken.containsKey(CoreAnnotations.NamedEntityTagAnnotation.class)) { return;
assert to.edge == null || to.originalTree().getRoots().contains(to.edge.getGovernor()); feats.incrementCount(signature + "&at_root"); feats.incrementCount(signature + "&at_root&root_pos:" + to.originalTree().getFirstRoot().tag()); } else { feats.incrementCount(signature + "¬_root");