/** * Creates a new SpanTree with the given name and an empty SpanList as its root node. * * @param name the name of the span tree */ public SpanTree(String name) { this.name = name; setRoot(new SpanList()); }
private SpanNode copySpan(SpanNode spanTree) { if (spanTree instanceof Span) { return new Span((Span) spanTree); } else if (spanTree instanceof AlternateSpanList) { return new AlternateSpanList((AlternateSpanList) spanTree); } else if (spanTree instanceof SpanList) { return new SpanList((SpanList) spanTree); } else if (spanTree instanceof DummySpanNode) { return spanTree; //shouldn't really happen } else { throw new IllegalStateException("Cannot create copy of " + spanTree + " with class " + ((spanTree == null) ? "null" : spanTree.getClass())); } }
/** * Deep-copies a SpanList. * * @param other the SpanList to copy. */ public SpanList(SpanList other) { this.children = new LinkedList<SpanNode>(); for (SpanNode otherNode : other.children) { if (otherNode instanceof Span) { children.add(new Span((Span) otherNode)); } else if (otherNode instanceof AlternateSpanList) { children.add(new AlternateSpanList((AlternateSpanList) otherNode)); } else if (otherNode instanceof SpanList) { children.add(new SpanList((SpanList) otherNode)); } else if (otherNode instanceof DummySpanNode) { children.add(otherNode); //shouldn't really happen } else { throw new IllegalStateException("Cannot create copy of " + otherNode + " with class " + ((otherNode == null) ? "null" : otherNode.getClass())); } } }
public AlternateSpanList(AlternateSpanList otherSpanList) { super((List<SpanNode>) null); for (Children otherSubtree : otherSpanList.childTrees) { //create our own subtree: Children children = new Children(this); //copy nodes: for (SpanNode otherNode : otherSubtree.children()) { if (otherNode instanceof Span) { children.add(new Span((Span) otherNode)); } else if (otherNode instanceof AlternateSpanList) { children.add(new AlternateSpanList((AlternateSpanList) otherNode)); } else if (otherNode instanceof SpanList) { children.add(new SpanList((SpanList) otherNode)); } else if (otherNode instanceof DummySpanNode) { children.add(otherNode); //shouldn't really happen } else { throw new IllegalStateException("Cannot create copy of " + otherNode + " with class " + ((otherNode == null) ? "null" : otherNode.getClass())); } } //add this subtree to our subtrees: childTrees.add(children); } }
private SpanNode readSpanNode() { byte type = buf.get(); buf.position(buf.position() - 1); SpanNode retval; if ((type & Span.ID) == Span.ID) { retval = new Span(); if (spanNodes != null) { spanNodes.add(retval); } read((Span) retval); } else if ((type & SpanList.ID) == SpanList.ID) { retval = new SpanList(); if (spanNodes != null) { spanNodes.add(retval); } read((SpanList) retval); } else if ((type & AlternateSpanList.ID) == AlternateSpanList.ID) { retval = new AlternateSpanList(); if (spanNodes != null) { spanNodes.add(retval); } read((AlternateSpanList) retval); } else { throw new DeserializationException("Cannot read SpanNode of type " + type); } return retval; }
private SpanNode readSpanNode() { byte type = buf.get(); buf.position(buf.position() - 1); SpanNode retval; if ((type & Span.ID) == Span.ID) { retval = new Span(); if (spanNodes != null) { spanNodes.add(retval); } read((Span) retval); } else if ((type & SpanList.ID) == SpanList.ID) { retval = new SpanList(); if (spanNodes != null) { spanNodes.add(retval); } read((SpanList) retval); } else if ((type & AlternateSpanList.ID) == AlternateSpanList.ID) { retval = new AlternateSpanList(); if (spanNodes != null) { spanNodes.add(retval); } read((AlternateSpanList) retval); } else { throw new DeserializationException("Cannot read SpanNode of type " + type); } return retval; }
@Override protected void doExecute(ExecutionContext ctx) { StringFieldValue input = (StringFieldValue)ctx.getValue(); if (input.getString().isEmpty()) { return; } StringFieldValue output = input.clone(); ctx.setValue(output); String prev = output.getString(); String next = toLowerCase(prev); SpanList root = new SpanList(); SpanTree tree = new SpanTree(SpanTrees.LINGUISTICS, root); SpanNode node = new Span(0, prev.length()); tree.annotate(node, new Annotation(AnnotationTypes.TERM, next.equals(prev) ? null : new StringFieldValue(next))); tree.annotate(node, new Annotation(AnnotationTypes.TOKEN_TYPE, new IntegerFieldValue(TokenType.ALPHABETIC.getValue()))); root.add(node); output.setSpanTree(tree); }