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

The following examples show how to use org.apache.uima.cas.CAS#getJCas() . 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: CasManager_impl.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Gets a specified interface to a CAS.
 * 
 * @param cas
 *          The CAS
 * @param requiredInterface
 *          interface to get. Currently must be one of CAS or JCas.
 */
public AbstractCas getCasInterface(CAS cas, Class<? extends AbstractCas> requiredInterface) {
  if (requiredInterface == CAS.class) {
    return cas;
  } else if (requiredInterface == JCas.class) {
    try {
      return cas.getJCas();
    } catch (CASException e) {
      throw new UIMARuntimeException(e);
    }
  } else if (requiredInterface.isInstance(cas)) // covers AbstractCas
  {
    return cas;
  }
  {
    throw new UIMARuntimeException(UIMARuntimeException.UNSUPPORTED_CAS_INTERFACE,
            new Object[] { requiredInterface });
  }
}
 
Example 3
Source File: NewsleakCsvStreamReader.java    From newsleak with GNU Affero General Public License v3.0 6 votes vote down vote up
public void getNext(CAS cas) throws IOException, CollectionException {
	currentRecord++;
	JCas jcas;
	try {
		jcas = cas.getJCas();
	} catch (CASException e) {
		throw new CollectionException(e);
	}

	// Set document data
	CSVRecord record = recordsIterator.next();
	String docId = record.get(0); // external document id from CSV file

	jcas.setDocumentText(cleanBodyText(record.get(1)));
	jcas.setDocumentLanguage(record.size() > 3 ? record.get(3) : defaultLanguage);

	// Set metadata
	Metadata metaCas = new Metadata(jcas);
	metaCas.setDocId(docId);
	metaCas.setTimestamp(record.get(2));
	metaCas.addToIndexes();

	// metadata
	// is assumed to be provided from external prcessing in a separate file

}
 
Example 4
Source File: CasManager_impl.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Gets a specified interface to a CAS.
 * 
 * @param cas
 *          The CAS
 * @param requiredInterface
 *          interface to get. Currently must be one of CAS or JCas.
 * @param <T> the particular interface
 * @return -         
 */
@SuppressWarnings("unchecked")
public static <T extends AbstractCas> T getCasInterfaceStatic(CAS cas, Class<T> requiredInterface) {
  if (requiredInterface == CAS.class) {
    return (T) cas;
  } else if (requiredInterface == JCas.class) {
    try {
      return (T) cas.getJCas();
    } catch (CASException e) {
      throw new UIMARuntimeException(e);
    }
  } else if (requiredInterface.isInstance(cas)) // covers AbstractCas
  {
    return (T) cas;
  }
  {
    throw new UIMARuntimeException(UIMARuntimeException.UNSUPPORTED_CAS_INTERFACE,
            new Object[] { requiredInterface });
  }
}
 
Example 5
Source File: JcasSofaTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
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();
  }
  catch (Exception e) {
    JUnitExtension.handleException(e);      
  }    
}
 
Example 6
Source File: WordPressXMLReader.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public void getNext(CAS aCAS) throws IOException, CollectionException {
  JCas jcas;
  try {
    jcas = aCAS.getJCas();
  } catch (CASException var6) {
    throw new CollectionException(var6);
  }

  try {
    if (this.xmlReader == null) {
      WstxInputFactory e = new WstxInputFactory();
      this.xmlReader = e.createXMLStreamReader((File) this.xmlFiles.get(this.currentParsedFile));
      this.iDoc = 0;
    }

    this.parseSubDocument(jcas);
    System.out.println(jcas.getDocumentText());
    ++this.iDoc;
    if (this.xmlReader.getDepth() < 2) {
      this.xmlReader.closeCompletely();
      this.xmlReader = null;
      ++this.currentParsedFile;
    }

  } catch (XMLStreamException var4) {
    var4.printStackTrace();
    throw new CollectionException(var4);
  } catch (Exception var5) {
    var5.printStackTrace();
    throw new CollectionException(var5);
  }
}
 
