Examples of JCas


Examples of org.apache.uima.jcas.JCas

  public void testCasToInlineXml() throws Exception {
    // Jira https://issues.apache.org/jira/browse/UIMA-2406
    CAS srcCas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
   
    JCas jcas = srcCas.getJCas();
   
    jcas.setDocumentText("1 2 3 4 5 6 7 8 9");
    OfShorts f = new OfShorts(jcas);
    ShortArray a = new ShortArray(jcas, 3);
    a.set(0, (short)0);
    a.set(1, (short)1);
    a.set(2, (short)2);
View Full Code Here

Examples of org.apache.uima.jcas.JCas

    AnnotationFS annot = (AnnotationFS) setIndex.iterator().get();
    assertNotNull(setIndex.find(annot));
    assertNull(setIndex.find(this.cas.createAnnotation(this.annotationType, -1, -1)));

    // do same for JCas
    JCas jcas = null;
    try {
      jcas = this.cas.getJCas();
    } catch (CASException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      assertTrue(false);
    }
    FSIndex jcasSetIndex = jcas.getJFSIndexRepository().getIndex(CASTestSetup.ANNOT_SET_INDEX);
    Annotation jcasAnnotation = (Annotation) jcasSetIndex.find(annot);
    assertNotNull(jcasAnnotation);
    assertNull(jcasSetIndex.find(this.cas.createAnnotation(this.annotationType, -1, -1)));

    // /////////////////////////////////////////////////////////////////////////
View Full Code Here

Examples of org.apache.uima.jcas.JCas

      }
     
      // Add a new Sofa
      // id.setSofaID("GermanDocument");
      // Sofa gs = new Sofa(jcas, id, "text");
      JCas gerJcas = jcas.createView("GermanDocument");
      Sofa gs = gerJcas.getSofa();
      assertTrue(3 == gs.getSofaRef());

      // Set the document text
      // gs.setLocalSofaData("das bier ist gut");
      gerJcas.setDocumentText("das bier ist gut");

      // Test multiple Sofas across binary serialization
      CASSerializer cs = Serialization.serializeNoMetaData(cas);
      cas = Serialization.createCAS(casMgr, cs);

      // Add a new Sofa
      // id.setSofaID("FrenchDocument");
      // Sofa fs = new Sofa(jcas, id, "text");
      CAS frCas = jcas.getCas().createView("FrenchDocument");
      SofaFS fs = frCas.getSofa();
      assertTrue(4 == fs.getSofaRef());

      // Open JCas views of some Sofas
      JCas engJcas = cas.getJCas(es);
      JCas frJcas = jcas.getView("FrenchDocument");

      // Set the document text after the Jcas view exists using JCas.setDocumentText method
      frJcas.setSofaDataString("cette biere est bonne", "text");

      // Create standard annotations against eng & fr and cross annotations against ger
      int engEnd = 0;
      int gerEnd = 0;
      int frEnd = 0;
      String engText = engJcas.getDocumentText();
      String gerText = gerJcas.getDocumentText();
      String frText = frJcas.getDocumentText();
      StringTokenizer est = new StringTokenizer(engText);
      StringTokenizer gst = new StringTokenizer(gerText);
      StringTokenizer fst = new StringTokenizer(frText);

      while (est.hasMoreTokens()) {
        assertTrue(gst.hasMoreTokens());
        assertTrue(fst.hasMoreTokens());

        String eTok = est.nextToken();
        int engBegin = engText.indexOf(eTok, engEnd);
        engEnd = engBegin + eTok.length();

        String gTok = gst.nextToken();
        int gerBegin = gerText.indexOf(gTok, gerEnd);
        gerEnd = gerBegin + gTok.length();

        String fTok = fst.nextToken();
        int frBegin = frText.indexOf(fTok, frEnd);
        frEnd = frBegin + fTok.length();

        Annotation engAnnot = new Annotation(engJcas, engBegin, engEnd);
        engAnnot.addToIndexes();

        Annotation frAnnot = new Annotation(frJcas, frBegin, frEnd);
        frAnnot.addToIndexes();

        CrossAnnotation gerAnnot = new CrossAnnotation(gerJcas);
        gerAnnot.setBegin(gerBegin);
        gerAnnot.setEnd(gerEnd);
        gerAnnot.setOtherAnnotation(engAnnot);
        gerAnnot.addToIndexes();
      }

      // Test that the annotations are in separate index spaces, and that Sofas are indexed
      JFSIndexRepository indexes = jcas.getJFSIndexRepository();
      // FSIndex sofaIndex = indexes.getIndex(CAS.SOFA_INDEX_NAME);
      indexes = engJcas.getJFSIndexRepository();
      FSIndex engIndex = indexes.getAnnotationIndex(Annotation.type);
      indexes = gerJcas.getJFSIndexRepository();
      FSIndex gerIndex = indexes.getAnnotationIndex(Annotation.type);
      indexes = frJcas.getJFSIndexRepository();
      FSIndex frIndex = indexes.getAnnotationIndex(Annotation.type);
      FSIterator sofaIter = jcas.getSofaIterator();
      int numSofas = 0;
      while (sofaIter.isValid()) {
        numSofas++;
View Full Code Here

Examples of org.apache.uima.jcas.JCas

      // Create a local Sofa and set string feature
      // SofaID_impl id = new SofaID_impl();
      // id.setSofaID("StringSofaData");
      // Sofa strSofa = new Sofa(jcas, id, "text");
      // strSofa.setLocalSofaData("this beer is good");
      JCas stringView = jcas.createView("StringSofaData");
      stringView.setDocumentText("this beer is good");

      // create a int array fs
      IntegerArray intArrayFS = new IntegerArray(jcas, 5);
      intArrayFS.set(0, 1);
      intArrayFS.set(1, 2);
      intArrayFS.set(2, 3);
      intArrayFS.set(3, 4);
      intArrayFS.set(4, 5);
      // create a Sofa and set the SofaArray feature to an int array FS.
      // id = new SofaID_impl();
      // id.setSofaID("intArraySofaData");
      // SofaFS intarraySofaFS = new Sofa(jcas, id, "text");
      // intarraySofaFS.setLocalSofaData(intArrayFS);
      JCas intArrayView = jcas.createView("intArraySofaData");
      intArrayView.setSofaDataArray(intArrayFS, "integers");

      // create a float array fs
      FloatArray floatArrayFS = new FloatArray(jcas, 5);
      floatArrayFS.set(0, (float) 0.1);
      floatArrayFS.set(1, (float) 0.2);
      floatArrayFS.set(2, (float) 0.3);
      floatArrayFS.set(3, (float) 0.4);
      floatArrayFS.set(4, (float) 0.5);
      // create a sofa and set the SofaArray feature to the float array
      // id = new SofaID_impl();
      // id.setSofaID("floatArraySofaData");
      // SofaFS floatarraySofaFS = new Sofa(jcas, id, "text");
      // floatarraySofaFS.setLocalSofaData(floatArrayFS);
      JCas floatArrayView = jcas.createView("floatArraySofaData");
      floatArrayView.setSofaDataArray(floatArrayFS, "floats");

      // create a short array fs
      ShortArray shortArrayFS = new ShortArray(jcas, 5);
      shortArrayFS.set(0, (short) 128);
      shortArrayFS.set(1, (short) 127);
      shortArrayFS.set(2, (short) 126);
      shortArrayFS.set(3, (short) 125);
      shortArrayFS.set(4, (short) 124);
      // create a Sofa and set the SofaArray feature to an int array FS.
      JCas shortArrayView = jcas.createView("shortArraySofaData");
      shortArrayView.setSofaDataArray(shortArrayFS, "shorts");

      // create a byte array fs
      ByteArray byteArrayFS = new ByteArray(jcas, 5);
      byteArrayFS.set(0, (byte) 8);
      byteArrayFS.set(1, (byte) 16);
      byteArrayFS.set(2, (byte) 64);
      byteArrayFS.set(3, (byte) 128);
      byteArrayFS.set(4, (byte) 255);
      // create a Sofa and set the SofaArray feature.
      JCas byteArrayView = jcas.createView("byteArraySofaData");
      byteArrayView.setSofaDataArray(byteArrayFS, "bytes");

      // create a long array fs
      LongArray longArrayFS = new LongArray(jcas, 5);
      longArrayFS.set(0, Long.MAX_VALUE);
      longArrayFS.set(1, Long.MAX_VALUE - 1);
      longArrayFS.set(2, Long.MAX_VALUE - 2);
      longArrayFS.set(3, Long.MAX_VALUE - 3);
      longArrayFS.set(4, Long.MAX_VALUE - 4);
      // create a Sofa and set the SofaArray feature.
      JCas longArrayView = jcas.createView("longArraySofaData");
      longArrayView.setSofaDataArray(longArrayFS, "longs");

      DoubleArray doubleArrayFS = new DoubleArray(jcas, 5);
      doubleArrayFS.set(0, Double.MAX_VALUE);
      doubleArrayFS.set(1, Double.MIN_VALUE);
      doubleArrayFS.set(2, Double.parseDouble("1.5555"));
      doubleArrayFS.set(3, Double.parseDouble("99.000000005"));
      doubleArrayFS.set(4, Double.parseDouble("4.44444444444444444"));
      // create a Sofa and set the SofaArray feature.
      JCas doubleArrayView = jcas.createView("doubleArraySofaData");
      doubleArrayView.setSofaDataArray(doubleArrayFS, "doubles");

      // create remote sofa and set the SofaURI feature
      JCas remoteView = jcas.createView("remoteSofaData");
      String sofaFileName = "./Sofa.xcas";
      remoteView.setSofaDataURI("file:" + sofaFileName, "text");
      PrintWriter out = new PrintWriter(sofaFileName);
      out.print("this beer is good");
      out.close();
     
      // read sofa data
      InputStream is = stringView.getSofaDataStream();
      assertTrue(is != null);
      byte[] dest = new byte[1];
      StringBuffer buf = new StringBuffer();
      while (is.read(dest) != -1) {
        buf.append((char) dest[0]);
      }
      assertTrue(buf.toString().equals("this beer is good"));

      dest = new byte[4];
      is.close();
      is = intArrayView.getSofaDataStream();
      assertTrue(is != null);
      BufferedInputStream bis = new BufferedInputStream(is);
      int i = 0;
      while (bis.read(dest) != -1) {
        assertTrue(ByteBuffer.wrap(dest).getInt() == intArrayFS.get(i++));
      }

      bis.close();
     
      is = floatArrayView.getSofaDataStream();
      assertTrue(is != null);
      bis = new BufferedInputStream(is);
      i = 0;
      while (bis.read(dest) != -1) {
        assertTrue(ByteBuffer.wrap(dest).getFloat() == floatArrayFS.get(i++));
      }

      dest = new byte[2];
      bis.close();
      is = shortArrayView.getSofaDataStream();
      assertTrue(is != null);
      bis = new BufferedInputStream(is);
      i = 0;
      while (bis.read(dest) != -1) {
        assertTrue(ByteBuffer.wrap(dest).getShort() == shortArrayFS.get(i++));
      }

      dest = new byte[1];
      bis.close();
      is = byteArrayView.getSofaDataStream();
      assertTrue(is != null);
      bis = new BufferedInputStream(is);
      i = 0;
      while (bis.read(dest) != -1) {
        assertTrue(ByteBuffer.wrap(dest).get() == byteArrayFS.get(i++));
      }

      dest = new byte[8];
      bis.close();
      is = longArrayView.getSofaDataStream();
      assertTrue(is != null);
      bis = new BufferedInputStream(is);
      i = 0;
      while (bis.read(dest) != -1) {
        assertTrue(ByteBuffer.wrap(dest).getLong() == longArrayFS.get(i++));
      }

      bis.close();
      is = doubleArrayView.getSofaDataStream();
      assertTrue(is != null);
      bis = new BufferedInputStream(is);
      i = 0;
      while (bis.read(dest) != -1) {
        assertTrue(ByteBuffer.wrap(dest).getDouble() == doubleArrayFS.get(i++));
      }

      dest = new byte[1];
      bis.close();
      is = remoteView.getSofaDataStream();
      assertTrue(is != null);
      buf = new StringBuffer();
      while (is.read(dest) != -1) {
        buf.append((char) dest[0]);
      }
View Full Code Here

Examples of org.apache.uima.jcas.JCas

  }
 
  public void testIndexTwice() throws Exception {
    try {
      CAS newCas = CasCreationUtils.createCas(new TypeSystemDescription_impl(), null, null);
      JCas newJCas = newCas.getJCas();
      CAS view = newCas.createView("DetaggedView");
      view.getJCas();

      Annotation annot = new Annotation(newJCas);
      annot.addToIndexes();
     
      Iterator<Annotation> annotIter = newJCas.getAnnotationIndex(Annotation.type).iterator();
      Annotation annot2 = annotIter.next();
      assertEquals(annot, annot2);
      assertEquals(annot2.getSofa(), annot2.getCASImpl().getSofa());
     
      annot2.addToIndexes();
View Full Code Here

Examples of org.apache.uima.jcas.JCas

      TypeSystemDescription typeSystem = UIMAFramework.getXMLParser().parseTypeSystemDescription(
              new XMLInputSource(typeSystemFile));
      CAS newCas = CasCreationUtils.createCas(typeSystem, null, null);
      File xcasFile = JUnitExtension.getFile("ExampleCas/multiSofaCas.xml");
      XCASDeserializer.deserialize(new FileInputStream(xcasFile), newCas);
      JCas newJCas = newCas.getJCas();
     
      SofaID sofaId = new SofaID_impl("EnglishDocument");
      JCas view = newJCas.getView(newJCas.getSofa(sofaId));
    }
    catch (Exception e) {
      JUnitExtension.handleException(e);     
    }     
  }
