/** * 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()); }
/** * Moves a child of this SpanList to another SpanList. * * @param nodeNum the index of the node to move * @param target the SpanList to add the node to * @throws IndexOutOfBoundsException if the given index is out of range */ public void move(int nodeNum, SpanList target) { SpanNode node = children().remove(nodeNum); if (node != null) { //we found the node node.setParent(null); resetCachedFromAndTo(); target.add(node); } }
/** * Returns the character index where this {@link SpanNode} starts (inclusive), i.e. the smallest {@link com.yahoo.document.annotation.SpanNode#getFrom()} of all children. * * @return the lowest getFrom() of all children, or -1 if this SpanList has no children. */ @Override public int getFrom() { if (children().isEmpty()) { return -1; } if (cachedFrom == Integer.MIN_VALUE) { calculateFrom(); } return cachedFrom; }
void resetCachedFromAndTo() { cachedFrom = Integer.MIN_VALUE; cachedTo = Integer.MIN_VALUE; if (getParent() instanceof SpanList) { ((SpanList) getParent()).resetCachedFromAndTo(); } }
/** * Recursively sorts all children by occurrence in the text covered. */ public void sortChildrenRecursive() { for (SpanNode node : children()) { if (node instanceof SpanList) { ((SpanList) node).sortChildrenRecursive(); } Collections.sort(children()); } }
/** Create a span, add it to this list and return it */ public Span span(int from, int length) { Span span=new Span(from,length); add(span); return span; }
@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); }
/** Removes and invalidates all references to child nodes. */ public void clearChildren() { for (SpanNode node : children()) { node.setInvalid(); node.setParent(null); } children().clear(); resetCachedFromAndTo(); }
/** * Adds a child node to this SpanList. * * @param node the node to add. * @return this, for call chaining * @throws IllegalStateException if SpanNode.isValid() returns false. */ public SpanList add(SpanNode node) { checkValidity(node, children()); node.setParent(this); resetCachedFromAndTo(); children().add(node); return this; }
@Override public String toString() { return "SpanList with " + children().size() + " children"; } }
public void write(SpanList spanList) { buf.put(SpanList.ID); buf.putInt1_2_4Bytes(spanList.numChildren()); Iterator<SpanNode> children = spanList.childIterator(); while (children.hasNext()) { write(children.next()); } }
/** * Returns the character index where this {@link SpanNode} ends (exclusive), i.e. the greatest {@link com.yahoo.document.annotation.SpanNode#getTo()} of all children. * * @return the greatest getTo() of all children, or -1 if this SpanList has no children. */ @Override public int getTo() { if (children().isEmpty()) { return -1; } if (cachedTo == Integer.MIN_VALUE) { calculateTo(); } return cachedTo; }
@Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof AlternateSpanList)) return false; if (!super.equals(o)) return false; AlternateSpanList that = (AlternateSpanList) o; if (!childTrees.equals(that.childTrees)) return false; return true; }
/** * Moves a child of this SpanList to another SpanList. * * @param i the index of the subtree to remove the node from * @param node the node to move * @param target the SpanList to add the node to * @throws IllegalArgumentException if the given node is not a child of this SpanList */ public void move(int i, SpanNode node, SpanList target) { boolean removed = children(i).remove(node); if (removed) { //we found the node node.setParent(null); resetCachedFromAndTo(); target.add(node); } else { throw new IllegalArgumentException("Node " + node + " is not a child of this SpanList, cannot move."); } }
/** * Removes and invalidates the given SpanNode from this. * * @param node the node to remove. * @return this, for chaining. */ public SpanList remove(SpanNode node) { boolean removed = children().remove(node); if (removed) { node.setParent(null); resetCachedFromAndTo(); node.setInvalid(); } return this; }
/** * Returns the number of children this SpanList holds. * * @return the number of children this SpanList holds. */ public int numChildren() { return children().size(); }
public void write(SpanList spanList) { buf.put(SpanList.ID); buf.putInt1_2_4Bytes(spanList.numChildren()); Iterator<SpanNode> children = spanList.childIterator(); while (children.hasNext()) { write(children.next()); } }
/** * Moves a child of this SpanList to another SpanList. * * @param node the node to move * @param target the SpanList to add the node to * @throws IllegalArgumentException if the given node is not a child of this SpanList */ public void move(SpanNode node, SpanList target) { boolean removed = children().remove(node); if (removed) { //we found the node node.setParent(null); resetCachedFromAndTo(); target.add(node); } else { throw new IllegalArgumentException("Node " + node + " is not a child of this SpanList, cannot move."); } }
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())); } }