Java Code Examples for org.apache.uima.cas.CAS#getView()

The following examples show how to use org.apache.uima.cas.CAS#getView() . 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: NerOutputWriter.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
@Override
	public void process(CAS aCAS) throws AnalysisEngineProcessException {
		out.println("======== CAS " + iCas + " begin ==================================");
	    out.println();

		CAS initialView = aCAS.getView("_InitialView");
		processView(initialView);
//	    Iterator<CAS> viewIt = aCAS.getViewIterator();
//		while (viewIt.hasNext()) {
//	      CAS view = viewIt.next();
//	      processView(view);
//
//	    }
	    
	    out.println("======== CAS " + iCas + " end ==================================");
	    out.println();
	    out.println();
	    out.flush();

	    iCas++;
	    
	}
 
Example 2
Source File: MentionSpansEvaluationWriter.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
@Override
	public void process(CAS aCAS) throws AnalysisEngineProcessException {
		
		out.println("======== CAS " + iCas + " begin ==================================");
	    out.println();

		CAS initialView = aCAS.getView("_InitialView");
		Map<String, Integer> result = processView(initialView);
		results.add(result);
//	    Iterator<CAS> viewIt = aCAS.getViewIterator();
//	    while (viewIt.hasNext()) {
//	      CAS view = viewIt.next();
//	      Map<String, Integer> result = processView(view);
//	      results.add(result);
//	    }
	    
	    out.println("======== CAS " + iCas + " end ==================================");
	    out.println();
	    out.println();
	    out.flush();
	    
	    
	    iCas++;
	    
	}
 
Example 3
Source File: CasComparer.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void assertEqualsInner(CAS c1, CAS c2) {
  alreadyCompared.clear();
  
  // this code handles initial views with no SofaFS
  CAS initialView1 = c1.getView(CAS.NAME_DEFAULT_SOFA);
  CAS initialView2 = c2.getView(CAS.NAME_DEFAULT_SOFA);
  assertEqualViewsInner(initialView1, initialView2);
  // this code skips the initial view, if it doesn't have a sofa FS
  FSIterator<Sofa> sofaIter = c1.getSofaIterator();
  int c1Sofas = 0;
  while (sofaIter.hasNext()) {
    SofaFS sofa = sofaIter.next();
    CAS tcas1 = c1.getView(sofa);
    CAS tcas2 = c2.getView(tcas1.getViewName());
    assertEqualViewsInner(tcas1, tcas2);
    c1Sofas++;
  }
  sofaIter = c2.getSofaIterator();
  int c2Sofas = 0;
  while (sofaIter.hasNext()) {
    c2Sofas++;
    sofaIter.moveToNext();
  }
  Assert.assertTrue(c1Sofas == c2Sofas);
}
 
Example 4
Source File: RtfParserFactory.java    From biomedicus with Apache License 2.0 6 votes vote down vote up
/**
 * Parses the rtf source into a set of UIMA CAS views.
 *
 * @param cas parent jCas view to create destination views in.
 * @param rtfSource the source rtf document.
 * @throws IOException if there is a problem reading.
 * @throws RtfReaderException if there is a problem parsing.
 */
RtfParser createParser(CAS cas, RtfSource rtfSource) throws RtfReaderException {
  List<DestinationCasMapping> destinationCasMappings = casMappings.getDestinationCasMappings();

  Map<String, Type> annotationTypeForSymbolName = casMappings
      .getControlWordCasMappings()
      .stream()
      .collect(Collectors.toMap(ControlWordCasMapping::getControlWord,
          p -> cas.getTypeSystem()
              .getType(p.getAnnotationName())));

  OutputDestinationFactory outputDestinationFactory = new CasOutputDestinationFactory(
      destinationCasMappings,
      annotationTypeForSymbolName,
      casMappings.getPropertyCasMappings(),
      cas,
      writeTables
  );

  CAS originalDocumentView = cas.getView(DocumentIdentifiers.ORIGINAL_DOCUMENT);
  IndexListener indexListener = new CasIndexListener(originalDocumentView);

  State initialState = State.createState(outputDestinationFactory, initialProperties,
      indexListener);
  return new RtfParser(rtfKeywordParser, rtfSource, initialState);
}
 
