org.apache.uima.cas.CAS类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(13.0k)|赞(0)|评价(0)|浏览(133)

本文整理了Java中org.apache.uima.cas.CAS类的一些代码示例,展示了CAS类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。CAS类的具体详情如下:
包路径:org.apache.uima.cas.CAS
类名称:CAS

CAS介绍

[英]Object-oriented CAS (Common Analysis System) API.

A CAS object provides the starting point for working with the CAS. It provides access to the type system, to indexes, iterators and filters (constraints). It also lets you create new annotations and other data structures. You can create a CAS object using static methods on the class org.apache.uima.util.CasCreationUtils.

The CAS object is also the container that manages multiple Subjects of Analysis or Sofas. A Sofa represents some form of an unstructured artifact that is processed in a UIMA pipeline. The Java string called the "DocumentText" used in a UIMA text processing pipeline is an example of a Sofa. A Sofa can be analyzed independently using the standard UIMA programming model or analyzed together with other Sofas utilizing the Sofa programming model extensions.

A Sofa is implemented as a built-in CAS type uima.cas.Sofa. Use org.apache.uima.cas.CAS#createSofa to instantiate a Sofa feature structure. The SofaFS class provides methods to set and get the features of a SofaFS. Although Sofas are implemented as standard feature structures, generic CAS APIs must never be used to create Sofas or set their features.

Use org.apache.uima.cas.CAS#getView(String) or org.apache.uima.cas.CAS#getView(SofaFS) to obtain a view of a particular Sofa in the CAS. This view will provide access to the Sofa data (for example the document text) as well as the index repository, which contains metadata (annotations and other feature structures) about that Sofa.

Use #getTypeSystem to access the type system. With a TypeSystem object, you can access the Type and Feature objects for the CAS built-in types. Note that this interface also provides constants for the names of the built-in types and features.
[中]面向对象的CAS(通用分析系统)API。
CAS对象提供了使用CAS的起点。它提供对类型系统、索引、迭代器和过滤器(约束)的访问。它还允许您创建新的注释和其他数据结构。您可以在类org上使用静态方法创建CAS对象。阿帕奇。尤马。util。CasCreationUtils。
CAS对象也是管理多个分析主题或沙发的容器。Sofa表示在UIMA管道中处理的某种形式的非结构化工件。UIMA文本处理管道中使用的名为“DocumentText”的Java字符串就是Sofa的一个示例。可以使用标准UIMA编程模型单独分析Sofa,也可以使用Sofa编程模型扩展与其他Sofa一起分析Sofa。
Sofa实现为内置CAS类型uima。中科院。沙发使用组织。阿帕奇。尤马。中科院。CAS#createSofa来实例化一个Sofa特征结构。SofaFS类提供了设置和获取SofaFS功能的方法。尽管SOFA是作为标准功能结构实现的,但不能使用通用CAS API来创建SOFA或设置其功能。
使用组织。阿帕奇。尤马。中科院。CAS#getView(字符串)或组织。阿帕奇。尤马。中科院。CAS#getView(SofaFS)获取CAS中特定沙发的视图。此视图将提供对Sofa数据(例如文档文本)以及索引存储库的访问,其中包含有关该Sofa的元数据(注释和其他功能结构)。
使用#getTypeSystem访问类型系统。使用TypeSystem对象,可以访问CAS内置类型的类型和功能对象。请注意,此接口还为内置类型和功能的名称提供常量。

代码示例

代码示例来源:origin: apache/opennlp

private void addChunkAnnotation(CAS tcas, AnnotationFS[] tokenAnnotations,
                String tag, int start, int end) {
 AnnotationFS chunk = tcas.createAnnotation(mChunkType,
   tokenAnnotations[start].getBegin(), tokenAnnotations[end - 1].getEnd());
 chunk.setStringValue(mChunkFeature, tag);
 tcas.getIndexRepository().addFS(chunk);
}

代码示例来源:origin: apache/opennlp

protected void documentDone(CAS cas) {
 // TODO: Create confidence FS
 // contains String name type
 // contains Double prob
 if (documentConfidenceType != null) {
  FeatureStructure confidenceFS = cas.createFS(documentConfidenceType);
  confidenceFS.setDoubleValue(documentConfidenceFeature,
    documentConfidence.mean());
  confidenceFS.setStringValue(documentConfidenceNameTypeFeature,
    mNameType.getName());
  cas.addFsToIndexes(confidenceFS);
 }
 // Clears the adaptive data which was created for the current document
 mNameFinder.clearAdaptiveData();
 documentConfidence = new Mean();
}

