Java Code Examples for opennlp.tools.parser.Parse#getChildren()

The following examples show how to use opennlp.tools.parser.Parse#getChildren() . 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: ParserExtractor.java    From knowledge-extraction with Apache License 2.0 6 votes vote down vote up
public static String getSubject(final Parse parse) {
	if (parse.getType().equals(LABEL_TOP)) {
		return getSubject(parse.getChildren()[0]);
	}

	if (parse.getType().equals(LABEL_SENTENCE)) {
		for (Parse child : parse.getChildren()) {
			if (child.getType().equals(LABEL_NOUN_PHRASE)) {
				return getSubject(child);
			}
		}
	}
	if (parse.getType().equals(LABEL_NOUN_PHRASE)) {
		return getFirstOccurenceForType(parse, LABEL_NAME_PREFIX);
	}

	return "";
}
 
Example 2
Source File: ParserExtractor.java    From knowledge-extraction with Apache License 2.0 6 votes vote down vote up
public static String getPredicate(final Parse parse) {
	if (parse.getType().equals(LABEL_TOP)) {
		return getPredicate(parse.getChildren()[0]);
	}

	if (parse.getType().equals(LABEL_SENTENCE)) {
		for (Parse child : parse.getChildren()) {
			if (child.getType().equals(LABEL_VERBAL_PHRASE)) {
				return getPredicate(child);
			}
		}
		return "";
	}
	if (parse.getType().equals(LABEL_VERBAL_PHRASE)) {
		return getFirstOccurenceForType(parse, LABEL_VERB_PREFIX);
	}

	return "";
}
 
Example 3
Source File: ParserExtractor.java    From knowledge-extraction with Apache License 2.0 6 votes vote down vote up
public static String getObject(final Parse parse) {
	String object = "";
	if (parse.getType().equals(LABEL_TOP)) {
		return getObject(parse.getChildren()[0]);
	}

	if (parse.getType().equals(LABEL_SENTENCE)) {
		for (Parse child : parse.getChildren()) {
			if (child.getType().equals(LABEL_VERBAL_PHRASE)) {
				object = getObject(child); 
				if (!object.isEmpty()){
					return object;
				}
			}
		}
		return object;
	}
	if (parse.getType().equals(LABEL_VERBAL_PHRASE)) {
		return getFirstOccurenceForType(parse, LABEL_NAME_PREFIX);
	}

	return object;
}
 
Example 4
Source File: ParserExtractor.java    From knowledge-extraction with Apache License 2.0 6 votes vote down vote up
public static String getConstituent(final Parse parse, final String syntactic_cat,
		String lexical_cat) {
	String object = "";
	if (parse.getType().equals(LABEL_TOP)) {
		return getConstituent(parse.getChildren()[0], syntactic_cat, lexical_cat);
	}

	if (parse.getType().equals(LABEL_SENTENCE)) {
		for (Parse child : parse.getChildren()) {
			if (child.getType().equals(syntactic_cat)) {
				object = getConstituent(child, syntactic_cat, lexical_cat); 
				if (!object.isEmpty()){
					return object;
				}
			}
		}
		return object;
	}
	if (parse.getType().equals(syntactic_cat)) {
		return getFirstOccurenceForType(parse, lexical_cat);
	}

	return object;
}
 
Example 5
Source File: CorefParse.java    From knowledge-extraction with Apache License 2.0 6 votes vote down vote up
private void print(Parse p, int deep) {
	if (p.getType().length() > 1 && p.getType().substring(0, 2).equals(Parser.TOK_NODE))
		return;
	
	char[] spaces = new char[deep*2];
	Arrays.fill(spaces, ' ');
	Span span = p.getSpan();
    System.out.print(new String(spaces) + p.getType() + " -- " + p.getText().substring(span.getStart(),
			span.getEnd()));
    if (parseMap.containsKey(p)) {
		System.out.print("#" + parseMap.get(p));
	}
    System.out.print("\n");
    for (Parse child : p.getChildren()) {
    	print(child, new Integer(deep + 1));
    }
}
 
Example 6
Source File: JM_Scorer.java    From uncc2014watsonsim with GNU General Public License v2.0 6 votes vote down vote up
public double matchChildren(Parse pa1, Parse pa2) {
	String p1NodeLabel = pa1.getLabel();
	String p2NodeLabel = pa2.getLabel();
	Parse[] children1 = pa1.getChildren();
	Parse[] children2 = pa2.getChildren();
	double matchFound = 0;
	
	if (pa1 == null || pa2 == null) {
		return 0;
	}
	
	if (p1NodeLabel.equals(p2NodeLabel)) {
		if (pa1.getCoveredText().equals(pa2.getCoveredText())) {
			matchFound = 1;
		}
	}
	
	return matchFound + matchChildren(children1[0], children2[0]) + matchChildren(children1[1], children2[1]);
}
 