Example 5
Source File: CasCopierTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testAnnotationWithNullSofaRef() throws Exception {
    CAS srcCas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
    CAS srcCasView = srcCas.createView("TestView");
    srcCasView.setDocumentText("This is a test");
    CAS destCas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
//    LowLevelCAS lowLevelSrcCasView = srcCasView.getLowLevelCAS();
//    int typeCode = lowLevelSrcCasView.ll_getTypeSystem().ll_getCodeForType(
//            srcCas.getAnnotationType());
    // switch method of creating Annotation to create one with valid sofa ref https://issues.apache.org/jira/browse/UIMA-4099
//    int destFsAddr = lowLevelSrcCasView.ll_createFS(typeCode);
//    Annotation fs = (Annotation) lowLevelSrcCasView.ll_getFSForRef(destFsAddr);
    // the above creates an invalid Annotation, because it doesn't set the sofa ref for the view
    // replace with below that includes the proper sofa ref
    JCas srcJCas = srcCasView.getJCas();
    Annotation fs = new Annotation(srcJCas, 0, 4);
//    fs.setIntValue(srcCas.getBeginFeature(), 0);
//    fs.setIntValue(srcCas.getEndFeature(), 4);
    assertEquals("This", fs.getCoveredText());
    srcCasView.addFsToIndexes(fs);
    CasCopier.copyCas(srcCas, destCas, true);
    CAS destCasView = destCas.getView("TestView");
    Iterator<Annotation> annotIter = destCasView.<Annotation>getAnnotationIndex().iterator();
    annotIter.next(); // document annotation
    Annotation copiedFs = annotIter.next();
    assertEquals("This", copiedFs.getCoveredText());
  }
 
Example 6
Source File: CasUtil.java    From uima-uimafit with Apache License 2.0 6 votes vote down vote up
/**
 * Convenience method to get the specified view or create a new view if the requested view does
 * not exist.
 * 
 * @param cas
 *          a CAS
 * @param viewName
 *          the requested view.
 * @param create
 *          the view is created if it does not exist.
 * @return the requested view
 * @throws IllegalArgumentException
 *           if the view does not exist and is not to be created.
 */
public static CAS getView(CAS cas, String viewName, boolean create) {
  CAS view;
  try {
    view = cas.getView(viewName);
  } catch (CASRuntimeException e) {
    // View does not exist
    if (create) {
      view = cas.createView(viewName);
    } else {
      throw new IllegalArgumentException("No view with name [" + viewName + "]");
    }
  }

  return view;
}
 
Example 7
Source File: CasPool.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Checks in a CAS to the pool. This automatically calls the {@link CAS#reset()} method, to ensure
 * that when the CAS is later retrieved from the pool it will be ready to use. Also notifies other
 * Threads that may be waiting for an instance to become available.
 * 
 * Synchronized on the CAS to avoid the unnatural case where 
 * multiple threads attempt to return the same CAS to the pool
 * at the same time. 
 * 
 * @param aCas
 *          the Cas to release
 */
public void releaseCas(CAS aCas) {
  // note the pool stores references to the InitialView of each CAS
  aCas.setCurrentComponentInfo(null);  // https://issues.apache.org/jira/browse/UIMA-3655
  CAS cas = aCas.getView(CAS.NAME_DEFAULT_SOFA);

  // make sure this CAS actually belongs to this pool and is checked out
  // synchronize to avoid the same CAS being released on 2 threads
  synchronized (cas) {
    if (!mAllInstances.contains(cas) || mFreeInstances.contains(cas)) {
      UIMAFramework.getLogger(CLASS_NAME).logrb(Level.WARNING, CLASS_NAME.getName(), "releaseCas",
              LOG_RESOURCE_BUNDLE, "UIMA_return_cas_to_pool__WARNING");
    } else {
      // restore the ClassLoader and unlock the CAS, since release() can be called 
      // from within a CAS Multiplier.
      ((CASImpl)cas).restoreClassLoaderUnlockCas(); 
      
      // reset CAS
      cas.reset();
      
      // Add the CAS to the end of the free instances List
      mFreeInstances.add(cas);
      permits.release();  // should follow adding cas back to mFreeInstances
    }
  }

  // Notify any threads waiting on this object
  // not needed by UIMA Core - other users may need.
  synchronized (this) {
    notifyAll();
  }
}
 
Example 8
Source File: DebugFSLogicalStructure.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public static ViewInfo[] getOtherViews(CAS cas) {
  Iterator<SofaFS> sofaIt = cas.getSofaIterator();
  List<ViewInfo> r = new ArrayList<>();
  while (sofaIt.hasNext()) {
    SofaFS item = sofaIt.next();
    CAS oCas = cas.getView(item);
    if (oCas != cas)
      r.add(new ViewInfo(oCas));
  }
  return r.toArray(new ViewInfo[r.size()]);
}
 
Example 9
Source File: XmlDetagger.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void process(CAS aCAS) throws AnalysisEngineProcessException {
  // get handle to CAS view containing XML document
  CAS xmlCas = aCAS.getView("xmlDocument");
  InputStream xmlStream = xmlCas.getSofa().getSofaDataStream();

  // parse with detag handler
  DetagHandler handler = new DetagHandler();
  try {
    SAXParser parser = parserFactory.newSAXParser();
    parser.parse(xmlStream, handler);
  } catch (Exception e) {
    throw new AnalysisEngineProcessException(e);
  }

  // create the plain text view and set its document text
  CAS plainTextView = aCAS.createView("plainTextDocument");
  plainTextView.setDocumentText(handler.getDetaggedText());
  plainTextView.setDocumentLanguage(aCAS.getView("_InitialView").getDocumentLanguage());

  // Index the SourceDocumentInformation object, if there is one, in the new sofa.
  // This is needed by the SemanticSearchCasIndexer
  Iterator iter = xmlCas.getAnnotationIndex(sourceDocInfoType).iterator();
  if (iter.hasNext()) {
    FeatureStructure sourceDocInfoFs = (FeatureStructure) iter.next();
    plainTextView.getIndexRepository().addFS(sourceDocInfoFs);

  }

}
 
Example 10
Source File: XmiCasDeserializerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testDeltaCasIndexExistingFsInNewView() throws Exception {
    CAS cas1 = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
            indexes);
    CAS cas2 = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
            indexes);
    cas1.setDocumentText("This is a test document in the initial view");
    Type referentType = cas1.getTypeSystem().getType("org.apache.uima.testTypeSystem.Referent");
    FeatureStructure fs1 = cas1.createFS(referentType);
    cas1.getIndexRepository().addFS(fs1);

    //serialize complete
    XmiSerializationSharedData sharedData = new XmiSerializationSharedData();
    String xml = serialize(cas1, sharedData);