代码示例来源:origin: apache/opennlp

@Override
 protected void setBestCategory(CAS tcas, String bestCategory) {
  FSIndex<AnnotationFS> categoryIndex = tcas.getAnnotationIndex(mCategoryType);

  AnnotationFS categoryAnnotation;

  if (categoryIndex.size() > 0) {
   categoryAnnotation = categoryIndex.iterator().next();
  } else {
   categoryAnnotation = tcas.createAnnotation(mCategoryType, 0,
     tcas.getDocumentText().length());

   tcas.getIndexRepository().addFS(categoryAnnotation);
  }

  categoryAnnotation.setStringValue(mCategoryFeature, bestCategory);
 }
}

代码示例来源:origin: webanno/webanno

private static AnnotationFS makeLinkHostFS(JCas aJCas, String aType, int aBegin, int aEnd,
    FeatureStructure... aLinks)
{
  Type hostType = aJCas.getTypeSystem().getType(aType);
  AnnotationFS hostA1 = aJCas.getCas().createAnnotation(hostType, aBegin, aEnd);
  if (aLinks != null) {
    hostA1.setFeatureValue(hostType.getFeatureByBaseName("links"),
        FSCollectionFactory.createFSArray(aJCas, asList(aLinks)));
  }
  aJCas.getCas().addFsToIndexes(hostA1);
  return hostA1;
}

代码示例来源:origin: de.tudarmstadt.ukp.dkpro.tc/dkpro-tc-ml

private void addTCSequenceAnnotation(JCas jcas)
{
  Type type = jcas.getCas().getTypeSystem().getType(nameSequence);
  Collection<AnnotationFS> sequenceAnnotation = CasUtil.select(jcas.getCas(), type);
  for (AnnotationFS seq : sequenceAnnotation) {
    TextClassificationSequence tcs = new TextClassificationSequence(jcas, seq.getBegin(),
        seq.getEnd());
    tcs.addToIndexes();
  }
}

代码示例来源:origin: webanno/webanno

private static FeatureStructure makeLinkFS(JCas aJCas, String aType, String aSlotLabel,
    AnnotationFS aTarget)
{
  Type linkType = aJCas.getTypeSystem().getType(aType);
  FeatureStructure linkA1 = aJCas.getCas().createFS(linkType);
  linkA1.setStringValue(linkType.getFeatureByBaseName("role"), aSlotLabel);
  linkA1.setFeatureValue(linkType.getFeatureByBaseName("target"), aTarget);
  aJCas.getCas().addFsToIndexes(linkA1);
  return linkA1;
}

代码示例来源:origin: org.apache.uima/textmarker-core

public static void addSourceDocumentInformation(CAS cas, File each) {
 Type sdiType = cas.getTypeSystem()
     .getType("org.apache.uima.examples.SourceDocumentInformation");
 if (sdiType != null) {
  if (cas.getAnnotationIndex(sdiType).size() == 0) {
   AnnotationFS sdi = cas.createAnnotation(sdiType, cas.getDocumentAnnotation().getBegin(),
       cas.getDocumentAnnotation().getEnd());
   Feature uriFeature = sdiType.getFeatureByBaseName("uri");
   sdi.setStringValue(uriFeature, each.toURI().getPath());
   cas.addFsToIndexes(sdi);
  }
 }
}

代码示例来源:origin: webanno/webanno

private AnnotationFS createPOSAnno(JCas aJCas, String aValue, int aBegin, int aEnd)
{
  Type type = aJCas.getTypeSystem().getType(POS.class.getTypeName());
  
  AnnotationFS clickedFs = aJCas.getCas().createAnnotation(type, aBegin, aEnd);
  Feature posValue = type.getFeatureByBaseName("PosValue");
  clickedFs.setStringValue(posValue, aValue);
  aJCas.addFsToIndexes(clickedFs);
  return clickedFs;
}

代码示例来源:origin: webanno/webanno