Example 7
Source File: Utils.java    From knowledge-extraction with Apache License 2.0 5 votes vote down vote up
public static void printParseTree(Parse p, int deep) {
if (p.getType().length() > 1 && p.getType().substring(0, 2).equals(Parser.TOK_NODE))
	return;

char[] spaces = new char[deep*2];
Arrays.fill(spaces, ' ');
Span span = p.getSpan();
      System.out.println(new String(spaces) + p.getType() + " -- " + p.getText().substring(span.getStart(),
		span.getEnd()));
      for (Parse child : p.getChildren()) {
      	printParseTree(child, new Integer(deep + 1));
      }
  }
 
Example 8
Source File: CorefParse.java    From knowledge-extraction with Apache License 2.0 5 votes vote down vote up
private void show(Parse p) {
	int start;
	start = p.getSpan().getStart();
	if (!p.getType().equals(Parser.TOK_NODE)) {
		System.out.print("(");
		System.out.print(p.getType());
		if (parseMap.containsKey(p)) {
			System.out.print("#" + parseMap.get(p));
		}
		// System.out.print(p.hashCode()+"-"+parseMap.containsKey(p));
		System.out.print(" ");
	}
	Parse[] children = p.getChildren();
	for (int pi = 0, pn = children.length; pi < pn; pi++) {
		Parse c = children[pi];
		Span s = c.getSpan();
		if (start < s.getStart()) {
			System.out.print(p.getText().substring(start, s.getStart()));
		}
		show(c);
		start = s.getEnd();
	}
	System.out.print(p.getText().substring(start, p.getSpan().getEnd()));
	if (!p.getType().equals(Parser.TOK_NODE)) {
		System.out.print(")");
	}
}
 
Example 9
Source File: NERScorer.java    From uncc2014watsonsim with GNU General Public License v2.0 5 votes vote down vote up
public Parse[] getAllChildren(Parse parse){
	Parse[] allChildren = new Parse[1];
	allChildren[0]=parse;
	Parse[] allChldr;
	Parse[] children = parse.getChildren();			
	allChldr= getAllChildren(children);
	allChildren  =ArrayUtils.addAll(allChildren, allChldr);
	return allChildren;		
}
 
Example 10
Source File: OpenNlpTests.java    From uncc2014watsonsim with GNU General Public License v2.0 5 votes vote down vote up
public Parse[] getAllChildren(Parse parse){
	Parse[] allChildren = new Parse[1];
	allChildren[0]=parse;
	Parse[] allChldr;
	Parse[] children = parse.getChildren();			
	allChldr= getAllChildren(children);
	allChildren  =ArrayUtils.addAll(allChildren, allChldr);
	return allChildren;		
}
 
Example 11
Source File: NETagger.java    From OpenEphyra with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Recursive method called by <code>extractNes(Parse)</code> to extract NEs
 * from a parse tree augmented with NE tags.
 * 
 * @param parse a node of a parse tree
 * @param nes NEs found so far
 */
private static void extractNesRec(Parse parse, ArrayList<String>[] nes) {
	String type = parse.getType();
	if (type.startsWith("NE")) {
		String text = parse.getText().substring(parse.getSpan().getStart(),
												parse.getSpan().getEnd());
		nes[getNeIds(type)[0]].add(text.trim());
	}
	
	for (Parse child : parse.getChildren())
		extractNesRec(child, nes);
}
 
Example 12
Source File: NETagger.java    From OpenEphyra with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds named entity information to parses.
 * 
 * @param tag named entity type
 * @param names spans of tokens that are named entities
 * @param tokens parses for the tokens
 */
private static void addNames(String tag, List names, Parse[] tokens) {
	for (int i = 0; i < names.size(); i++) {
		Span nameTokenSpan = (Span) names.get(i);
		Parse startToken = tokens[nameTokenSpan.getStart()];
		Parse endToken = tokens[nameTokenSpan.getEnd()];
		Parse commonP = startToken.getCommonParent(endToken);
		
		if (commonP != null) {
			Span nameSpan = new Span(startToken.getSpan().getStart(),
									 endToken.getSpan().getEnd());
			
			if (nameSpan.equals(commonP.getSpan())) {
				// common parent matches exactly the named entity
				commonP.insert(new Parse(commonP.getText(), nameSpan, tag,
						1.0));
			} else {
				// common parent includes the named entity
				Parse[] kids = commonP.getChildren();
				boolean crossingKids = false;
				
				for (int j = 0; j < kids.length; j++)
					if (nameSpan.crosses(kids[j].getSpan()))
						crossingKids = true;
				
				if (!crossingKids) {
					// named entity does not cross children
					commonP.insert(new Parse(commonP.getText(), nameSpan,
							tag, 1.0));
				} else {
					// NE crosses children
					if (commonP.getType().equals("NP")) {
						Parse[] grandKids = kids[0].getChildren();
						
						Parse last = grandKids[grandKids.length - 1];
						if (grandKids.length > 1 &&
							nameSpan.contains(last.getSpan()))
							commonP.insert(new Parse(commonP.getText(),
									commonP.getSpan(), tag,1.0));
					}
				}
			}
		}
	}
}