//    System.out.println(xml);
    int maxOutgoingXmiId = sharedData.getMaxXmiId();

    //deserialize into cas2
    XmiSerializationSharedData sharedData2 = new XmiSerializationSharedData();
    this.deserialize(xml, cas2, sharedData2, true, -1);
    CasComparer.assertEquals(cas1, cas2);

    //create Marker, add/modify fs and serialize in delta xmi format.
    Marker marker = cas2.createMarker();

    //create View
    CAS view = cas2.createView("NewView");
    //add FS to index
    Type referentType2 = cas2.getTypeSystem().getType("org.apache.uima.testTypeSystem.Referent");
    Iterator<FeatureStructure> fsIter = cas2.getIndexRepository().getAllIndexedFS(referentType2);
    while (fsIter.hasNext()) {
      FeatureStructure fs = fsIter.next();
      view.getIndexRepository().addFS(fs);
    }
    AnnotationFS cas2newAnnot = view.createAnnotation(cas2.getAnnotationType(), 6, 8);
    view.getIndexRepository().addFS(cas2newAnnot);

    // serialize cas2 in delta format
    String deltaxml1 = serialize(cas2, sharedData2, marker);
//    System.out.println(deltaxml1);

    //deserialize delta xmi into cas1
    this.deserialize(deltaxml1, cas1, sharedData, true, maxOutgoingXmiId, AllowPreexistingFS.allow);

    //check that new View contains the FS
    CAS deserView = cas1.getView("NewView");
    Iterator<FeatureStructure> deserFsIter = deserView.getIndexRepository().getAllIndexedFS(referentType);
    assertTrue(deserFsIter.hasNext());
  }
 
Example 11
Source File: XmiCasDeserializerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testDeltaCasIndexExistingFsInView() throws Exception {
    CAS cas1 = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
            indexes);
    CAS cas2 = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
            indexes);
    cas1.setDocumentText("This is a test document in the initial view");
    Type referentType = cas1.getTypeSystem().getType("org.apache.uima.testTypeSystem.Referent");
    FeatureStructure fs1 = cas1.createFS(referentType);
    cas1.getIndexRepository().addFS(fs1);

    //serialize complete
    XmiSerializationSharedData sharedData = new XmiSerializationSharedData();
    String xml = serialize(cas1, sharedData);
//    System.out.println(xml);
    int maxOutgoingXmiId = sharedData.getMaxXmiId();

    //deserialize into cas2
    XmiSerializationSharedData sharedData2 = new XmiSerializationSharedData();
    this.deserialize(xml, cas2, sharedData2, true, -1);
    CasComparer.assertEquals(cas1, cas2);

    //create Marker, add/modify fs and serialize in delta xmi format.
    Marker marker = cas2.createMarker();

    //create View
    CAS view = cas2.createView("NewView");
    //add FS to index
    Type referentType2 = cas2.getTypeSystem().getType("org.apache.uima.testTypeSystem.Referent");
    Iterator<FeatureStructure> fsIter = cas2.getIndexRepository().getAllIndexedFS(referentType2);
    while (fsIter.hasNext()) {
      FeatureStructure fs = fsIter.next();
      view.getIndexRepository().addFS(fs);
    }
    AnnotationFS cas2newAnnot = view.createAnnotation(cas2.getAnnotationType(), 6, 8);
    view.getIndexRepository().addFS(cas2newAnnot);

    // serialize cas2 in delta format
    String deltaxml1 = serialize(cas2, sharedData2, marker);
