fireStatusChanged("FeatureLanguageAnalyserPR processing: "
+ getDocument().getName());
if (!(document instanceof DocumentImpl)) {
throw new GateRuntimeException("Can only handle DocumentImpl not " +
document.getClass());
}
// Get the annotations in document order
AnnotationSet anns =
document.getAnnotations(inputSpecificationSet).get(inputSpecificationType);
List<Annotation> annlist = gate.Utils.inDocumentOrder(anns);
List<AnnotationSpec> annspecs = new LinkedList<AnnotationSpec>();
StringBuilder newText = new StringBuilder();
long curoffset = 0;
// System.err.println("Processing original annotations: "+anns.size());
for(Annotation ann : annlist) {
String txt;
if(inputSpecificationFeature == null) {
txt = gate.Utils.stringFor(document, ann);
newText.append(txt);
annspecs.add(new AnnotationSpec(ann,curoffset,curoffset+txt.length(),ann.getId()));
curoffset += txt.length();
newText.append(actualSeparatorString);
curoffset += actualSeparatorString.length();
} else {
txt = (String)ann.getFeatures().get(inputSpecificationFeature);
if(txt != null) {
newText.append(txt);
annspecs.add(new AnnotationSpec(ann,curoffset,curoffset+txt.length(),ann.getId()));
curoffset += txt.length();
newText.append(actualSeparatorString);
curoffset += actualSeparatorString.length();
}
}
}
FeatureMap theparms = Factory.newFeatureMap();
theparms.put("collectRepositioningInfo", document.getCollectRepositioningInfo());
theparms.put("encoding", ((DocumentImpl) document).getEncoding());
theparms.put("markupAware", document.getMarkupAware());
theparms.put("mimeType", ((DocumentImpl) document).getMimeType());
theparms.put("preserveOriginalContent", document.getPreserveOriginalContent());
theparms.put("stringContent", newText.toString());
FeatureMap thefeats = Factory.newFeatureMap();
FeatureMap docfeats = document.getFeatures();
thefeats.putAll(docfeats);
String theName = document.getName();
// create a copy of the current document
Document newDoc;
try {
newDoc = (Document) Factory.createResource(
"gate.corpora.DocumentImpl",
theparms,
thefeats,
theName+"_virtual");
} catch (ResourceInstantiationException ex) {
throw new GateRuntimeException(ex);
}
// set the initial annotations in the virtual document
AnnotationSet newSet = newDoc.getAnnotations(virtualSpecificationSet);
for(AnnotationSpec annspec : annspecs) {
FeatureMap fm = Factory.newFeatureMap();
fm.putAll(annspec.annotation.getFeatures());
fm.put("orig_id",annspec.origId);
try {
newSet.add(annspec.fromOffset, annspec.toOffset, virtualSpecificationType, fm);
} catch(InvalidOffsetException ex) {
throw new GateRuntimeException(
"Invalid offset when creating annotation for virtual document: from/to/doclength: "+
annspec.fromOffset+"/"+annspec.toOffset+"/"+newDoc.getContent().size(),ex);
}
}