当前位置: 首页>>代码示例>>Java>>正文


Java TregexPattern.matcher方法代码示例

本文整理汇总了Java中edu.stanford.nlp.trees.tregex.TregexPattern.matcher方法的典型用法代码示例。如果您正苦于以下问题:Java TregexPattern.matcher方法的具体用法?Java TregexPattern.matcher怎么用?Java TregexPattern.matcher使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在edu.stanford.nlp.trees.tregex.TregexPattern的用法示例。


在下文中一共展示了TregexPattern.matcher方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: findAntecendentInPredicateNominativeConstruction

import edu.stanford.nlp.trees.tregex.TregexPattern; //导入方法依赖的package包/类
/**
 * returns the antecedent NP or null
 * The way this method is called could be made more efficient.  
 * It doesn't really need to get called for every mention
 * 
 */
private static Mention findAntecendentInPredicateNominativeConstruction(Mention m, Document d) {
	Tree root = m.getSentence().rootNode();
	Tree node = m.node();
	
	TregexPattern pat = TregexPatternFactory.getPattern("S < NP=np1 <+(VP) (VP < (/^VB.*/ < be|is|was|were|are|being|been) < NP=np2)");
	TregexMatcher matcher = pat.matcher(root);
	while (matcher.find()) {
		if(matcher.getNode("np2") == node){
			Tree ante  = matcher.getNode("np1");
			for(Mention m2: d.mentions()){
				if(ante == m2.node()){
					return m2;
				}
			}
		}
	}
	
	return null;
}
 
开发者ID:hltfbk,项目名称:Excitement-TDMLEDA,代码行数:26,代码来源:Resolve.java

示例2: getRelatedNodes

import edu.stanford.nlp.trees.tregex.TregexPattern; //导入方法依赖的package包/类
/** Given a <code>Tree</code> node <code>t</code>, attempts to
 *  return a list of nodes to which node <code>t</code> has this
 *  grammatical relation.
 *
 *  @param t Target for finding governors of t related by this GR
 *  @param root The root of the Tree
 *  @return Governor nodes to which t bears this GR
 */
public Collection<Tree> getRelatedNodes(Tree t, Tree root) {
  if (root.value() == null) {
    root.setValue("ROOT");  // todo: cdm: it doesn't seem like this line should be here
  }
  Set<Tree> nodeList = new LinkedHashSet<Tree>();
  for (TregexPattern p : targetPatterns) {    // cdm: I deleted: && nodeList.isEmpty()
    TregexMatcher m = p.matcher(root);
    while (m.find()) {
      if (m.getMatch() == t) {
        nodeList.add(m.getNode("target"));
        //System.out.println("found " + this + "(" + t + ", " + m.getNode("target") + ")");
      }
    }
  }
  return nodeList;
}
 
开发者ID:FabianFriedrich,项目名称:Text2Process,代码行数:25,代码来源:GrammaticalRelation.java

示例3: getRelatedNodes

import edu.stanford.nlp.trees.tregex.TregexPattern; //导入方法依赖的package包/类
/** Given a {@code Tree} node {@code t}, attempts to
 *  return a list of nodes to which node {@code t} has this
 *  grammatical relation, with {@code t} as the governor.
 *
 *  @param t Target for finding dependents of t related by this GR
 *  @param root The root of the Tree
 *  @return A Collection of dependent nodes to which t bears this GR
 */
public Collection<Tree> getRelatedNodes(Tree t, Tree root) {
  Set<Tree> nodeList = new ArraySet<Tree>();
  for (TregexPattern p : targetPatterns) {    // cdm: I deleted: && nodeList.isEmpty()
    TregexMatcher m = p.matcher(root);
    while (m.findAt(t)) {
      nodeList.add(m.getNode("target"));
      if (DEBUG) {
        System.err.println("found " + this + "(" + t + ", " + m.getNode("target") + ") using pattern " + p);
        for (String nodeName : m.getNodeNames()) {
          if (nodeName.equals("target"))
            continue;
          System.err.println("  node " + nodeName + ": " + m.getNode(nodeName));
        }
      }
    }
  }
  return nodeList;
}
 
开发者ID:paulirwin,项目名称:Stanford.NER.Net,代码行数:27,代码来源:GrammaticalRelation.java

示例4: extractNPorPRP