//    System.out.println(deltaxml1);

    //deserialize delta xmi into cas1
    this.deserialize(deltaxml1, cas1, sharedData, true, maxOutgoingXmiId, AllowPreexistingFS.allow);

    //check that new View contains the FS
    CAS deserView = cas1.getView("NewView");
    Iterator<FeatureStructure> deserFsIter = deserView.getIndexRepository().getAllIndexedFS(referentType);
    assertTrue(deserFsIter.hasNext());
  }
 
Example 12
Source File: XCASDeserializerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testMultipleSofas() throws Exception {
    /*************************************************
     * Make CAS with 2 sofas, initial and OtherSofa  *
     *                                               *
     * Add instance of TOP and index in both views   *
     *                                               *
     * Serialize to string "xml"                     *
     *                                               *
     * Deserialize from string                       *
     *************************************************/
    CAS cas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
    // set document text for the initial view
    cas.setDocumentText("This is a test");
    // create a new view and set its document text
    CAS cas2 = cas.createView("OtherSofa");
    cas2.setDocumentText("This is only a test");

    // Change this test to create an instance of TOP because you cannot add an annotation to other than 
    //   the view it is created in. https://issues.apache.org/jira/browse/UIMA-4099
    // create a TOP and add to index of both views
    Type topType = cas.getTypeSystem().getTopType();
    FeatureStructure aTOP = cas.createFS(topType);
    cas.getIndexRepository().addFS(aTOP);
    cas2.getIndexRepository().addFS(aTOP); 
    FSIterator<FeatureStructure> it = cas.getIndexRepository().getAllIndexedFS(topType);
    FSIterator<FeatureStructure> it2 = cas2.getIndexRepository().getAllIndexedFS(topType);
    it.next(); it.next();
    it2.next(); it2.next(); 
    assertFalse(it.hasNext());
    assertFalse(it2.hasNext());
     
    // serialize
    StringWriter sw = new StringWriter();
    XMLSerializer xmlSer = new XMLSerializer(sw, false);
    XCASSerializer xcasSer = new XCASSerializer(cas.getTypeSystem());
    xcasSer.serialize(cas, xmlSer.getContentHandler(), true);
    String xml = sw.getBuffer().toString();

    // deserialize into another CAS (repeat twice to check it still works after reset)
    CAS newCas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
    for (int i = 0; i < 2; i++) {
      XCASDeserializer newDeser = new XCASDeserializer(newCas.getTypeSystem());
      ContentHandler newDeserHandler = newDeser.getXCASHandler(newCas);
      SAXParserFactory fact = SAXParserFactory.newInstance();
      SAXParser parser = fact.newSAXParser();
      XMLReader xmlReader = parser.getXMLReader();
      xmlReader.setContentHandler(newDeserHandler);
      xmlReader.parse(new InputSource(new StringReader(xml)));

      // check sofas
      assertEquals("This is a test", newCas.getDocumentText());
      CAS newCas2 = newCas.getView("OtherSofa");
      assertEquals("This is only a test", newCas2.getDocumentText());

      // check that annotation is still indexed in both views
      it = newCas.getIndexRepository().getAllIndexedFS(topType);
      it2 = newCas2.getIndexRepository().getAllIndexedFS(topType);
      it.next(); it.next();
      it2.next(); it2.next(); 
      assertFalse(it.hasNext());
      assertFalse(it2.hasNext());
//      assertTrue(tIndex.size() == 2); // document annot and this one
//      assertTrue(t2Index.size() == 2); // ditto
      newCas.reset();  // testing if works after cas reset, go around loop 2nd time
    }
  }
 
Example 13
Source File: AnnotationViewerDialog.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Launch that viewer.
 *
 * @param inputDirPath the input dir path
 * @param fileName the file name
 * @param typeSystem the type system
 * @param aTypesToDisplay the a types to display
 * @param javaViewerRBisSelected the java viewer R bis selected
 * @param javaViewerUCRBisSelected the java viewer UCR bis selected
 * @param xmlRBisSelected the xml R bis selected
 * @param styleMapFile the style map file
 * @param viewerDirectory the viewer directory
 */
