All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gate.composite.impl.DefaultCombiningMethod Maven / Gradle / Ivy

There is a newer version: 8.6.1
Show newest version
package gate.composite.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import gate.Annotation;
import gate.Document;
import gate.composite.CombiningMethodException;
import gate.composite.CompositeDocument;
import gate.compound.CompoundDocument;
import gate.compound.impl.AnnotationStream;
import gate.util.OffsetComparator;

/**
 * Default Implementation of the Combining Method. This method requires
 * three parameters.
 * 

* unitAnnotationType *

* inputASName *

* copyUnderlyingAnnotations. *

* unit Annotation type is the unit of combining two texts. E.g. if it * is set to "Sentence", one "Sentence" annotation from each document * member is considered and put together in the composite document. *

* inputASName tells the method from which annotation set the * annotations should be used from. *

* if set to true the copyUnderlyingAnnotations, all the underlying * annotations are copied to the composite document. * * @author niraj */ public class DefaultCombiningMethod extends AbstractCombiningMethod { private static final long serialVersionUID = 4050197561715800118L; /** * The parameters must contain two parameters "unitAnnotationType" and * "inputASName" and "copyUnderlyingAnnotations" The parameter names * are Case Sensitive. Example: *

* map.put("unitAnnotationType","Sentence"); *

* map.put("inputASName","Key"); *

* map.put("copyUnderlyingAnnotations","true"); */ public CompositeDocument combine(CompoundDocument compoundDocument, Map parameters) throws CombiningMethodException { try { // parameters String unitAnnotationType = (String)parameters.get("unitAnnotationType"); if(unitAnnotationType == null || unitAnnotationType.trim().length() == 0) throw new CombiningMethodException("unitAnnotationType cannot be null"); String inputASName = (String)parameters.get("inputASName"); String copy = (String)parameters.get("copyUnderlyingAnnotations"); boolean copyUnderlyingAnnotations = copy == null ? false : Boolean.parseBoolean((String)parameters .get("copyUnderlyingAnnotations")); Set annotationTypesToCopy = null; if(!copyUnderlyingAnnotations) annotationTypesToCopy = new HashSet(); // initialize startDocument startDocument(compoundDocument, annotationTypesToCopy); // obtain a list of documentIDs List documentIDs = compoundDocument.getDocumentIDs(); int total = 0; for(int i = 0; i < documentIDs.size(); i++) { String documentID = documentIDs.get(i); Document doc = compoundDocument.getDocument(documentID); if(doc instanceof CompositeDocument) continue; total++; } AnnotationStream streams[] = new AnnotationStream[total]; for(int i = 0, j = 0; i < documentIDs.size() && j < total; i++, j++) { String documentID = documentIDs.get(i); Document doc = compoundDocument.getDocument(documentID); if(doc instanceof CompositeDocument) { j--; continue; } streams[j] = new AnnotationStream(doc, inputASName, unitAnnotationType, documentID, new OffsetComparator()); } boolean breaked = false; while(true) { for(int i = 0; i < streams.length; i++) { Annotation annot = streams[i].next(); if(annot == null) { breaked = true; break; } String docID = streams[i].getLanguage(); Document doc = compoundDocument.getDocument(docID); // adding it to the composite document addContent(doc, annot); } if(breaked) break; } // finalize document return finalizeDocument(); } catch(Exception e) { throw new CombiningMethodException(e); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy