Java Code Examples for org.apache.uima.jcas.JCas#release()

The following examples show how to use org.apache.uima.jcas.JCas#release() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DocumentProcessor.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
public ProcessedDocument process(Document d)
    throws UIMAException, IOException, ClassNotFoundException, NoSuchMethodException, MissingSettingException, EntityLinkingDataAccessException,
    UnprocessableDocumentException {
  CAS cas = casManager[type.ordinal()].getCas(type.toString());
  ProcessedDocument result;
  JCas jcas = cas.getJCas();
  d.addSettingstoJcas(jcas);
  try {
    jcas.setDocumentText(d.getText());
    result = process(jcas);
  } catch (Exception e) {
    throw new UnprocessableDocumentException(e.getCause() != null? e.getCause().getMessage() : e.getMessage());
  } finally {
    jcas.release();
  }
  return result;
}
 
Example 2
Source File: ImprovedLuceneInMemorySentenceRetrievalExecutor.java    From bioasq with Apache License 2.0 6 votes vote down vote up
private HashMap<String, String> sentenceAnalysis(String sentence) {
  HashMap<String, String> dependency = new HashMap<String, String>();
  try {
    JCas snippetJcas = JCasFactory.createJCas();
    snippetJcas.setDocumentText(sentence);
    List<Token> tokens = parserProvider.parseDependency(snippetJcas);
    for (Token tok : tokens) {
      if (tok.getHead() == null)
        continue;
      dependency.put(tok.getLemmaForm(), tok.getHead().getLemmaForm());
    }
    snippetJcas.release();
  } catch (UIMAException err) {
    err.printStackTrace();
  }
  return dependency;
}
 
Example 3
Source File: SparkUimaUtils.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public static void createSequenceFile(Object[] params, String uri)
    throws URISyntaxException, IOException, UIMAException, NoSuchMethodException, MissingSettingException, ClassNotFoundException {
  Configuration conf = new Configuration();
  Path path = new Path(uri);
  Writer writer =
      SequenceFile.createWriter(
          conf, Writer.file(path),
          Writer.keyClass(Text.class),
          Writer.valueClass(SCAS.class));

  int count = 0;

  CollectionReaderDescription readerDescription = Reader.getCollectionReaderDescription(Reader.COLLECTION_FORMAT.NYT, params);
  for (JCas jCas : SimplePipelineCasPoolIterator.iteratePipeline(20, readerDescription)) {
      if(JCasUtil.exists(jCas, DocumentMetaData.class)) {
        ++count;
        // Get the ID.
        DocumentMetaData dmd = JCasUtil.selectSingle(jCas, DocumentMetaData.class);
        String docId = "NULL";
        if (dmd != null) {
          docId = dmd.getDocumentId();
        } else {
          throw new IOException("No Document ID for xml: " + jCas.getView("xml").getDocumentText());
        }
        Text docIdText = new Text(docId);
        SCAS scas = new SCAS(jCas.getCas());
        writer.append(docIdText, scas);
      }
      jCas.release();
  }
  logger.info("Wrote " + count + " documents to " + uri);
  IOUtils.closeStream(writer);
}
 
Example 4
Source File: TildeTokenizer.java    From termsuite-core with Apache License 2.0 5 votes vote down vote up
@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);
	}
}
 
Example 5
Source File: SimpleTextSegmenter.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public AbstractCas next() throws AnalysisEngineProcessException {
  int breakAt = mPos + mSegmentSize;
  if (breakAt > mDoc.length())
    breakAt = mDoc.length();
  // search for the next newline character. Note: this example segmenter implementation
  // assumes that the document contains many newlines. In the worst case, if this segmenter
  // is runon a document with no newlines, it will produce only one segment containing the
  // entire document text. A better implementation might specify a maximum segment size as
  // well as a minimum.
  while (breakAt < mDoc.length() && mDoc.charAt(breakAt - 1) != '\n')
    breakAt++;

  JCas jcas = getEmptyJCas();
  try {
    jcas.setDocumentText(mDoc.substring(mPos, breakAt));
    // if original CAS had SourceDocumentInformation, also add SourceDocumentInformatio
    // to each segment
    if (mDocUri != null) {
      SourceDocumentInformation sdi = new SourceDocumentInformation(jcas);
      sdi.setUri(mDocUri);
      sdi.setOffsetInSource(mPos);
      sdi.setDocumentSize(breakAt - mPos);
      sdi.addToIndexes();

      if (breakAt == mDoc.length()) {
        sdi.setLastSegment(true);
      }
    }

    mPos = breakAt;
    return jcas;
  } catch (Exception e) {
    jcas.release();
    throw new AnalysisEngineProcessException(e);
  }
}