public void launchThatViewer(String inputDirPath, String fileName, TypeSystem typeSystem,
        final String[] aTypesToDisplay, boolean javaViewerRBisSelected,
        boolean javaViewerUCRBisSelected, boolean xmlRBisSelected, File styleMapFile,
        File viewerDirectory) {
  try {

    File xcasFile = new File(inputDirPath, fileName);
    // create a new CAS
    CAS cas = CasCreationUtils.createCas(Collections.EMPTY_LIST, typeSystem, UIMAFramework
            .getDefaultPerformanceTuningProperties());
    // deserialize XCAS into CAS
    try (InputStream xcasInStream = new FileInputStream(xcasFile)) {
      XmlCasDeserializer.deserialize(xcasInStream, cas, true);
    }
    
    //get the specified view
    cas = cas.getView(this.defaultCasViewName);

    // launch appropriate viewer
    if (javaViewerRBisSelected || javaViewerUCRBisSelected) { // JMP
      // record preference for next time
      med1.setViewType(javaViewerRBisSelected ? "Java Viewer" : "JV User Colors");

      // create tree viewer component
      CasAnnotationViewer viewer = new CasAnnotationViewer();
      viewer.setDisplayedTypes(aTypesToDisplay);
      if (javaViewerUCRBisSelected)
        getColorsForTypesFromFile(viewer, styleMapFile);
      else
        viewer.setHiddenTypes(new String[] { "uima.cpm.FileLocation" });
      // launch viewer in a new dialog
      viewer.setCAS(cas);
      JDialog dialog = new JDialog(AnnotationViewerDialog.this, "Annotation Results for "
              + fileName + " in " + inputDirPath); // JMP
      dialog.getContentPane().add(viewer);
      dialog.setSize(850, 630);
      dialog.pack();
      dialog.show();
    } else {
      CAS defaultView = cas.getView(CAS.NAME_DEFAULT_SOFA);
      if (defaultView.getDocumentText() == null) {
        displayError("The HTML and XML Viewers can only view the default text document, which was not found in this CAS.");
        return;
      }
      // generate inline XML
      File inlineXmlFile = new File(viewerDirectory, "inline.xml");
      CasToInlineXml casToInlineXml = new CasToInlineXml();
      casToInlineXml.setFormattedOutput(false);
      String xmlAnnotations = casToInlineXml.generateXML(defaultView);
      FileOutputStream outStream = new FileOutputStream(inlineXmlFile);
      outStream.write(xmlAnnotations.getBytes(StandardCharsets.UTF_8));
      outStream.close();

      if (xmlRBisSelected) // JMP passed in
      {
        // record preference for next time
        med1.setViewType("XML");

        BrowserUtil.openUrlInDefaultBrowser(inlineXmlFile.getAbsolutePath());
      } else
      // HTML view
      {
        med1.setViewType("HTML");
        // generate HTML view
        // first process style map if not done already
        if (!processedStyleMap) {
          if (!styleMapFile.exists()) {
            annotationViewGenerator.autoGenerateStyleMapFile(
                    promptForAE().getAnalysisEngineMetaData(), styleMapFile);
          }
          annotationViewGenerator.processStyleMap(styleMapFile);
          processedStyleMap = true;
        }
        annotationViewGenerator.processDocument(inlineXmlFile);
        File genFile = new File(viewerDirectory, "index.html");
        // open in browser
        BrowserUtil.openUrlInDefaultBrowser(genFile.getAbsolutePath());
      }
    }

    // end LTV here

  } catch (Exception ex) {
    displayError(ex);
  }
}
 