Example 7
Source File: ReadFactTSV.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
private void convert(CAS aCAS) throws IOException, CollectionException, AnalysisEngineProcessException, CASException  {
  JCas jcas;
  try {
    jcas = aCAS.getJCas();
  } catch (CASException e) {
    throw new CollectionException(e);
  }

  JCasBuilder doc = new JCasBuilder(jcas);
  Fact fact = facts.poll();
  if(fact == null) {
    return;
  }
  doc.add(fact.sentence, Sentence.class);
  OpenFact openFact = new OpenFact(jcas);
  if(fact.factBegin != null) {
    openFact.setBegin(fact.factBegin);
    openFact.setBegin(fact.factEnd);
  }
  Subject subject = new Subject(jcas, fact.subjectBegin, fact.subjectEnd);
  subject.setExplicit(true);
  openFact.setSubject(subject);
  subject.addToIndexes();
  ObjectF object = new ObjectF(jcas, fact.objectBegin, fact.objectEnd);
  object.setExplicit(true);
  openFact.setObject(object);
  object.addToIndexes();
  Relation relation = new Relation(jcas);
  if(fact.relationBegin != null) {
    relation.setBegin(fact.relationBegin);
    relation.setEnd(fact.relationEnd);
  }
  openFact.setRelation(relation);
  relation.addToIndexes();
  openFact.addToIndexes();
  doc.close();
}
 
