本文整理汇总了Java中edu.stanford.nlp.trees.tregex.TregexMatcher.getMatch方法的典型用法代码示例。如果您正苦于以下问题:Java TregexMatcher.getMatch方法的具体用法?Java TregexMatcher.getMatch怎么用?Java TregexMatcher.getMatch使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类edu.stanford.nlp.trees.tregex.TregexMatcher
的用法示例。
在下文中一共展示了TregexMatcher.getMatch方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getRelatedNodes
import edu.stanford.nlp.trees.tregex.TregexMatcher; //导入方法依赖的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;
}
示例2: apply
import edu.stanford.nlp.trees.tregex.TregexMatcher; //导入方法依赖的package包/类
public String apply(TregexMatcher m) {
if(mwCounter == null)
throw new RuntimeException("Cannot enable POSSequence features without POS sequence map. Use option -frenchMWMap.");
Tree t = m.getMatch();
StringBuilder sb = new StringBuilder();
for(Tree kid : t.children()) {
if( ! kid.isPreTerminal())
throw new RuntimeException("Not POS sequence for tree: " + t.toString());
String tag = doBasicCat ? tlp.basicCategory(kid.value()) : kid.value();
sb.append(tag).append(" ");
}
if(mwCounter.getCount(t.value(), sb.toString().trim()) > cutoff)
return annotationMark + sb.toString().replaceAll("\\s+", "").toLowerCase();
else
return "";
}
示例3: extractNPorPRP
import edu.stanford.nlp.trees.tregex.TregexMatcher; //导入方法依赖的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);
}
}
}
示例4: findTreePattern
import edu.stanford.nlp.trees.tregex.TregexMatcher; //导入方法依赖的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);
}
}
示例5: countMWEStatistics
import edu.stanford.nlp.trees.tregex.TregexMatcher; //导入方法依赖的package包/类
static public void countMWEStatistics(Tree t,
TwoDimensionalCounter<String, String> unigramTagger,
TwoDimensionalCounter<String, String> labelPreterm,
TwoDimensionalCounter<String, String> pretermLabel,
TwoDimensionalCounter<String, String> labelTerm,
TwoDimensionalCounter<String, String> termLabel)
{
updateTagger(unigramTagger,t);
//Count MWE statistics
TregexMatcher m = pMWE.matcher(t);
while (m.findNextMatchingNode()) {
Tree match = m.getMatch();
String label = match.value();
if(RESOLVE_DUMMY_TAGS && label.equals(FrenchTreeReader.MISSING_PHRASAL))
continue;
String preterm = Sentence.listToString(match.preTerminalYield());
String term = Sentence.listToString(match.yield());
labelPreterm.incrementCount(label,preterm);
pretermLabel.incrementCount(preterm,label);
labelTerm.incrementCount(label,term);
termLabel.incrementCount(term, label);
}
}
示例6: fillVectorWithYield
import edu.stanford.nlp.trees.tregex.TregexMatcher; //导入方法依赖的package包/类
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;
}
}
}
示例7: npppHead
import edu.stanford.nlp.trees.tregex.TregexMatcher; //导入方法依赖的package包/类
protected Tree npppHead(Tree t) {
TregexMatcher m = p0.matcher(t);
if (m.find() && t == m.getMatch()) {
return m.getNode("np1");
}
m = p01.matcher(t);
if (m.find() && t.getLeaves().get(0) == m.getMatch().getLeaves().get(0)) {
Tree np1 = m.getNode("np1");
Tree pp1 = m.getNode("pp");
Tree res = np1.deepCopy();
for(Tree child : pp1.children())
{
res.addChild(child);
}
return res;
}
m = p1.matcher(t);
if (m.find() && t == m.getMatch()) {
return m.getNode("np1");
}
m = p2.matcher(t);
if (m.find() && t == m.getMatch()) {
return m.getNode("np1");
}
m = p3.matcher(t);
if (m.find() && t == m.getMatch()) {
return m.getNode("np1");
}
return t;
}
示例8: match
import edu.stanford.nlp.trees.tregex.TregexMatcher; //导入方法依赖的package包/类
@Override
protected boolean match(ExtractorMatcher matcher,
TregexMatcher tregexMatcher,
Tree tree) {
boolean result = super.match(matcher, tregexMatcher, tree);
if (result) {
List<Tree> children = matcher.getMatch().getChildrenAsList();
int triggerIndex = children.indexOf(matcher.trigger);
int argIndex = children.indexOf(matcher.argument);
matcher.matched = tregexMatcher.getMatch();
TreeFactory tf = tregexMatcher.getMatch().treeFactory();
matcher.matched = tregexMatcher.getMatch().deepCopy(tf);
children = matcher.matched.getChildrenAsList();
matcher.trigger = tf.newTreeNode(
"NP",
children.subList(triggerIndex, children.size()));
matcher.argument = tf
.newTreeNode("NP", children.subList(0, argIndex + 1));
while (matcher.matched.numChildren() != 0) {
matcher.matched.removeChild(0);
}
matcher.matched.addChild(matcher.argument);
matcher.matched.addChild(matcher.trigger);
}
return result;
}
示例9: isVadj
import edu.stanford.nlp.trees.tregex.TregexMatcher; //导入方法依赖的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;
}
示例10: npppHead
import edu.stanford.nlp.trees.tregex.TregexMatcher; //导入方法依赖的package包/类
protected Tree npppHead(Tree t) {
// when extracting causal relation, do not use head only, because the arugment can be an event itself
// TregexMatcher m = p0.matcher(t);
// if (m.find() && t == m.getMatch()) {
//
// return m.getNode("np1");
// }
//
// m = p01.matcher(t);
//
// if (m.find() && t.getLeaves().get(0) == m.getMatch().getLeaves().get(0)) {
//
// Tree np1 = m.getNode("np1");
// Tree pp1 = m.getNode("pp");
// Tree res = np1.deepCopy();
// for(Tree child : pp1.children())
// {
// res.addChild(child);
// }
// return res;
// }
TregexMatcher m = p1.matcher(t);
if (m.find() && t == m.getMatch()) {
return m.getNode("np1");
}
m = p2.matcher(t);
if (m.find() && t == m.getMatch()) {
return m.getNode("np1");
}
m = p3.matcher(t);
if (m.find() && t == m.getMatch()) {
return m.getNode("np1");
}
return t;
}
示例11: getCandidates
import edu.stanford.nlp.trees.tregex.TregexMatcher; //导入方法依赖的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;
}
示例12: extractEnumerations
import edu.stanford.nlp.trees.tregex.TregexMatcher; //导入方法依赖的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);
}
}
}
示例13: getChunkVector
import edu.stanford.nlp.trees.tregex.TregexMatcher; //导入方法依赖的package包/类
/**
* 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("[email protected]=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;
}
示例14: npppHead
import edu.stanford.nlp.trees.tregex.TregexMatcher; //导入方法依赖的package包/类
protected Tree npppHead(Tree t) {
TregexMatcher m = p0.matcher(t);
if (m.find() && t == m.getMatch()) {
return m.getNode("np1");
}
m = p01.matcher(t);
if (m.find() && t.getLeaves().get(0) == m.getMatch().getLeaves().get(0)) {
Tree np1 = m.getNode("np1");
Tree pp1 = m.getNode("pp");
Tree res = np1.deepCopy();
for(Tree child : pp1.children())
{
// if SBAR in NP phrase, stop adding children
if(child.toString().contains("SBAR"))
break;
res.addChild(child);
}
return res;
}
m = p1.matcher(t);
if (m.find() && t == m.getMatch()) {
return m.getNode("np1");
}
m = p2.matcher(t);
if (m.find() && t == m.getMatch()) {
return m.getNode("np1");
}
m = p3.matcher(t);
if (m.find() && t == m.getMatch()) {
return m.getNode("np1");
}
return t;
}
示例15: isCoordination
import edu.stanford.nlp.trees.tregex.TregexMatcher; //导入方法依赖的package包/类
@Override
public boolean isCoordination() {
TregexMatcher m = conjPattern.matcher(coordination);
if (!m.find()) {
return false;
}
Tree parent = m.getMatch();
boolean hasCC = false;
conjuncts.clear();
int state = 0;
for (int i = 0; i < parent.numChildren(); i++) {
Tree child = parent.getChild(i);
switch (state) {
case 0:
if (isCC(child)) {
state = 1;
} else if (isComma(child)) {
return false;
} else {
conjuncts.add(new Pair<Integer, Tree>(i, child));
state = 2;
}
break;
case 1:
conjuncts.add(new Pair<Integer, Tree>(i, child));
state = 2;
break;
case 2:
if (isCC(child)) {
state = 4;
} else if (isComma(child)) {
state = 3;
} else {
return false;
}
break;
case 3:
if (isCC(child) && i != parent.numChildren() - 1) {
state = 4;
} else {
conjuncts.add(new Pair<Integer, Tree>(i, child));
state = 2;
}
break;
case 4:
if (child.value().equals("ADVP")
&& !conjuncts.get(0).second.value().equals("ADVP")) {
;
} else if (child.value().equals("RB")
&& !conjuncts.get(0).second.value().equals("RB")) {
;
} else {
hasCC = true;
conjuncts.add(new Pair<Integer, Tree>(i, child));
state = 5;
}
break;
default:
if (isComma(child)) {
} else {
hasCC = false;
}
}
}
if (!hasCC) {
return false;
}
// isomorphic
// only compare the first and last conjuncts
Tree first = conjuncts.getFirst().second;
Tree last = conjuncts.getLast().second;
if (first.numChildren() == 1 && last.numChildren() == 1) {
if (first.firstChild().isLeaf() && last.firstChild().isLeaf()) {
return true;
}
}
return isIsomorphic(first, last);
}