Example 14
Source File: TcasTransAnnotator.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void process(CAS aCas, ResultSpecification aResultSpec) throws AnnotatorProcessException {
  CAS engTcas, germTcas;

  // get English text Sofa and open CAS view
  // SofaID realSofaName = getContext().mapToSofaID("EnglishDocument");
  // System.out.println("TRANSANNOTATOR: real sofa name for English document " +
  // realSofaName.getSofaID());

  // engTcas = aCas.getView(aCas.getSofa(realSofaName));
  engTcas = aCas;
  // if (engTcas == null )
  // System.out.println(realSofaName + " sofa not found in CAS");

  SofaID realSofaName = getContext().mapToSofaID("GermanDocument");
  // System.out.println("TRANSANNOTATOR: real sofa name of GermanDocument " +
  // realSofaName.getSofaID());
  // Create the output German text Sofa and open CAS view
  germTcas = aCas.getView(aCas.createSofa(realSofaName, "text"));

  // Get some necessary Type System constants
  Type annot = engTcas.getAnnotationType();
  Type cross = engTcas.getTypeSystem().getType("sofa.test.CrossAnnotation");
  Feature other = cross.getFeatureByBaseName("otherAnnotation");

  // Get the English text
  String engText = engTcas.getDocumentText();

  // Setup for translated text
  int engEnd = 0;
  int germBegin = 0;
  int germEnd = 0;
  StringBuffer translation = new StringBuffer();

  // Parse the English text
  StringTokenizer st = new StringTokenizer(engText);
  while (st.hasMoreTokens()) {
    String thisTok = st.nextToken();
    int engBegin = engText.indexOf(thisTok, engEnd);
    engEnd = engBegin + thisTok.length();

    // Create token annotations on English text
    AnnotationFS engAnnot = engTcas.createAnnotation(annot, engBegin, engEnd);
    engTcas.getIndexRepository().addFS(engAnnot);

    // Simple word-by-word translation
    String germWord = Translate(thisTok);

    // Accumulate the translated text
    if (germBegin > 0) {
      translation.append(' ');
      germBegin += 1;
    }
    translation.append(germWord.toCharArray(), 0, germWord.length());

    // Create token annotations on German text
    germEnd = germBegin + germWord.length();
    AnnotationFS germAnnot = germTcas.createAnnotation(cross, germBegin, germEnd);
    germTcas.getIndexRepository().addFS(germAnnot);

    // add link to English text
    germAnnot.setFeatureValue(other, engAnnot);
    germBegin = germEnd;
  }

  // Finally, set the output tranlation Sofa data
  germTcas.setDocumentText(translation.toString());

}
 
Example 15
Source File: TransAnnotator.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void process(CAS aCas, ResultSpecification aResultSpec) throws AnnotatorProcessException {
  CAS engTcas, germTcas;

  // get English text Sofa and open CAS view
  SofaID realSofaName = getContext().mapToSofaID("EnglishDocument");
  // System.out.println("TRANSANNOTATOR: real sofa name for English document " +
  // realSofaName.getSofaID());

  engTcas = aCas.getView(aCas.getSofa(realSofaName));
  // if (engTcas == null)
  // System.out.println(realSofaName + " sofa not found in CAS");

  realSofaName = getContext().mapToSofaID("GermanDocument");
  // System.out.println("TRANSANNOTATOR: real sofa name of GermanDocument " +
  // realSofaName.getSofaID());
  // Create the output German text Sofa and open CAS view
  germTcas = aCas.getView(aCas.createSofa(realSofaName, "text"));

  // Get some necessary Type System constants
  Type annot = engTcas.getAnnotationType();
  Type cross = engTcas.getTypeSystem().getType("sofa.test.CrossAnnotation");
  Feature other = cross.getFeatureByBaseName("otherAnnotation");

  // Get the English text
  String engText = engTcas.getDocumentText();

  // Setup for translated text
  int engEnd = 0;
  int germBegin = 0;
  int germEnd = 0;
  StringBuffer translation = new StringBuffer();

  // Parse the English text
  StringTokenizer st = new StringTokenizer(engText);
  while (st.hasMoreTokens()) {
    String thisTok = st.nextToken();
    int engBegin = engText.indexOf(thisTok, engEnd);
    engEnd = engBegin + thisTok.length();

    // Create token annotations on English text
    AnnotationFS engAnnot = engTcas.createAnnotation(annot, engBegin, engEnd);
    engTcas.getIndexRepository().addFS(engAnnot);

    // Simple word-by-word translation
    String germWord = Translate(thisTok);

    // Accumulate the translated text
    if (germBegin > 0) {
      translation.append(' ');
      germBegin += 1;
    }
    translation.append(germWord.toCharArray(), 0, germWord.length());

    // Create token annotations on German text
    germEnd = germBegin + germWord.length();
    AnnotationFS germAnnot = germTcas.createAnnotation(cross, germBegin, germEnd);
    germTcas.getIndexRepository().addFS(germAnnot);

    // add link to English text
    germAnnot.setFeatureValue(other, engAnnot);
    germBegin = germEnd;
  }

  // Finally, set the output tranlation Sofa data
  germTcas.setDocumentText(translation.toString());

}
 