View Full Code Here

Examples of org.apache.uima.jcas.JCas

   *           if there is an error in processing the Resource
   *
   * @see org.apache.uima.collection.base_cpm.CasObjectProcessor#processCas(org.apache.uima.cas.CAS)
   */
  public void processCas(CAS aCAS) throws ResourceProcessException {
    JCas jcas;
    try {
      jcas = aCAS.getJCas();
    } catch (CASException e) {
      throw new ResourceProcessException(e);
    }

    // retrieve the filename of the input file from the CAS
    File outFile = null;
    boolean hasDefaultView = false;

    if (mTEXT) {
      // get the default View if it exists
      try {
        jcas = aCAS.getView(CAS.NAME_DEFAULT_SOFA).getJCas();
        hasDefaultView = true;
        FSIterator it = jcas.getAnnotationIndex(SourceDocumentInformation.type).iterator();
        if (it.hasNext()) {
          // get the output file name from the annotation in the CAS ...
          // ... note this is a little flakey if processing an XCAS file,
          // which could have such an annotation with a different name than the input XCAS file!
          // So we don't do this if XCAS output is specified.
View Full Code Here

Examples of org.apache.uima.jcas.JCas

   * @see org.apache.uima.collection.base_cpm.CasObjectProcessor#processCas(org.apache.uima.cas.CAS)
   */
  public void processCas(CAS aCAS) throws ResourceProcessException {
    String modelFileName = null;

    JCas jcas;
    try {
      jcas = aCAS.getJCas();
    } catch (CASException e) {
      throw new ResourceProcessException(e);
    }

    // retrieve the filename of the input file from the CAS
    FSIterator it = jcas.getAnnotationIndex(SourceDocumentInformation.type).iterator();
    File outFile = null;
    if (it.hasNext()) {
      SourceDocumentInformation fileLoc = (SourceDocumentInformation) it.next();
      File inFile;
      try {
        // handle blanks in path
        // https://issues.apache.org/jira/browse/UIMA-1748
        // use 3 arg form of URI Constructor to properly quote any otherwise illegal chars such as blank
        // https://issues.apache.org/jira/browse/UIMA-2097
        URI uri = UriUtils.quote(fileLoc.getUri());
        inFile = new File(uri);
        String outFileName = inFile.getName();
        if (fileLoc.getOffsetInSource() > 0) {
          outFileName += ("_" + fileLoc.getOffsetInSource());
        }
        outFileName += ".xmi";
        outFile = new File(mOutputDir, outFileName);
        modelFileName = mOutputDir.getAbsolutePath() + "/" + inFile.getName() + ".ecore";
      } catch (URISyntaxException e) {
        // bad URI, use default processing below
      }
    }
    if (outFile == null) {
      outFile = new File(mOutputDir, "doc" + mDocNum++ + ".xmi"); // Jira UIMA-629
    }
    // serialize XCAS and write to output file
    try {
      writeXmi(jcas.getCas(), outFile, modelFileName);
    } catch (IOException e) {
      throw new ResourceProcessException(e);
    } catch (SAXException e) {
      throw new ResourceProcessException(e);
    }
View Full Code Here

Examples of org.apache.uima.jcas.JCas

  /**
   * @see org.apache.uima.collection.CollectionReader#getNext(org.apache.uima.cas.CAS)
   */
  public void getNext(CAS aCAS) throws IOException, CollectionException {
    JCas jcas;
    try {
      jcas = aCAS.getJCas();
    } catch (CASException e) {
      throw new CollectionException(e);
    }

    // open input stream to file
    File file = (File) mFiles.get(mCurrentIndex++);
    FileInputStream fis = new FileInputStream(file);
    if (mTEXT) {
      try {
        // if there's a CAS Initializer, call it
        if (getCasInitializer() != null) {
          getCasInitializer().initializeCas(fis, aCAS);
        } else // No CAS Initiliazer, so read file and set document text ourselves
        {
          String text = FileUtils.file2String(file, mEncoding);     
          // put document text in JCas
          jcas.setDocumentText(text);
        }
      } finally {
        if (fis != null)
          fis.close();
      }

      // set language if it was explicitly specified as a configuration parameter
      if (mLanguage != null) {
        jcas.setDocumentLanguage(mLanguage);
      }

      // Also store location of source document in CAS. This information is critical
      // if CAS Consumers will need to know where the original document contents are located.
      // For example, the Semantic Search CAS Indexer writes this information into the
View Full Code Here

Examples of org.apache.uima.jcas.JCas

   *           if there is an error in processing the Resource
   *
   * @see org.apache.uima.collection.base_cpm.CasObjectProcessor#processCas(org.apache.uima.cas.CAS)
   */
  public void processCas(CAS aCAS) throws ResourceProcessException {
    JCas jcas;
    try {
      jcas = aCAS.getJCas();
    } catch (CASException e) {
      throw new ResourceProcessException(e);
    }

    // retrieve the filename of the input file from the CAS
    FSIterator it = jcas.getAnnotationIndex(SourceDocumentInformation.type).iterator();
    File outFile = null;
    if (it.hasNext()) {
      SourceDocumentInformation fileLoc = (SourceDocumentInformation) it.next();
      File inFile;
      try {
        // handle blanks in path
        // https://issues.apache.org/jira/browse/UIMA-1748
        // use 3 arg form of URI Constructor to properly quote any otherwise illegal chars such as blank
        // https://issues.apache.org/jira/browse/UIMA-2097
        URI uri = UriUtils.quote(fileLoc.getUri());
        inFile = new File(uri);

        String outFileName = inFile.getName();
        if (fileLoc.getOffsetInSource() > 0) {
          outFileName += fileLoc.getOffsetInSource();
        }
        outFile = new File(mOutputDir, outFileName);
      } catch (URISyntaxException e) {
        // invalid URL, use default processing below
      }
    }
    if (outFile == null) {
      outFile = new File(mOutputDir, "doc" + mDocNum++);
    }
    // serialize XCAS and write to output file
    try {
      writeXCas(jcas.getCas(), outFile);
    } catch (IOException e) {
      throw new ResourceProcessException(e);
    } catch (SAXException e) {
      throw new ResourceProcessException(e);
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.