@Test
public void testSimpleChain() throws Exception
{
  JCas jcas = makeJCasOneSentence();
  CAS cas = jcas.getCas();
  
  List<Token> tokens = new ArrayList<>(select(jcas, Token.class));
  
  Token t1 = tokens.get(0);
  Token t2 = tokens.get(1);
  Token t3 = tokens.get(2);
  
  Type head = cas.getTypeSystem().getType("webanno.custom.SimpleChain");
  Type link = cas.getTypeSystem().getType("webanno.custom.SimpleLink");
  
  makeChainHead(head,
      makeChainLink(link, cas, t1.getBegin(), t1.getEnd(), null, null, 
      makeChainLink(link, cas, t2.getBegin(), t2.getEnd(), null, null,
      makeChainLink(link, cas, t3.getBegin(), t3.getEnd(), null, null, null))));
  writeAndAssertEquals(jcas, 
      WebannoTsv3Writer.PARAM_CHAIN_LAYERS, asList("webanno.custom.Simple"));
}

代码示例来源:origin: de.unistuttgart.ims/de.unistuttgart.ims.drama.util

public static <T extends TOP> T getOrCreate(JCas jcas, Class<T> targetClass) {
    if (JCasUtil.exists(jcas, targetClass)) {
      return JCasUtil.selectSingle(jcas, targetClass);
    } else {
      T annotation = jcas.getCas().createFS(JCasUtil.getType(jcas, targetClass));
      jcas.getCas().addFsToIndexes(annotation);
      return annotation;
    }
  }
}

代码示例来源:origin: de.tudarmstadt.ukp.clarin.webanno/de.tudarmstadt.ukp.clarin.webanno.brat

/**
 * Create a new chain head feature structure. Already adds the chain to the CAS.
 */
private FeatureStructure newChain(JCas aJCas, AnnotationFS aFirstLink)
{
  Type chainType = getAnnotationType(aJCas.getCas());
  FeatureStructure newChain = aJCas.getCas().createFS(chainType);
  newChain.setFeatureValue(chainType.getFeatureByBaseName(chainFirstFeatureName), aFirstLink);
  aJCas.addFsToIndexes(newChain);
  return newChain;
}

代码示例来源:origin: org.apache.uima/ruta-core