Example 16
Source File: BlueAnnotationViewer.java    From bluima with Apache License 2.0 4 votes vote down vote up
public void createHtml(JCas jCas, TypeSystem typeSystem, File styleMapFile,
        File outputDirectory) throws IOException {
    try {

        FileUtils.forceMkdir(outputDirectory);
        generator.setOutputDirectory(outputDirectory);

        CAS cas = jCas.getCas();

        // get the specified view
        cas = cas.getView(this.defaultCasViewName);

        CAS defaultView = cas.getView(CAS.NAME_DEFAULT_SOFA);
        if (defaultView.getDocumentText() == null) {
            System.err
                    .println("The HTML and XML Viewers can only view the default text document, which was not found in this CAS.");
            return;
        }

        // generate inline XML
        File inlineXmlFile = new File(outputDirectory, "inline.xml");
        String xmlAnnotations = new CasToInlineXml()
                .generateXML(defaultView);
        FileOutputStream outStream = new FileOutputStream(inlineXmlFile);
        outStream.write(xmlAnnotations.getBytes("UTF-8"));
        outStream.close();

        // generate HTML view
        // if (!styleMapFile.exists()) {
        // AnalysisEngineDescription aed = null;
        //
        // annotationViewGenerator.autoGenerateStyleMapFile(
        // aed.getAnalysisEngineMetaData(), styleMapFile);
        // }
        generator.processStyleMap(styleMapFile);
        generator.processDocument(inlineXmlFile);
        // File genFile = new File(viewerDirectory, "index.html");

    } catch (Exception ex) {
        throw new IOException("cannot create html annotationviewer", ex);
    }
}
 
Example 17
Source File: TransAnnotator.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void process(CAS aCas, ResultSpecification aResultSpec) throws AnnotatorProcessException {
  CAS engTcas, germTcas;

  // get English text Sofa and open CAS view
  SofaID realSofaName = getContext().mapToSofaID("EnglishDocument");
  // System.out.println("TRANSANNOTATOR: real sofa name for English document " +
  // realSofaName.getSofaID());

  engTcas = aCas.getView(aCas.getSofa(realSofaName));
  // if (engTcas == null)
  // System.out.println(realSofaName + " sofa not found in CAS");

  realSofaName = getContext().mapToSofaID("GermanDocument");
  // System.out.println("TRANSANNOTATOR: real sofa name of GermanDocument " +
  // realSofaName.getSofaID());
  // Create the output German text Sofa and open CAS view
  germTcas = aCas.getView(aCas.createSofa(realSofaName, "text"));

  // Get some necessary Type System constants
  Type annot = engTcas.getAnnotationType();
  Type cross = engTcas.getTypeSystem().getType("sofa.test.CrossAnnotation");
  Feature other = cross.getFeatureByBaseName("otherAnnotation");

  // Get the English text
  String engText = engTcas.getDocumentText();

  // Setup for translated text
  int engEnd = 0;
  int germBegin = 0;
  int germEnd = 0;
  StringBuffer translation = new StringBuffer();

  // Parse the English text
  StringTokenizer st = new StringTokenizer(engText);
  while (st.hasMoreTokens()) {
    String thisTok = st.nextToken();
    int engBegin = engText.indexOf(thisTok, engEnd);
    engEnd = engBegin + thisTok.length();

    // Create token annotations on English text
    AnnotationFS engAnnot = engTcas.createAnnotation(annot, engBegin, engEnd);
    engTcas.getIndexRepository().addFS(engAnnot);

    // Simple word-by-word translation
    String germWord = Translate(thisTok);

    // Accumulate the translated text
    if (germBegin > 0) {
      translation.append(' ');
      germBegin += 1;
    }
    translation.append(germWord.toCharArray(), 0, germWord.length());

    // Create token annotations on German text
    germEnd = germBegin + germWord.length();
    AnnotationFS germAnnot = germTcas.createAnnotation(cross, germBegin, germEnd);
    germTcas.getIndexRepository().addFS(germAnnot);

    // add link to English text
    germAnnot.setFeatureValue(other, engAnnot);
    germBegin = germEnd;
  }

  // Finally, set the output tranlation Sofa data
  germTcas.setDocumentText(translation.toString());

}
 
Example 18
Source File: AnnotationInsertingWriter.java    From biomedicus with Apache License 2.0 4 votes vote down vote up
@Override
public void process(CAS aCAS) throws AnalysisEngineProcessException {
  CAS originalDocumentView = aCAS.getView(rtfDocumentName);
  SymbolIndexedDocument symbolIndexedDocument =
      SymbolIndexedDocument.fromView(originalDocumentView);

  CAS view = aCAS.getView(documentName);

  TreeSet<Integer> covered = new TreeSet<>();
  for (String annotationType : Objects.requireNonNull(annotationTypes)) {
    Type type = view.getTypeSystem().getType(annotationType);

    AnnotationIndex<Annotation> annotationIndex = view.getAnnotationIndex(type);

    for (Annotation annotation : annotationIndex) {
      IntStream.rangeClosed(annotation.getBegin(), annotation.getEnd()).forEach(covered::add);
    }
  }

  Iterator<Integer> iterator = covered.iterator();
  int next = iterator.next();
  int last = -1;
  while (iterator.hasNext()) {
    int first = next;
    while (iterator.hasNext()) {
      last = next;
      next = iterator.next();
      if (next - last > 1) {
        break;
      }
    }
    RegionTaggerBuilder.create()
        .withBeginTag("\\u2222221B ")
        .withEndTag("\\u2222221E ")
        .withSymbolIndexedDocument(symbolIndexedDocument)
        .withDestinationName(documentName)
        .withBegin(first)
        .withEnd(last)
        .createRegionTagger()
        .tagRegion();
  }

  String rewrittenDocument = symbolIndexedDocument.getDocument();

  Artifact artifact = UimaAdapters.getArtifact(aCAS, null);

  Path file = outputDir.resolve(artifact.getArtifactID() + ".rtf");

  try (BufferedWriter bufferedWriter = Files
      .newBufferedWriter(file, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)) {
    bufferedWriter.write(rewrittenDocument);
  } catch (IOException e) {
    throw new AnalysisEngineProcessException(e);
  }
}
 
