Java源码示例:org.apache.uima.util.CasCopier
示例1
@Override
public void process(JCas jcas) throws AnalysisEngineProcessException {
try {
JCas copied = JCasFactory.createJCas(typesystem);
CasCopier.copyCas(jcas.getCas(), copied.getCas(), true, true);
String id = Strings.padStart(ProcessingStepUtils.getSequenceId(jcas), 4, '0');
CasIOUtil.writeXmi(copied, new File(dir, id + ".xmi"));
} catch (IOException | UIMAException e) {
e.printStackTrace();
}
}
示例2
@Override
public AbstractCas next() throws AnalysisEngineProcessException {
this.enableHasNext(false);
JCas cas = this.getEmptyJCas();
try {
CasCopier.copyCas(this.cas.getCas(), cas.getCas(), false);
StringBuilder builder = new StringBuilder();
int begin = 0;
int end = 0;
for (Token token : this.getTokens()) {
begin = builder.length();
builder.append(token.word());
end = builder.length();
builder.append(' ');
WordAnnotation annotation = new WordAnnotation(cas, begin, end);
annotation.setTag(token.tag());
annotation.setLemma(token.lemma());
annotation.addToIndexes();
}
cas.setDocumentText(builder.toString());
cas.setDocumentLanguage("lv");
this.getTokens().clear();
return cas;
} catch (Exception e) {
cas.release();
throw new AnalysisEngineProcessException(e);
}
}
示例3
@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {
// because of later evaluation, copy annotation to view_gold (later used
// by AnnotationEvaluator) and remove it from view_system.
Collection<? extends Annotation> goldsFromInitialView = select(jCas,
goldAnnotation);
JCas goldView = null;
try {
goldView = jCas.createView(VIEW_GOLD);
} catch (Throwable e) {
throw new AnalysisEngineProcessException(
NO_RESOURCE_FOR_PARAMETERS, new Object[] { VIEW_GOLD }, e);
}
CasCopier casCopier = new CasCopier(jCas.getCas(), goldView.getCas());
goldView.setDocumentText(jCas.getDocumentText());
// view_system annot. stored in List for later delete
// (conccurentModifExeption)
List<Annotation> toDelete = new ArrayList<Annotation>();
for (Annotation g : goldsFromInitialView) {
goldView.addFsToIndexes(casCopier.copyFs(g));
if (deleteFrom) {
toDelete.add(g);
}
}
Annotation[] arr = toDelete.toArray(new Annotation[toDelete.size()]);
for (int i = 0; i < arr.length; i++) {
arr[i].removeFromIndexes(jCas);
}
}
示例4
private void doTestMultiThreadedSerialize(File typeSystemDescriptor) throws Exception {
// deserialize a complex CAS from XCAS
CAS cas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
InputStream serCasStream = new FileInputStream(JUnitExtension.getFile("ExampleCas/cas.xml"));
XCASDeserializer deser = new XCASDeserializer(cas.getTypeSystem());
ContentHandler deserHandler = deser.getXCASHandler(cas);
SAXParserFactory fact = SAXParserFactory.newInstance();
SAXParser parser = fact.newSAXParser();
XMLReader xmlReader = parser.getXMLReader();
xmlReader.setContentHandler(deserHandler);
xmlReader.parse(new InputSource(serCasStream));
serCasStream.close();
// make n copies of the cas, so they all share
// the same type system
final CAS [] cases = new CAS[MAX_THREADS];
for (int i = 0; i < MAX_THREADS; i++) {
cases[i] = CasCreationUtils.createCas(cas.getTypeSystem(), new TypePriorities_impl(), indexes, null);
CasCopier.copyCas(cas, cases[i], true);
}
// start n threads, serializing as XMI
MultiThreadUtils.ThreadM [] threads = new MultiThreadUtils.ThreadM[MAX_THREADS];
for (int i = 0; i < MAX_THREADS; i++) {
threads[i] = new MultiThreadUtils.ThreadM(new DoSerialize(cases[i]));
threads[i].start();
}
MultiThreadUtils.waitForAllReady(threads);
for (int i = 0; i < threadsToUse.length; i++) {
MultiThreadUtils.ThreadM[] sliceOfThreads = new MultiThreadUtils.ThreadM[threadsToUse[i]];
System.arraycopy(threads, 0, sliceOfThreads, 0, threadsToUse[i]);
long startTime = System.currentTimeMillis();
MultiThreadUtils.kickOffThreads(sliceOfThreads);
MultiThreadUtils.waitForAllReady(sliceOfThreads);
System.out.println("\nNumber of threads serializing: " + threadsToUse[i] +
" Normalized millisecs (should be close to the same): " + (System.currentTimeMillis() - startTime) / threadsToUse[i]);
}
MultiThreadUtils.terminateThreads(threads);
}
示例5
public void process(JCas aJCas) throws AnalysisEngineProcessException {
// procure a new CAS if we don't have one already
if (mMergedCas == null) {
mMergedCas = getEmptyJCas();
}
// append document text
String docText = aJCas.getDocumentText();
int prevDocLen = mDocBuf.length();
mDocBuf.append(docText);
// copy specified annotation types
CasCopier copier = new CasCopier(aJCas.getCas(), mMergedCas.getCas());
Set copiedIndexedFs = new HashSet(); // needed in case one annotation is in two indexes (could
// happen if specified annotation types overlap)
for (int i = 0; i < mAnnotationTypesToCopy.length; i++) {
Type type = mMergedCas.getTypeSystem().getType(mAnnotationTypesToCopy[i]);
FSIndex index = aJCas.getCas().getAnnotationIndex(type);
Iterator iter = index.iterator();
while (iter.hasNext()) {
FeatureStructure fs = (FeatureStructure) iter.next();
if (!copiedIndexedFs.contains(fs)) {
Annotation copyOfFs = (Annotation) copier.copyFs(fs);
// update begin and end
copyOfFs.setBegin(copyOfFs.getBegin() + prevDocLen);
copyOfFs.setEnd(copyOfFs.getEnd() + prevDocLen);
mMergedCas.addFsToIndexes(copyOfFs);
copiedIndexedFs.add(fs);
}
}
}
// get the SourceDocumentInformation FS, which indicates the sourceURI of the document
// and whether the incoming CAS is the last segment
FSIterator it = aJCas.getAnnotationIndex(SourceDocumentInformation.type).iterator();
if (!it.hasNext()) {
throw new AnalysisEngineProcessException(MESSAGE_DIGEST, MISSING_SOURCE_DOCUMENT_INFO,
new Object[0]);
}
SourceDocumentInformation sourceDocInfo = (SourceDocumentInformation) it.next();
if (sourceDocInfo.getLastSegment()) {
// time to produce an output CAS
// set the document text
mMergedCas.setDocumentText(mDocBuf.toString());
// add source document info to destination CAS
SourceDocumentInformation destSDI = new SourceDocumentInformation(mMergedCas);
destSDI.setUri(sourceDocInfo.getUri());
destSDI.setOffsetInSource(0);
destSDI.setLastSegment(true);
destSDI.addToIndexes();
mDocBuf = new StringBuffer();
mReadyToOutput = true;
}
}