@Override
public void process(JCas jcas) throws AnalysisEngineProcessException {
 CAS cas = jcas.getCas();
 Type sdiType = cas.getTypeSystem().getType(RutaEngine.SOURCE_DOCUMENT_INFORMATION);
  FSIterator<AnnotationFS> sdiit = cas.getAnnotationIndex(sdiType).iterator();
  if (sdiit.isValid()) {
   AnnotationFS annotationFS = sdiit.get();
   Feature uriFeature = sdiType.getFeatureByBaseName("uri");
   String stringValue = annotationFS.getStringValue(uriFeature);
   File f = new File(stringValue);
   String name = f.getName();
 CAS inView = cas.getView(inputView);
 if (outView == null) {
  outView = getContext().getEmptyCas(CAS.class);
 outView.reset();
 CasCopier cc = new CasCopier(inView, outView, true);
 cc.copyCasView(inView, outputView, true);

代码示例来源:origin: org.dkpro.tc/dkpro-tc-core

@Override
public void initialize(UimaContext context) throws ResourceInitializationException
{
  super.initialize(context);
  outputFile = new File(targetFolder, DeepLearningConstants.FILENAME_MAXIMUM_LENGTH);
  try {
    JCas typeFactory = JCasFactory.createJCas();
    Type type = JCasUtil.getType(typeFactory, Class.forName(instanceTypeName));
    AnnotationFS createAnnotation = typeFactory.getCas().createAnnotation(type, 0, 0);
    instanceType = createAnnotation.getType();
  }
  catch (Exception e) {
    throw new ResourceInitializationException(e);
  }
}

代码示例来源:origin: nlpie/biomedicus

/**
 * Function for creating generic UIMA {@code FeatureStructure}s.
 *
 * @param featureStructure {@code FeatureStructure} to create a new object of the same type in the
 * target cas.
 * @return newly created {@code FeatureStructure} matching the type of the parameter {@code
 * FeatureStructure}
 */
private FeatureStructure defaultCreateType(FeatureStructure featureStructure) {
 String typeName = featureStructure.getType().getName();
 Type targetType = targetCas.getTypeSystem().getType(typeName);
 return targetCas.createFS(targetType);
}

代码示例来源:origin: de.tudarmstadt.ukp.dkpro.core/de.tudarmstadt.ukp.dkpro.core.arktools-gpl

@Override
public void process(JCas jCas)
  throws AnalysisEngineProcessException
{
  modelProvider.configure(jCas.getCas());
  mappingProvider.configure(jCas.getCas());
  List<Token> tokens = selectCovered(jCas, Token.class, 0, jCas.getDocumentText().length());
  List<TaggedToken> taggedTokens = tagTweetTokens(tokens, modelProvider.getResource());
  for (TaggedToken taggedToken : taggedTokens) {
    Type posType = mappingProvider.getTagType(taggedToken.tag);
    POS pos = (POS) jCas.getCas().createAnnotation(posType, taggedToken.getBegin(),
        taggedToken.getEnd());
    pos.setPosValue(taggedToken.tag.intern());
    pos.addToIndexes();
    taggedToken.token.setPos(pos);
  }
}

代码示例来源:origin: nlpie/biomedicus

CASArtifact(
  @Nullable LabelAdapters labelAdapters,
  CAS cas,
  String artifactID
) {
 this.labelAdapters = labelAdapters;
 this.cas = cas;
 TypeSystem typeSystem = cas.getTypeSystem();
 metadataType = typeSystem.getType("ArtifactMetadata");
 keyFeature = metadataType.getFeatureByBaseName("key");
 valueFeature = metadataType.getFeatureByBaseName("value");
 metadataCas = cas.createView("metadata");
 metadataCas.setDocumentText("");
 Type idType = typeSystem.getType("ArtifactID");
 Feature idFeat = idType.getFeatureByBaseName("artifactID");
 this.artifactID = artifactID;
 FeatureStructure documentIdFs = metadataCas.createFS(idType);
 documentIdFs.setStringValue(idFeat, artifactID);
 metadataCas.addFsToIndexes(documentIdFs);
 metadataIndex = metadataCas.getIndexRepository().getIndex("metadata", metadataType);
 casMetadata = new CASMetadata();
}

代码示例来源:origin: apache/opennlp

cas.removeFsFromIndexes(annotation);
sentenceTokenList.add(tokenAnnotation.getCoveredText());
  names[i].getStart()).getBegin();
  names[i].getEnd() - 1).getEnd();
 nameAnnotations[i] = cas.createAnnotation(nameType, startIndex, endIndex);
 cas.getIndexRepository().addFS(nameAnnotations[i]);

代码示例来源:origin: apache/opennlp

.getAnnotationIndex(containerType);
String text = containerAnnotation.getCoveredText();
 sentences[i] = cas.createAnnotation(sentenceType,
   sentPositions[i].getStart() + containerAnnotation.getBegin(),
   sentPositions[i].getEnd() + containerAnnotation.getBegin());
 cas.getIndexRepository().addFS(sentences[i]);

代码示例来源:origin: de.tudarmstadt.ukp.dkpro.core/de.tudarmstadt.ukp.dkpro.core.io.web1t-asl

@Override
public void process(JCas jcas)
  throws AnalysisEngineProcessException
{
  try {
    converter.add(jcas, inputPaths, jcas.getCas().getTypeSystem().getType(contextType));
  }
  catch (IOException e) {
    throw new AnalysisEngineProcessException(e);
  }
}

代码示例来源:origin: apache/opennlp

protected void process(CAS cas, AnnotationFS sentenceAnnotation) {
 FSIndex<AnnotationFS> allTokens = cas.getAnnotationIndex(mTokenType);
 ContainingConstraint containingConstraint =
   new ContainingConstraint(sentenceAnnotation);
 String sentence = sentenceAnnotation.getCoveredText();
 Iterator<AnnotationFS> containingTokens = cas.createFilteredIterator(
   allTokens.iterator(), containingConstraint);
 List<Span> tokenSpans = new LinkedList<>();
 while (containingTokens.hasNext()) {
  AnnotationFS token = containingTokens.next();
  tokenSpans.add(new Span(token.getBegin() - sentenceAnnotation.getBegin(),
    token.getEnd() - sentenceAnnotation.getBegin()));
 }
 ParseConverter converter = new ParseConverter(sentence, tokenSpans.toArray(new Span[tokenSpans.size()]));
 Parse unparsedTree = converter.getParseForTagger();
 if (unparsedTree.getChildCount() > 0) {
  Parse parse = mParser.parse(unparsedTree);
  // TODO: We need a strategy to handle the case that a full
  //       parse could not be found. What to do in this case?
  parse = converter.transformParseFromTagger(parse);
  if (mLogger.isLoggable(Level.INFO)) {
   StringBuffer parseString = new StringBuffer();
   parse.show(parseString);
   mLogger.log(Level.INFO, parseString.toString());
  }
  createAnnotation(cas, sentenceAnnotation.getBegin(), parse);
 }
}

相关文章