Example 19
Source File: XmiCasDeserializerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testMultipleSofas() throws Exception {
    try {
      CAS cas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
              new FsIndexDescription[0]);
      // set document text for the initial view
      cas.setDocumentText("This is a test");
      // create a new view and set its document text
      CAS cas2 = cas.createView("OtherSofa");
      cas2.setDocumentText("This is only a test");

      // Change this test to create an instance of TOP because you cannot add an annotation to other than 
      //   the view it is created in. https://issues.apache.org/jira/browse/UIMA-4099
      // create a TOP and add to index of both views
      Type topType = cas.getTypeSystem().getTopType();
      FeatureStructure aTOP = cas.createFS(topType);
      cas.getIndexRepository().addFS(aTOP);
      cas2.getIndexRepository().addFS(aTOP); 
      FSIterator<FeatureStructure> it = cas.getIndexRepository().getAllIndexedFS(topType);
      FSIterator<FeatureStructure> it2 = cas2.getIndexRepository().getAllIndexedFS(topType);
      it.next(); it.next();
      it2.next(); it2.next(); 
      assertFalse(it.hasNext());
      assertFalse(it2.hasNext());

      // serialize
      StringWriter sw = new StringWriter();
      XMLSerializer xmlSer = new XMLSerializer(sw, false);
      XmiCasSerializer xmiSer = new XmiCasSerializer(cas.getTypeSystem());
      xmiSer.serialize(cas, xmlSer.getContentHandler());
      String xml = sw.getBuffer().toString();

      // deserialize into another CAS (repeat twice to check it still works after reset)
      CAS newCas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
              new FsIndexDescription[0]);
      for (int i = 0; i < 2; i++) {
        XmiCasDeserializer newDeser = new XmiCasDeserializer(newCas.getTypeSystem());
        ContentHandler newDeserHandler = newDeser.getXmiCasHandler(newCas);
        SAXParserFactory fact = SAXParserFactory.newInstance();
        SAXParser parser = fact.newSAXParser();
        XMLReader xmlReader = parser.getXMLReader();
        xmlReader.setContentHandler(newDeserHandler);
        xmlReader.parse(new InputSource(new StringReader(xml)));

        // check sofas
        assertEquals("This is a test", newCas.getDocumentText());
        CAS newCas2 = newCas.getView("OtherSofa");
        assertEquals("This is only a test", newCas2.getDocumentText());

        // check that annotation is still indexed in both views
        // check that annotation is still indexed in both views
        it = newCas.getIndexRepository().getAllIndexedFS(topType);
        it2 = newCas2.getIndexRepository().getAllIndexedFS(topType);
        it.next(); it.next();
        it2.next(); it2.next(); 
        assertFalse(it.hasNext());
//        assertFalse(it2.hasNext());        assertTrue(tIndex.size() == 2); // document annot and this one
//        assertTrue(t2Index.size() == 2); // ditto

        newCas.reset();
      }
    } catch (Exception e) {
      JUnitExtension.handleException(e);
    }
  }
 
Example 20
Source File: CasUtil.java    From uima-uimafit with Apache License 2.0 3 votes vote down vote up
/**
 * Convenience method to get the specified view or a default view if the requested view does not
 * exist. The default can also be {@code null}.
 * 
 * @param cas
 *          a CAS
 * @param viewName
 *          the requested view.
 * @param fallback
 *          the default view if the requested view does not exist.
 * @return the requested view or the default if the requested view does not exist.
 */
public static CAS getView(CAS cas, String viewName, CAS fallback) {
  CAS view;
  try {
    view = cas.getView(viewName);
  } catch (CASRuntimeException e) {
    // use fall-back view instead
    view = fallback;
  }
  return view;
}