import edu.stanford.nlp.trees.tregex.TregexPattern; //导入方法依赖的package包/类
protected void extractNPorPRP(CoreMap s, List<Mention> mentions, Set<IntPair> mentionSpanSet, Set<IntPair> namedEntitySpanSet) {
  List<CoreLabel> sent = s.get(CoreAnnotations.TokensAnnotation.class);
  Tree tree = s.get(TreeCoreAnnotations.TreeAnnotation.class);
  tree.indexLeaves();
  SemanticGraph dependency = s.get(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class);

  final String mentionPattern = "/^(?:NP|PRP)/";
  TregexPattern tgrepPattern = TregexPattern.compile(mentionPattern);
  TregexMatcher matcher = tgrepPattern.matcher(tree);
  while (matcher.find()) {
    Tree t = matcher.getMatch();
    List<Tree> mLeaves = t.getLeaves();
    int beginIdx = ((CoreLabel)mLeaves.get(0).label()).get(CoreAnnotations.IndexAnnotation.class)-1;
    int endIdx = ((CoreLabel)mLeaves.get(mLeaves.size()-1).label()).get(CoreAnnotations.IndexAnnotation.class);
    IntPair mSpan = new IntPair(beginIdx, endIdx);
    if(!mentionSpanSet.contains(mSpan) && !insideNE(mSpan, namedEntitySpanSet)) {
      int mentionID = assignIds? ++maxID:-1;
      Mention m = new Mention(mentionID, beginIdx, endIdx, dependency, new ArrayList<CoreLabel>(sent.subList(beginIdx, endIdx)), t);
      mentions.add(m);
      mentionSpanSet.add(mSpan);
    }
  }
}
 
开发者ID:benblamey,项目名称:stanford-nlp,代码行数:24,代码来源:RuleBasedCorefMentionFinder.java

示例5: findTreePattern

import edu.stanford.nlp.trees.tregex.TregexPattern; //导入方法依赖的package包/类
/** Find syntactic pattern in a sentence by tregex */
private void findTreePattern(Tree tree, String pattern, Set<Pair<Integer, Integer>> foundPairs) {
  try {
    TregexPattern tgrepPattern = TregexPattern.compile(pattern);
    TregexMatcher m = tgrepPattern.matcher(tree);
    while (m.find()) {
      Tree t = m.getMatch();
      Tree np1 = m.getNode("m1");
      Tree np2 = m.getNode("m2");
      Tree np3 = null;
      if(pattern.contains("m3")) np3 = m.getNode("m3");
      addFoundPair(np1, np2, t, foundPairs);
      if(np3!=null) addFoundPair(np2, np3, t, foundPairs);
    }
  } catch (Exception e) {
    // shouldn't happen....
    throw new RuntimeException(e);
  }
}
 
开发者ID:benblamey,项目名称:stanford-nlp,代码行数:20,代码来源:MentionExtractor.java

示例6: getRelatedNodes

import edu.stanford.nlp.trees.tregex.TregexPattern; //导入方法依赖的package包/类
/** Given a <code>Tree</code> node <code>t</code>, attempts to
 *  return a list of nodes to which node <code>t</code> has this
 *  grammatical relation.
 *
 *  @param t Target for finding governors of t related by this GR
 *  @param root The root of the Tree
 *  @return Governor nodes to which t bears this GR
 */
public Collection<Tree> getRelatedNodes(Tree t, Tree root) {
  if (root.value() == null) {
    root.setValue("ROOT");  // todo: cdm: it doesn't seem like this line should be here
  }
  Set<Tree> nodeList = new LinkedHashSet<Tree>();
  for (TregexPattern p : targetPatterns) {    // cdm: I deleted: && nodeList.isEmpty()
    TregexMatcher m = p.matcher(root);
    while (m.findAt(t)) {
      nodeList.add(m.getNode("target"));
      if (DEBUG) {
        System.err.println("found " + this + "(" + t + ", " + m.getNode("target") + ") using pattern " + p);
        for (String nodeName : m.getNodeNames()) {
          if (nodeName.equals("target")) 
            continue;
          System.err.println("  node " + nodeName + ": " + m.getNode(nodeName));
        }
      }
    }
  }
  return nodeList;
}
 
开发者ID:benblamey,项目名称:stanford-nlp,代码行数:30,代码来源:GrammaticalRelation.java

示例7: evaluateTregexPattern

import edu.stanford.nlp.trees.tregex.TregexPattern; //导入方法依赖的package包/类
public List<String> evaluateTregexPattern(String parseTree, String tregexPattern)
{
    List<String> foundMatches = new ArrayList<String>();

    TregexPattern pattern = TregexPattern.compile(tregexPattern);
    TregexMatcher matches = pattern.matcher(Tree.valueOf(parseTree));
    Set<String> nodes = matches.getNodeNames();
    while (matches.find())
    {
        foundMatches.add(matches.getMatch().pennString());
        for (String node : nodes)
        {
            foundMatches.add(matches.getNode(node).pennString());
        }
    }

    return foundMatches;
}
 
开发者ID:dmnapolitano,项目名称:stanford-thrift,代码行数:19,代码来源:StanfordTregexThrift.java

示例8: findHeadVerb

import edu.stanford.nlp.trees.tregex.TregexPattern; //导入方法依赖的package包/类
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();
}
 
开发者ID:Lambda-3,项目名称:Graphene,代码行数:11,代码来源:HeadVerbFinder.java

示例9: applyRuleOnNode

import edu.stanford.nlp.trees.tregex.TregexPattern; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:sivareddyg,项目名称:UDepLambda,代码行数:22,代码来源:TreeTransformer.java

示例10: getChunkVector

import edu.stanford.nlp.trees.tregex.TregexPattern; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:stanfordnlp,项目名称:phrasal,代码行数:34,代码来源:RawFrenchToJSON.java

示例11: isVadj

import edu.stanford.nlp.trees.tregex.TregexPattern; //导入方法依赖的package包/类
private boolean isVadj(ExtractorMatcher matcher) {

	  for(TregexPattern vadj : vadjs)
	  {
		  // check VP matches Vadj patterns
		  TregexMatcher m = vadj.matcher(matcher.trigger);
		  if (m.find() && matcher.trigger == m.getMatch()) {
			  matcher.trigger = getLastJJ(m.getNode("tr"));
			  return true;
		  }

		  // check VP rightmost child matches Vadj patterns
		  Tree rightMostVp = RightMostVp.rightMostVp(matcher.trigger);

		  m = vadj.matcher(rightMostVp);
		  if (m.find() && rightMostVp == m.getMatch()) {
			  matcher.trigger = getLastJJ(m.getNode("tr"));
			  return true;
		  }

		  // check VP rightmost child's children matches Vadj patterns
		  for (Tree child : rightMostVp.children()) {
			  m = vadj.matcher(child);
			  
			  if (m.find() && child == m.getMatch()) {
				  matcher.trigger = getLastJJ(m.getNode("tr"));
				  return true;
			  }
		  }
	  }

	  return false;
  }
 
开发者ID:leebird,项目名称:legonlp,代码行数:34,代码来源:ArgVadj.java

示例12: getCandidates

import edu.stanford.nlp.trees.tregex.TregexPattern; //导入方法依赖的package包/类
protected List<Entity> getCandidates(Entity entity, Treebank treebank, boolean intraSentence)
{
 List<Entity> entityList = new ArrayList<Entity>();
 
 for(Tree tree : treebank)
 {
  List<Tree> leaves = tree.getLeaves();
  OffsetLabel first = (OffsetLabel) leaves.get(0).label();
  OffsetLabel last = (OffsetLabel) leaves.get(leaves.size() - 1).label();
  int start = first.beginPosition();
  int end = last.endPosition();
  
  TregexPattern np = TregexPattern.compile("NP|NNP|NNPS|NN|NNS");
  TregexMatcher m = np.matcher(tree);
  while(m.find())
  {
	  Tree npTree = m.getMatch();
	  List<Token> tokens = Utils.getTokens(tree, npTree);

	  if(!npTree.isLeaf())
	  {
		  Entity candidate = new Entity("",npTree.nodeString(),tokens);
  
			if (entity.from() > candidate.to()) {
				if (intraSentence) {
					if ((entity.from() > start) && (entity.to() < end))
						entityList.add(candidate);
				} else
					entityList.add(candidate);
			}
	  }
  } 
 }
 return entityList;
}
 
开发者ID:leebird,项目名称:legonlp,代码行数:36,代码来源:ResoluteAnaphora.java

示例13: processPattern

