use of de.catma.document.annotation.TagReference in project catma by forTEXT.
the class ProjectResourceExportApiRequestHandler method serializeProjectResources.
private String serializeProjectResources() {
try {
Export export = new Export();
for (SourceDocument sourceDocument : project.getSourceDocuments()) {
ArrayList<AnnotationCollection> annotationCollections = new ArrayList<>();
for (AnnotationCollectionReference annotationCollectionReference : sourceDocument.getUserMarkupCollectionRefs()) {
annotationCollections.add(project.getUserMarkupCollection(annotationCollectionReference));
}
ArrayList<TagDefinition> tagDefinitions = new ArrayList<>();
ArrayList<TagReference> tagReferences = new ArrayList<>();
for (AnnotationCollection annotationCollection : annotationCollections) {
for (TagsetDefinition tagsetDefinition : annotationCollection.getTagLibrary().getTagsetDefinitions()) {
tagDefinitions.addAll(tagsetDefinition.stream().collect(Collectors.toList()));
}
tagReferences.addAll(annotationCollection.getTagReferences());
}
ExportDocument exportDocument = new ExportDocument(new PreApiSourceDocument(sourceDocument, String.format("%s%s/doc/%s", BASE_URL, handlerPath.substring(1), sourceDocument.getUuid().toLowerCase())), tagDefinitions.stream().map(PreApiTagDefinition::new).collect(Collectors.toList()), tagReferences.stream().map((TagReference tagReference) -> {
try {
return new PreApiAnnotation(tagReference, tagDefinitions.stream().filter(td -> td.getUuid().equals(tagReference.getTagDefinitionId())).findFirst().get(), sourceDocument);
} catch (IOException e) {
logger.log(Level.WARNING, String.format("Error serializing TagReference: %s", tagReference), e);
return null;
}
}).collect(Collectors.toList()));
export.addExportDocument(exportDocument);
}
return new SerializationHelper<Export>().serialize(export);
} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to serialize project resources", e);
return "{\"error\": \"Failed to serialize project resources, please contact CATMA support\"}";
}
}
use of de.catma.document.annotation.TagReference in project catma by forTEXT.
the class TeiUserMarkupCollectionSerializer method mergeTagReferences.
private HashMap<Range, List<TagReference>> mergeTagReferences(List<TagReference> tagReferences, Range initialRange) {
HashMap<Range, List<TagReference>> mergedTagReferences = new HashMap<Range, List<TagReference>>();
mergedTagReferences.put(initialRange, new ArrayList<TagReference>());
for (TagReference tagReference : tagReferences) {
Range targetRange = tagReference.getRange();
List<Range> affectedRanges = getAffectedRanges(mergedTagReferences.keySet(), targetRange);
for (Range affectedRange : affectedRanges) {
if (affectedRange.isInBetween(targetRange)) {
mergedTagReferences.get(affectedRange).add(tagReference);
} else {
List<TagReference> existingReferences = mergedTagReferences.get(affectedRange);
Range overlappingRange = affectedRange.getOverlappingRange(targetRange);
List<Range> disjointRanges = affectedRange.getDisjointRanges(targetRange);
// range outside of the overlapping range
// left or right depending on the position of the overlapping range
Range firstDisjointRange = disjointRanges.get(0);
List<TagReference> firstCopy = new ArrayList<TagReference>();
firstCopy.addAll(existingReferences);
mergedTagReferences.put(firstDisjointRange, firstCopy);
// the overlapping range sits in the middle
if (disjointRanges.size() == 2) {
// range right of the overlappting range
Range secondDisjointRange = disjointRanges.get(1);
List<TagReference> secondCopy = new ArrayList<TagReference>();
secondCopy.addAll(existingReferences);
mergedTagReferences.put(secondDisjointRange, secondCopy);
}
existingReferences.add(tagReference);
mergedTagReferences.put(overlappingRange, existingReferences);
mergedTagReferences.remove(affectedRange);
}
}
}
return mergedTagReferences;
}
use of de.catma.document.annotation.TagReference in project catma by forTEXT.
the class TeiUserMarkupCollectionSerializer method serialize.
public void serialize(AnnotationCollection userMarkupCollection, SourceDocument sourceDocument) throws IOException {
String targetURI = makeTargetURI(sourceDocument);
if (userMarkupCollection.isEmpty()) {
return;
}
TeiElement textElement = (TeiElement) teiDocument.getNodes(TeiElementName.text).get(0);
TeiElement ptrParentElement = (TeiElement) teiDocument.getNodes(TeiElementName.ab, AttributeValue.type_catma).get(0);
Set<String> addedTagInstances = new HashSet<String>();
HashMap<Range, List<TagReference>> mergedTagReferences = mergeTagReferences(userMarkupCollection.getTagReferences(), new Range(0, sourceDocument.getLength()));
TreeSet<Range> sortedRanges = new TreeSet<Range>();
sortedRanges.addAll(mergedTagReferences.keySet());
for (Range range : sortedRanges) {
List<TagReference> currentReferences = mergedTagReferences.get(range);
TeiElement parent = ptrParentElement;
if (!currentReferences.isEmpty()) {
parent = writeSegment(currentReferences, ptrParentElement, textElement, addedTagInstances, userMarkupCollection.getTagLibrary());
}
writeText(targetURI, range, parent, sourceDocument);
}
}
use of de.catma.document.annotation.TagReference in project catma by forTEXT.
the class TeiUserMarkupCollectionSerializer method writeSegment.
private TeiElement writeSegment(List<TagReference> tagReferences, TeiElement abElement, TeiElement textElement, Set<String> addedTagInstances, TagLibrary tagLibrary) {
List<TagInstance> tagInstances = new ArrayList<TagInstance>();
for (TagReference tr : tagReferences) {
if (!addedTagInstances.contains(tr.getTagInstanceId())) {
writeTagInstance(tr.getTagInstance(), textElement, tagLibrary);
addedTagInstances.add(tr.getTagInstanceId());
}
tagInstances.add(tr.getTagInstance());
}
AnaValueHandler anaValueHandler = new AnaValueHandler();
TeiElement seg = new TeiElement(TeiElementName.seg);
seg.setAttributeValue(Attribute.ana, anaValueHandler.makeValueFrom(tagInstances));
abElement.appendChild(seg);
return seg;
}
use of de.catma.document.annotation.TagReference in project catma by forTEXT.
the class JsonLdWebAnnotation method toTagReferenceList.
public List<TagReference> toTagReferenceList(String projectId, String markupCollectionId) throws Exception {
TagInstance tagInstance = this.getTagInstance();
String sourceDocumentUri = this.getSourceDocumentUri();
List<Range> ranges = this.getRanges();
List<TagReference> tagReferences = new ArrayList<>();
try {
for (Range range : ranges) {
tagReferences.add(new TagReference(tagInstance, sourceDocumentUri, range, markupCollectionId));
}
} catch (URISyntaxException e) {
throw new IOException(String.format("error loading Collection %1$s of project %2$s ", markupCollectionId, projectId), e);
}
return tagReferences;
}
Aggregations