edu.stanford.nlp.trees.tregex.TregexMatcher Java Examples

The following examples show how to use edu.stanford.nlp.trees.tregex.TregexMatcher. 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 vote down vote up
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 vote down vote up
/**
 * 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: TreeTransformer.java    From UDepLambda with Apache License 2.0 5 votes vote down vote up
private static String replaceNamedVars(TregexMatcher matcher, String original) {
  Pattern namedNodePattern = Pattern.compile("\\{(.+?)\\}");
  Matcher namedNodematcher = namedNodePattern.matcher(original);
  while (namedNodematcher.find()) {
    String namedNodeString = namedNodematcher.group(1);
    Tree namedNode = matcher.getNode(namedNodeString);
    original =
        original.replace(String.format("{%s}", namedNodeString),
            replaceSpecialChars(namedNode.label().value()));
    namedNodematcher = namedNodePattern.matcher(original);
  }
  return original;
}
 
Example #4
Source File: CoreNLPToJSON.java    From phrasal with GNU General Public License v3.0 5 votes vote down vote up
static void fillVectorWithYield(String[] vector, TregexMatcher tregexMatcher) {
  while (tregexMatcher.find()) {
    Tree match = tregexMatcher.getMatch();
    List<Tree> leaves = match.getLeaves();
    if (leaves.size() == 1) continue;
    boolean seenStart = false;
    for (Tree leaf : leaves) {
      int index = ((HasIndex) leaf.label()).index() - 1;
      if ( ! vector[index].equals("O")) break;
      vector[index] = seenStart ? "I" : "B";
      seenStart = true;
    }
  }
}
 
Example #5
Source File: RawFrenchToJSON.java    From phrasal with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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 #6
Source File: CoreNLPToJSON.java    From phrasal with GNU General Public License v3.0 4 votes vote down vote up
/**
   * 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;
  }