import edu.stanford.nlp.trees.tregex.TregexPattern; //导入方法依赖的package包/类
/**
 * Tries to match a pattern against a tree.  If it succeeds, apply the surgical operations contained in a {@link TsurgeonPattern}.
 * @param matchPattern A {@link TregexPattern} to be matched against a {@link Tree}.
 * @param p A {@link TsurgeonPattern} to apply.
 * @param t the {@link Tree} to match against and perform surgery on.
 * @return t, which has been surgically modified.
 */
public static Tree processPattern(TregexPattern matchPattern, TsurgeonPattern p, Tree t) {
  TregexMatcher m = matchPattern.matcher(t);
  while(m.find()) {
    t = p.evaluate(t,m);
    if(t==null)
      break;
    m = matchPattern.matcher(t);
  }
  return t;
}
 
开发者ID:FabianFriedrich,项目名称:Text2Process,代码行数:18,代码来源:Tsurgeon.java

示例14: extractEnumerations

import edu.stanford.nlp.trees.tregex.TregexPattern; //导入方法依赖的package包/类
/** Extract enumerations (A, B, and C) */
protected void extractEnumerations(CoreMap s, List<Mention> mentions, Set<IntPair> mentionSpanSet, Set<IntPair> namedEntitySpanSet){
  List<CoreLabel> sent = s.get(CoreAnnotations.TokensAnnotation.class);
  Tree tree = s.get(TreeCoreAnnotations.TreeAnnotation.class);
  SemanticGraph dependency = s.get(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class);

  final String mentionPattern = "NP < (/^(?:NP|NNP|NML)/=m1 $.. (/^CC|,/ $.. /^(?:NP|NNP|NML)/=m2))";
  TregexPattern tgrepPattern = TregexPattern.compile(mentionPattern);
  TregexMatcher matcher = tgrepPattern.matcher(tree);
  Map<IntPair, Tree> spanToMentionSubTree = Generics.newHashMap();
  while (matcher.find()) {
    matcher.getMatch();
    Tree m1 = matcher.getNode("m1");
    Tree m2 = matcher.getNode("m2");

    List<Tree> mLeaves = m1.getLeaves();
    int beginIdx = ((CoreLabel)mLeaves.get(0).label()).get(CoreAnnotations.IndexAnnotation.class)-1;
    int endIdx = ((CoreLabel)mLeaves.get(mLeaves.size()-1).label()).get(CoreAnnotations.IndexAnnotation.class);
    spanToMentionSubTree.put(new IntPair(beginIdx, endIdx), m1);

    mLeaves = m2.getLeaves();
    beginIdx = ((CoreLabel)mLeaves.get(0).label()).get(CoreAnnotations.IndexAnnotation.class)-1;
    endIdx = ((CoreLabel)mLeaves.get(mLeaves.size()-1).label()).get(CoreAnnotations.IndexAnnotation.class);
    spanToMentionSubTree.put(new IntPair(beginIdx, endIdx), m2);
  }

  for(IntPair mSpan : spanToMentionSubTree.keySet()){
    if(!mentionSpanSet.contains(mSpan) && !insideNE(mSpan, namedEntitySpanSet)) {
      int mentionID = assignIds? ++maxID:-1;
      Mention m = new Mention(mentionID, mSpan.get(0), mSpan.get(1), dependency,
                              new ArrayList<CoreLabel>(sent.subList(mSpan.get(0), mSpan.get(1))), spanToMentionSubTree.get(mSpan));
      mentions.add(m);
      mentionSpanSet.add(mSpan);
    }
  }
}
 
开发者ID:benblamey,项目名称:stanford-nlp,代码行数:37,代码来源:RuleBasedCorefMentionFinder.java

示例15: checkPleonastic

import edu.stanford.nlp.trees.tregex.TregexPattern; //导入方法依赖的package包/类
private static boolean checkPleonastic(Mention m, Tree tree, String pattern) {
  try {
    TregexPattern tgrepPattern = TregexPattern.compile(pattern);
    TregexMatcher matcher = tgrepPattern.matcher(tree);
    while (matcher.find()) {
      Tree np1 = matcher.getNode("m1");
      if (((CoreLabel)np1.label()).get(CoreAnnotations.BeginIndexAnnotation.class)+1 == m.headWord.get(CoreAnnotations.IndexAnnotation.class)) {
        return true;
      }
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
  return false;
}
 
开发者ID:benblamey,项目名称:stanford-nlp,代码行数:16,代码来源:RuleBasedCorefMentionFinder.java


注:本文中的edu.stanford.nlp.trees.tregex.TregexPattern.matcher方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。