in opennlp-tools/src/main/java/opennlp/tools/parser/Parse.java [825:879]
public static Parse parseParse(String parse, GapLabeler gl) {
StringBuilder text = new StringBuilder();
int offset = 0;
Stack<Constituent> stack = new Stack<>();
List<Constituent> cons = new LinkedList<>();
for (int ci = 0, cl = parse.length(); ci < cl; ci++) {
char c = parse.charAt(ci);
if (c == '(') {
String rest = parse.substring(ci + 1);
String type = getType(rest);
if (type == null) {
logger.warn("null type for: {}", rest);
}
String token = getToken(rest);
stack.push(new Constituent(type, new Span(offset, offset)));
if (token != null) {
if (Objects.equals(type, "-NONE-") && gl != null) {
if (logger.isTraceEnabled()) {
logger.trace("stack.size={}", stack.size());
}
gl.labelGaps(stack);
} else {
cons.add(new Constituent(AbstractBottomUpParser.TOK_NODE,
new Span(offset, offset + token.length())));
text.append(token).append(" ");
offset += token.length() + 1;
}
}
} else if (c == ')') {
Constituent con = stack.pop();
int start = con.getSpan().getStart();
if (start < offset) {
cons.add(new Constituent(con.getLabel(), new Span(start, offset - 1)));
}
}
}
String txt = text.toString();
int tokenIndex = -1;
Parse p = new Parse(txt, new Span(0, txt.length()), AbstractBottomUpParser.TOP_NODE, 1, 0);
for (Constituent con : cons) {
String type = con.getLabel();
if (!type.equals(AbstractBottomUpParser.TOP_NODE)) {
if (AbstractBottomUpParser.TOK_NODE.equals(type)) {
tokenIndex++;
}
Parse c = new Parse(txt, con.getSpan(), type, 1, tokenIndex);
if (logger.isTraceEnabled()) {
logger.trace("insert[{}] {} {} {}", tokenIndex, type, c, c.hashCode());
}
p.insert(c);
//codeTree(p);
}
}
return p;
}