use of gate.Node in project gate-core by GateNLP.
the class AnnotationSetImpl method getStrict.
// get(startOfset, endOffset)
/**
* Select annotations by offset. This returns the set of annotations that
* overlap strictly with the interval defined by the two provided offsets.The
* result will include all the annotations that start at the start offset and
* end strictly at the end offset
*/
public AnnotationSet getStrict(Long startOffset, Long endOffset) {
// start at the start offset and end strictly at the end offset
if (annotsByStartNode == null)
indexByStartOffset();
List<Annotation> annotationsToAdd = null;
Iterator<Annotation> annotsIter;
Node currentNode;
Annotation currentAnnot;
// find all the annots that start at the start offset
currentNode = nodesByOffset.get(startOffset);
if (currentNode != null) {
Collection<Annotation> objFromPoint = getAnnotsByStartNode(currentNode.getId());
if (objFromPoint != null) {
annotsIter = objFromPoint.iterator();
while (annotsIter.hasNext()) {
currentAnnot = annotsIter.next();
if (currentAnnot.getEndNode().getOffset().compareTo(endOffset) == 0) {
if (annotationsToAdd == null)
annotationsToAdd = new ArrayList<Annotation>();
annotationsToAdd.add(currentAnnot);
}
// if
}
// while
}
// if
}
// if
return new ImmutableAnnotationSetImpl(doc, annotationsToAdd);
}
use of gate.Node in project gate-core by GateNLP.
the class AnnotationSetImpl method addToStartOffsetIndex.
// addToTypeIndex(a)
/**
* Add an annotation to the start offset index. Does nothing if the index
* doesn't exist.
*/
@SuppressWarnings("unchecked")
void addToStartOffsetIndex(Annotation a) {
Node startNode = a.getStartNode();
Node endNode = a.getEndNode();
Long start = startNode.getOffset();
Long end = endNode.getOffset();
// add a's nodes to the offset index
if (nodesByOffset != null) {
nodesByOffset.put(start, startNode);
nodesByOffset.put(end, endNode);
}
// add marking for longest annot
long annotLength = end - start;
if (annotLength > longestAnnot)
longestAnnot = annotLength;
// if there's no appropriate index give up
if (annotsByStartNode == null)
return;
// get the annotations that start at the same node, or create new
// set
Object thisNodeObject = annotsByStartNode.get(startNode.getId());
if (thisNodeObject == null) {
// put directly the annotation
annotsByStartNode.put(startNode.getId(), a);
} else {
// already something there : a single Annotation or a
// Collection
Set<Annotation> newCollection = null;
if (thisNodeObject instanceof Annotation) {
// at this Node
if (thisNodeObject.equals(a))
return;
newCollection = new HashSet<Annotation>(3);
newCollection.add((Annotation) thisNodeObject);
annotsByStartNode.put(startNode.getId(), newCollection);
} else
newCollection = (Set<Annotation>) thisNodeObject;
// get the existing set
// add the new node annotation
newCollection.add(a);
}
}
use of gate.Node in project gate-core by GateNLP.
the class DocumentImpl method identifyTheRootAnnotation.
// writeStartTag()
/**
* Identifies the root annotations inside an annotation set. The root
* annotation is the one that starts at offset 0, and has the greatest span.
* If there are more than one with this function, then the annotation with the
* smalled ID wil be selected as root. If none is identified it will return
* null.
*
* @param anAnnotationSet
* The annotation set possibly containing the root annotation.
* @return The root annotation or null is it fails
*/
@SuppressWarnings("unused")
private Annotation identifyTheRootAnnotation(AnnotationSet anAnnotationSet) {
if (anAnnotationSet == null)
return null;
// If the starting node of this annotation is not null, then the annotation
// set will not have a root annotation.
Node startNode = anAnnotationSet.firstNode();
Node endNode = anAnnotationSet.lastNode();
// offset equal to 0.
if (startNode.getOffset().longValue() != 0)
return null;
// Go anf find the annotation.
Annotation theRootAnnotation = null;
// Check if there are annotations starting at offset 0. If there are, then
// check all of them to see which one has the greatest span. Basically its
// END offset should be the bigest offset from the input annotation set.
long start = startNode.getOffset().longValue();
long end = endNode.getOffset().longValue();
for (Iterator<Annotation> it = anAnnotationSet.iterator(); it.hasNext(); ) {
Annotation currentAnnot = it.next();
// end of the AnnotationSet then check to see if its ID is the smallest.
if ((start == currentAnnot.getStartNode().getOffset().longValue()) && (end == currentAnnot.getEndNode().getOffset().longValue())) {
// The currentAnnotation has is a potencial root one.
if (theRootAnnotation == null)
theRootAnnotation = currentAnnot;
else {
// If its ID is greater that the currentAnnot then update the root
if (theRootAnnotation.getId().intValue() > currentAnnot.getId().intValue())
theRootAnnotation = currentAnnot;
}
// End if
}
// End if
}
// End for
return theRootAnnotation;
}
use of gate.Node in project gate-core by GateNLP.
the class AnnotationSetImpl method readObject.
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
this.longestAnnot = 0l;
ObjectInputStream.GetField gf = in.readFields();
this.name = (String) gf.get("name", null);
this.doc = (DocumentImpl) gf.get("doc", null);
boolean isIndexedByType = false;
boolean isIndexedByStartNode = false;
this.annotations = (Annotation[]) gf.get("annotations", null);
if (this.annotations == null) {
// old style serialised version
@SuppressWarnings("unchecked") Map<Integer, Annotation> annotsByIdMap = (Map<Integer, Annotation>) gf.get("annotsById", null);
if (annotsByIdMap == null)
throw new IOException("Invalid serialised data: neither annotations array or map by id" + " are present.");
annotations = annotsByIdMap.values().toArray(new Annotation[] {});
} else {
// new style serialised version
isIndexedByType = in.readBoolean();
isIndexedByStartNode = in.readBoolean();
}
// this.name = (String)in.readObject();
// this.doc = (DocumentImpl)in.readObject();
// Annotation[] annotations = (Annotation[])in.readObject();
// do we need to create the indices?
// boolean isIndexedByType = in.readBoolean();
// boolean isIndexedByStartNode = in.readBoolean();
this.annotsById = new HashMap<Integer, Annotation>(annotations.length);
// rebuilds the indices if required
if (isIndexedByType) {
annotsByType = new HashMap<String, AnnotationSet>(Gate.HASH_STH_SIZE);
}
if (isIndexedByStartNode) {
nodesByOffset = new RBTreeMap<Long, Node>();
annotsByStartNode = new HashMap<Integer, Object>(annotations.length);
}
// add all the annotations one by one
for (int i = 0; i < annotations.length; i++) {
add(annotations[i]);
}
this.relations = (RelationSet) gf.get("relations", null);
annotations = null;
}
use of gate.Node in project gate-core by GateNLP.
the class AnnotationSetImpl method getNodes.
/**
* Returns the nodes corresponding to the Longs. The Nodes are created if
* they don't exist.
*/
private final Node[] getNodes(Long start, Long end) throws InvalidOffsetException {
// are the offsets valid?
if (!doc.isValidOffsetRange(start, end)) {
throw new InvalidOffsetException("Offsets [" + start + ":" + end + "] not valid for this document of size " + doc.getContent().size());
}
// to find out if nodes need creating or if they exist already
if (nodesByOffset == null) {
indexByStartOffset();
}
// find existing nodes if appropriate nodes don't already exist,
// create them
Node startNode = nodesByOffset.get(start);
if (startNode == null)
startNode = new NodeImpl(doc.getNextNodeId(), start);
Node endNode = null;
if (start.equals(end)) {
endNode = startNode;
return new Node[] { startNode, endNode };
}
endNode = nodesByOffset.get(end);
if (endNode == null)
endNode = new NodeImpl(doc.getNextNodeId(), end);
return new Node[] { startNode, endNode };
}
Aggregations