Example 8
Source File: IntVectorTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testPool() throws Exception {
  try {
    
    AnalysisEngineDescription aed = (AnalysisEngineDescription)UIMAFramework.getXMLParser().parse(
            new XMLInputSource(JUnitExtension
                    .getFile("TextAnalysisEngineImplTest/TestPrimitiveTae1.xml")));
    
    AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(aed);

    // define a caspool of size 2
    CasManager cm = ((UimaContext_ImplBase)ae.getUimaContext()).getResourceManager().getCasManager();
    cm.defineCasPool("uniqueString", 2, null);
    
    CAS c1 = cm.getCas("uniqueString");
    CAS c2 = cm.getCas("uniqueString");
    c1.getJCas();
    
    CAS c1v2 = c1.createView("view2");
    CAS c2v2 = c2.createView("view3");
    c2v2.getJCas();
    
    TypeSystem ts = c1.getTypeSystem();
    
    Assert.assertTrue(ts == c2.getTypeSystem());
    Assert.assertTrue(ts == c1v2.getTypeSystem());
    Assert.assertTrue(ts == c2v2.getTypeSystem());

  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}
 
Example 9
Source File: Conll2003ReaderTcBmeow.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
@Override
public void getNext(CAS cas) throws IOException, CollectionException {
	reader.getNext(cas);

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

	for(Sentence sentence : JCasUtil.select(jcas, Sentence.class)) {
		TextClassificationSequence sequence = new TextClassificationSequence(jcas, sentence.getBegin(),
				sentence.getEnd());
		sequence.addToIndexes();

		for(Token token : JCasUtil.selectCovered(jcas, Token.class, sentence)){
			TextClassificationTarget unit = new TextClassificationTarget(jcas, token.getBegin(), token.getEnd());
			unit.setSuffix(token.getCoveredText());
			unit.addToIndexes();

			TextClassificationOutcome outcome = new TextClassificationOutcome(jcas, token.getBegin(), token.getEnd());
			outcome.setOutcome(getTextClassificationOutcome(jcas, unit));
			outcome.addToIndexes();
		}

	}
}
 
Example 10
Source File: Conll2003ReaderTc.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
@Override
public void getNext(CAS cas) throws IOException, CollectionException {
	reader.getNext(cas);

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

	for(Sentence sentence : JCasUtil.select(jcas, Sentence.class)) {
		TextClassificationSequence sequence = new TextClassificationSequence(jcas, sentence.getBegin(),
				sentence.getEnd());
		sequence.addToIndexes();

		for(Token token : JCasUtil.selectCovered(jcas, Token.class, sentence)){
			TextClassificationTarget unit = new TextClassificationTarget(jcas, token.getBegin(), token.getEnd());
			unit.setSuffix(token.getCoveredText());
			unit.addToIndexes();

			TextClassificationOutcome outcome = new TextClassificationOutcome(jcas, token.getBegin(), token.getEnd());
			outcome.setOutcome(getTextClassificationOutcome(jcas, unit));
			outcome.addToIndexes();
		}

	}
}
 
Example 11
Source File: PredictionsWriter.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
@Override
    public void process(CAS aCAS) throws AnalysisEngineProcessException {

        try {
            JCas jCas = aCAS.getJCas();

            Collection<Sentence> sentences = JCasUtil.select(jCas, Sentence.class);
            for (Sentence sentence : sentences) {
                List<Token> tokens = JCasUtil.selectCovered(jCas, Token.class, sentence);
                Class<? extends Annotation> goldAnnotation;
                Class<? extends Annotation> predAnnotation;
                if (knowNER) {
                    goldAnnotation = NamedEntity.class;
                    predAnnotation = TextClassificationOutcome.class;
                } else {
                    goldAnnotation = TextClassificationOutcome.class;
                    predAnnotation = NamedEntity.class;
                }

                for (Token token : tokens) {

                    String line = getTag(goldAnnotation, jCas, token) + "\t" + getTag(predAnnotation, jCas, token);
                    out.add(line);
//                    verboseOut.add(token.getCoveredText() + "\t" + line);

                }
//                verboseOut.add("\n");
                out.add("\n");
            }
//            System.out.println(out);
        }catch (CASException e) {
            e.printStackTrace();
        }
//        out.flush();
    }
 
Example 12
Source File: StreamingCollectionReader.java    From termsuite-core with Apache License 2.0 5 votes vote down vote up
@Override
public void getNext(CAS cas) throws IOException, CollectionException {
	this.cumulatedLength += currentDoc.getText().length();
	logger.info("[Stream {}] Processing document {}: {} (total length processed: {})", 
			this.streamName,
			this.mCurrentIndex,
			this.currentDoc.getUri(),
			this.cumulatedLength);

	SourceDocumentInformation sdi;
	try {
		
		sdi = new SourceDocumentInformation(cas.getJCas());
		sdi.setUri(currentDoc.getUri());
		cas.setDocumentLanguage(mLanguage.getCode());
		cas.setDocumentText(currentDoc.getText());
		sdi.setDocumentSize(currentDoc.getText().length());
		sdi.setCumulatedDocumentSize(this.cumulatedLength);
		sdi.setBegin(0);
		sdi.setEnd(currentDoc.getText().length());
		sdi.setOffsetInSource(0);
		sdi.setDocumentIndex(mCurrentIndex);
		
		/*
		 * Cannot be known in case of streaming
		 */
		sdi.setCorpusSize(-1);
		sdi.setNbDocuments(-1);
		
		// Cannot know if this is the last
		sdi.setLastSegment(false);
		
		sdi.addToIndexes();
		this.mCurrentIndex++;
	} catch (CASException e) {
		throw new CollectionException(e);
	}
}
 
Example 13
Source File: JsonCasDeserializer.java    From termsuite-core with Apache License 2.0 5 votes vote down vote up
private static void FillTermOccAnnotations(JsonParser parser , JsonToken token, TermOccAnnotation toa, CAS cas) throws IOException, CASException {
    if (token.equals(JsonToken.FIELD_NAME)){
        switch (parser.getCurrentName()){
            case F_PATTERN :
                String[] patternTable = parser.nextTextValue().split(" ");
                StringArray stringArray = new StringArray(cas.getJCas(), patternTable.length);

                for (int i = 0; i < patternTable.length; i++){
                    stringArray.set(i,patternTable[i]);
                }
                toa.setPattern(stringArray);
                break;

            case F_SPOTTING_RULE_NAME :
                toa.setSpottingRuleName(parser.nextTextValue());
                break;
            case F_TERM_KEY :
                toa.setTermKey(parser.nextTextValue());
                break;
            case F_WORDS :
                fillWords(toa,cas);
                break;
            case F_BEGIN :
                toa.setBegin(parser.nextIntValue(0));
                break;
            case F_END :
                toa.setEnd(parser.nextIntValue(0));
                break;
        }
    }
}
 
Example 14
Source File: JCasUtilTest.java    From uima-uimafit with Apache License 2.0 4 votes vote down vote up
@Test
  public void testSelectBetweenRandom() throws Exception {
    final int ITERATIONS = 10;

    Random rnd = new Random();

    for (int i = 1; i <= ITERATIONS; i++) {
      CAS cas = jCas.getCas();
      initRandomCas(cas, 10 * i);

      JCas jcas = cas.getJCas();
      List<Token> tokens = new ArrayList<Token>(select(jcas, Token.class));

//      long timeNaive = 0;
//      long timeOptimized = 0;
      for (int j = 0; j < ITERATIONS; j++) {
        Token t1 = tokens.get(rnd.nextInt(tokens.size()));
        Token t2 = tokens.get(rnd.nextInt(tokens.size()));

        int left = Math.min(t1.getEnd(), t2.getEnd());
        int right = Math.max(t1.getBegin(), t2.getBegin());

//        long ti;
        List<Sentence> reference;
        if ((t1.getBegin() < t2.getBegin() && t2.getBegin() < t1.getEnd())
                || (t1.getBegin() < t2.getEnd() && t2.getEnd() < t1.getEnd())
                || (t2.getBegin() < t1.getBegin() && t1.getBegin() < t2.getEnd())
                || (t2.getBegin() < t1.getEnd() && t1.getEnd() < t2.getEnd())) {
          // If the boundary annotations overlap, the result must be empty
//          ti = System.currentTimeMillis();
          reference = new ArrayList<Sentence>();
//          timeNaive += System.currentTimeMillis() - ti;
        } else {
//          ti = System.currentTimeMillis();
          reference = selectCovered(jcas, Sentence.class, left, right);
//          timeNaive += System.currentTimeMillis() - ti;
        }

//        ti = System.currentTimeMillis();
        List<Sentence> actual = selectBetween(Sentence.class, t1, t2);
//        timeOptimized += System.currentTimeMillis() - ti;

        assertEquals("Naive: Searching between " + t1 + " and " + t2, reference, actual);
      }

//      System.out.format("Speed up factor %.2f [naive:%d optimized:%d diff:%d]\n",
//              (double) timeNaive / (double) timeOptimized, timeNaive, timeOptimized, timeNaive
//                      - timeOptimized);
    }
  }
 
Example 15
Source File: JCasUtilv3Test.java    From uima-uimafit with Apache License 2.0 4 votes vote down vote up
@Test
public void testSelectBetweenRandom() throws Exception {
  final int ITERATIONS = 10;

  Random rnd = new Random();

  for (int i = 1; i <= ITERATIONS; i++) {
    CAS cas = jCas.getCas();
    initRandomCas(cas, 10 * i);

    JCas jcas = cas.getJCas();
    List<Token> tokens = new ArrayList<Token>(select(jcas, Token.class));

    long timeNaive = 0;
    long timeOptimized = 0;
    for (int j = 0; j < ITERATIONS; j++) {
      Token t1 = tokens.get(rnd.nextInt(tokens.size()));
      Token t2 = tokens.get(rnd.nextInt(tokens.size()));

      int left = Math.min(t1.getEnd(), t2.getEnd());
      int right = Math.max(t1.getBegin(), t2.getBegin());

      long ti;
      List<Sentence> reference;
      if ((t1.getBegin() < t2.getBegin() && t2.getBegin() < t1.getEnd())
              || (t1.getBegin() < t2.getEnd() && t2.getEnd() < t1.getEnd())
              || (t2.getBegin() < t1.getBegin() && t1.getBegin() < t2.getEnd())
              || (t2.getBegin() < t1.getEnd() && t1.getEnd() < t2.getEnd())) {
        // If the boundary annotations overlap, the result must be empty
        ti = System.currentTimeMillis();
        reference = new ArrayList<Sentence>();
        timeNaive += System.currentTimeMillis() - ti;
      } else {
        ti = System.currentTimeMillis();
        reference = selectCovered(jcas, Sentence.class, left, right);
        timeNaive += System.currentTimeMillis() - ti;
      }

      ti = System.currentTimeMillis();
      // uimaFIT: selectBetween(Sentence.class, t1, t2);
      List<Sentence> actual = jcas.select(Sentence.class).between(t1, t2).asList();
      timeOptimized += System.currentTimeMillis() - ti;

      assertEquals("Naive: Searching between " + t1 + " and " + t2, reference, actual);
    }

    System.out.format("Speed up factor %.2f [naive:%d optimized:%d diff:%d]\n",
            (double) timeNaive / (double) timeOptimized, timeNaive, timeOptimized, timeNaive
                    - timeOptimized);
  }
}
 
Example 16
Source File: JCasUtilv3Test.java    From uima-uimafit with Apache License 2.0 4 votes vote down vote up
@Test
public void testSelectCoverRandom() throws Exception {
  final int ITERATIONS = 10;

  for (int i = 0; i < ITERATIONS; i++) {
    CAS cas = jCas.getCas();
    initRandomCas(cas, 10 * i);

    JCas jcas = cas.getJCas();
    Collection<Sentence> sentences = select(jcas, Sentence.class);
    
    long timeNaive = 0;
    long timeOptimized = 0;
    
    // Prepare the index
    long timeIndexed = System.currentTimeMillis();
    Map<Sentence, List<Token>> index = indexCovered(jcas, Sentence.class, Token.class);
    timeIndexed = System.currentTimeMillis() - timeIndexed;
    
    // -- The order of entries in the index is NOT defined!
    // Check that order of indexed sentences corresponds to regular CAS-index order
    // List<Sentence> relevantSentences = new ArrayList<>(sentences);
    // relevantSentences.retainAll(index.keySet());
    // assertEquals(relevantSentences, new ArrayList<>(index.keySet()));
    
    for (Sentence t : sentences) {
      long ti = System.currentTimeMillis();
      // The naive approach is assumed to be correct
      // uimaFIT: selectCovered(jcas, Token.class, t.getBegin(), t.getEnd());
      List<Token> expected = jcas.select(Token.class).coveredBy(t.getBegin(), t.getEnd()).asList();
      timeNaive += System.currentTimeMillis() - ti;

      // Record time for optimized selectCovered
      ti = System.currentTimeMillis();
      // uimaFIT: selectCovered(jcas, Token.class, t);
      List<Token> actual1 = jcas.select(Token.class).coveredBy(t).asList();
      timeOptimized += System.currentTimeMillis() - ti;

      // Record index lookup time
      ti = System.currentTimeMillis();
      Collection<Token> actual2 = index.get(t);
      timeIndexed += System.currentTimeMillis() - ti;

      check(jcas, t, expected, actual1);
      check(jcas, t, expected, actual2);
      
      // System.out.printf("%n--- OK ---------------%n%n");
    }
    System.out.printf(
            "%3d Optimized: speed up factor %3.2f [naive:%4d optimized:%4d (diff:%4d)]%n", i,
            (double) timeNaive / (double) timeOptimized, timeNaive, timeOptimized,
            timeNaive - timeOptimized);
    System.out.printf(
            "%3d Indexed:   speed up factor %3.2f [naive:%4d indexed  :%4d (diff:%4d)]%n%n", i,
            (double) timeNaive / (double) timeIndexed, timeNaive, timeIndexed,
            timeNaive - timeIndexed);
  }
}
 
Example 17
Source File: CasToInlineXmlTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testCasToInlineXml() throws Exception {
    // Jira https://issues.apache.org/jira/browse/UIMA-2406
    
    File typeSystemFile1 = JUnitExtension.getFile("ExampleCas/testTypeSystem_arrays.xml");
    File indexesFile = JUnitExtension.getFile("ExampleCas/testIndexes_arrays.xml");

    TypeSystemDescription typeSystem = UIMAFramework.getXMLParser().parseTypeSystemDescription(
            new XMLInputSource(typeSystemFile1));
    FsIndexDescription[] indexes = UIMAFramework.getXMLParser().parseFsIndexCollection(new XMLInputSource(indexesFile))
            .getFsIndexes();

    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);
    f.setF1Shorts(a);
    f.addToIndexes();
    
    OfStrings ss = new OfStrings(jcas);
    StringArray sa = new StringArray(jcas, 3);
    sa.set(0, "0s");
    sa.set(1, "1s");
    sa.set(2, "2s");
    ss.setF1Strings(sa);
    ss.addToIndexes();
    
    CasToInlineXml c2x = new CasToInlineXml();
    String result = c2x.generateXML(srcCas).trim();
    System.out.println(result);
    int s = result.indexOf("<Document>");
    result = result.substring(s);
    result = canonicalizeNl(result);
    
    // Java 9 xml formatter formats the text with a new line and indentations
    String expected = "<Document>\n" +
        IND+"<uima.tcas.DocumentAnnotation sofa=\"Sofa\" begin=\"0\" end=\"17\" language=\"x-unspecified\">\n" +
        IND+IND+"<org.apache.uima.testTypeSystem_arrays.OfStrings sofa=\"Sofa\" begin=\"0\" end=\"0\" f1Strings=\"[0s,1s,2s]\"/>\n"  +
        IND+IND+"<org.apache.uima.testTypeSystem_arrays.OfShorts sofa=\"Sofa\" begin=\"0\" end=\"0\" f1Shorts=\"[0,1,2]\"/>"
        		+ (Misc.isJava9ea() ? "\n        " : "") + 
        		"1 2 3 4 5 6 7 8 9"
        		+ (Misc.isJava9ea() ? "\n    " : "") + "</uima.tcas.DocumentAnnotation>\n" +
        "</Document>";
    for (int i = 0; i < result.length(); i++ ) {
      if (result.charAt(i) != expected.charAt(i)) {
        System.out.format("Unequal compare at position %,d, char code result = %d, expected = %d%n", i, (int)result.charAt(i), (int)expected.charAt(i));
        break;
      }
    }
    boolean whitespaceFlag = XMLUnit.getIgnoreWhitespace();
    XMLUnit.setIgnoreWhitespace(true);
    try {
      XMLAssert.assertXMLEqual(expected, result);
    }
    finally {
      XMLUnit.setIgnoreWhitespace(whitespaceFlag);
    }
//    assertEquals(expected, result.trim());
  }
 
