edu.stanford.nlp.trees.tregex.TregexPattern Java Examples
The following examples show how to use
edu.stanford.nlp.trees.tregex.TregexPattern.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HeadVerbFinder.java From Graphene with GNU General Public License v3.0 | 5 votes |
public Optional<String> findHeadVerb(Tree parseTree) { TregexPattern pattern = TregexPattern.compile("ROOT <<: (__ < (VP=vp [ <+(VP) (VP=lowestvp !< VP) | ==(VP=lowestvp !< VP) ]))"); TregexMatcher matcher = pattern.matcher(parseTree); while (matcher.findAt(parseTree)) { Tree lowestvp = matcher.getNode("lowestvp"); return Optional.of(ParseTreeExtractionUtils.getContainingWords(lowestvp).get(0).word()); } return Optional.empty(); }
Example #2
Source File: TreeTransformer.java From UDepLambda with Apache License 2.0 | 5 votes |
/** * Applies rule on the root node. * * @param rule the rule to be applied. * @param tree the tree in which the target node is present. * @param targetNode the target node on which the rule has to be applied. * @return Returns true if a rule group is successfully applied on the root * node. */ private static boolean applyRuleOnNode(TransformationRule rule, DependencyTree tree, DependencyTree targetNode) { TregexPattern tregex = rule.getTregex(); TregexMatcher matcher = tregex.matcher(tree); if (matcher.matchesAt(targetNode)) { for (Transformation transformation : rule.getTransformationList()) { applyTransformation(transformation, matcher); } return true; } return false; }
Example #3
Source File: RawFrenchToJSON.java From phrasal with GNU General Public License v3.0 | 5 votes |
/** * Extract chunks. * * @param tree * @return */ static int[] getChunkVector(Tree tree) { String[] iobVector = new String[tree.yield().size()]; Arrays.fill(iobVector, "O"); // NOTE: The order in which these patterns are applied is important. // Base XPs TregexPattern baseXPPattern = TregexPattern.compile("__ < (__ < (__ !< __)) !< (__ < (__ < __))"); // Non-recursive NPs TregexPattern NPPattern = TregexPattern.compile("@NP < (__ $ __) !<< (@NP < (__ $ __)) !<< @PP"); // Non-recursive PPs TregexPattern PPattern = TregexPattern.compile("@PP !<< @PP"); TregexMatcher tregexMatcher = baseXPPattern.matcher(tree); CoreNLPToJSON.fillVectorWithYield(iobVector, tregexMatcher); tregexMatcher = NPPattern.matcher(tree); CoreNLPToJSON.fillVectorWithYield(iobVector, tregexMatcher); tregexMatcher = PPattern.matcher(tree); CoreNLPToJSON.fillVectorWithYield(iobVector, tregexMatcher); int[] indexVector = CoreNLPToJSON.iobToIndices(iobVector); return indexVector; }
Example #4
Source File: TransformationRuleGroups.java From UDepLambda with Apache License 2.0 | 4 votes |
public TregexPattern getTregex(String pattern) { tregexCache.putIfAbsent(pattern, patternCompiler.compile(pattern)); return tregexCache.get(pattern); }
Example #5
Source File: TransformationRule.java From UDepLambda with Apache License 2.0 | 4 votes |
public TregexPattern getTregex() { return tregex; }
Example #6
Source File: CoreNLPToJSON.java From phrasal with GNU General Public License v3.0 | 4 votes |
/** * Extract chunks. * * @param tree * @return */ private static int[] getChunkVector(Tree tree) { String[] iobVector = new String[tree.yield().size()]; Arrays.fill(iobVector, "O"); // Yield patterns // TregexPattern baseNPPattern = TregexPattern.compile("@NP < (/NN/ < (__ !< __)) !< @NP"); TregexPattern baseXPPattern = TregexPattern.compile("__ < (__ < (__ !< __)) !< (__ < (__ < __))"); TregexPattern basePPPattern = TregexPattern.compile("@PP <, @IN !<< @NP >! @PP"); TregexMatcher tregexMatcher = baseXPPattern.matcher(tree); fillVectorWithYield(iobVector, tregexMatcher); tregexMatcher = basePPPattern.matcher(tree); fillVectorWithYield(iobVector, tregexMatcher); // Edge patterns TregexPattern vpPattern = TregexPattern.compile("@VP >! @VP"); TregexPattern argumentPattern = TregexPattern.compile("!@VP=node > @VP !< (__ !< __)"); TregexPattern puncPattern = TregexPattern.compile("/^[^a-zA-Z0-9]+$/=node < __ "); TsurgeonPattern p = Tsurgeon.parseOperation("delete node"); tregexMatcher = vpPattern.matcher(tree); while (tregexMatcher.find()) { Tree match = tregexMatcher.getMatch(); Tsurgeon.processPattern(argumentPattern, p, match); Tsurgeon.processPattern(puncPattern, p, match); List<Tree> leaves = match.getLeaves(); if (leaves.size() == 1) continue; boolean seenStart = false; int lastIndex = -1; for (Tree leaf : leaves) { int index = ((HasIndex) leaf.label()).index() - 1; if (index < 0 || index >= iobVector.length) { System.err.println("ERROR: Mangled subtree: " + match.toString()); continue; } if (lastIndex > 0 && index - lastIndex != 1) break; if ( ! iobVector[index].equals("O")) break; iobVector[index] = seenStart ? "I" : "B"; seenStart = true; lastIndex = index; } } int[] indexVector = iobToIndices(iobVector); return indexVector; }