Example 18
Source File: JSONReader.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
@Override
public void getNext(CAS cas) throws IOException, CollectionException {
  Resource res = nextFile();
  String name = res.getPath();
  JsonNode json = objectMapper.readTree(res.getInputStream());

  String title = null;
  if(titleKey != null) {
    title = json.get(titleKey).asText();
  }
  String content = json.get(contentKey).asText();

  StringBuilder sb = new StringBuilder();
  if (title != null) {
    sb.append(title).append(System.lineSeparator()).append(System.lineSeparator());
  } else {
    logger.debug(res.getPath() + " does not have a title field.");
  }

  if (content != null) {
    sb.append(content);
  } else {
    logger.debug(res.getPath() + " does not have a content field.");
  }

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

  // Set doc id.
  String id = null;
  if(idKey != null) {
    id = json.get(idKey).asText();
  }
  if(id == null) {
    id = name;
  }
  DocumentMetaData dmd = new DocumentMetaData(jcas);
  dmd.addToIndexes();
  dmd.setDocumentId(id);
  jcas.setDocumentLanguage(getLanguage());
  jcas.setDocumentText(sb.toString());
}
 
Example 19
Source File: JCasUtil.java    From uima-uimafit with Apache License 2.0 3 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 jcas
 *          a JCas
 * @param viewName
 *          the requested view.
 * @param create
 *          the view is created if it does not exist.
 * @return the requested view
 * @throws IllegalStateException
 *           if the JCas wrapper cannot be obtained.
 */
public static JCas getView(JCas jcas, String viewName, boolean create) {
  try {
    CAS view = CasUtil.getView(jcas.getCas(), viewName, create);
    return view != null ? view.getJCas() : null;
  } catch (CASException e) {
    throw new IllegalStateException(e);
  }
}
 
Example 20
Source File: JCasUtil.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 jcas
 *          a JCas
 * @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.
 * @throws IllegalStateException
 *           if the JCas wrapper cannot be obtained.
 */
public static JCas getView(JCas jcas, String viewName, JCas fallback) {
  try {
    CAS fallbackCas = fallback != null ? fallback.getCas() : null;
    CAS view = CasUtil.getView(jcas.getCas(), viewName, fallbackCas);
    return view != null ? view.getJCas() : null;
  } catch (CASException e) {
    throw new IllegalStateException(e);
  }
}