Java Code Examples for org.apache.uima.jcas.cas.StringArray#set()

The following examples show how to use org.apache.uima.jcas.cas.StringArray#set() . 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: NewFeatureUtilsTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testStringArray() {
  Buzzword bw = new Buzzword(jCas);
  StringArray tags = new StringArray(jCas, 2);
  tags.set(0, "tag1");
  tags.set(1, "tag2");
  bw.setTags(tags);
  bw.addToIndexes();

  Feature f = bw.getType().getFeatureByBaseName("tags");
  StringArray newTags = new StringArray(jCas, 2);
  newTags.set(0, "first");
  newTags.set(1, "second");
  NewFeatureUtils.setPrimitiveArray(jCas, bw, f, Arrays.asList(newTags.toArray()));

  assertEquals("first", bw.getTags(0));
  assertEquals("second", bw.getTags(1));
}
 
Example 2
Source File: FeatureUtilsTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testStringArray() {
  DocumentAnnotation da = (DocumentAnnotation) jCas.getDocumentAnnotationFs();
  StringArray rel = new StringArray(jCas, 3);
  rel.set(0, "ENG");
  rel.set(1, "WAL");
  rel.set(2, "SCO");
  da.setDocumentReleasability(rel);

  Feature f = da.getType().getFeatureByBaseName(DOCUMENT_RELEASABILITY);

  Object[] o = FeatureUtils.featureToArray(f, da);
  assertEquals(3, o.length);
  assertTrue(o[0] instanceof String);
  assertEquals("ENG", (String) o[0]);
  assertTrue(o[1] instanceof String);
  assertEquals("WAL", (String) o[1]);
  assertTrue(o[2] instanceof String);
  assertEquals("SCO", (String) o[2]);
}
 
Example 3
Source File: FeatureUtilsTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testStringArrayToObject() {
  DocumentAnnotation da = (DocumentAnnotation) jCas.getDocumentAnnotationFs();
  StringArray rel = new StringArray(jCas, 3);
  rel.set(0, "true");
  rel.set(1, "2");
  rel.set(2, "0.45");
  da.setDocumentReleasability(rel);

  Feature f = da.getType().getFeatureByBaseName(DOCUMENT_RELEASABILITY);

  Object[] o = FeatureUtils.featureToArray(f, da);
  assertEquals(3, o.length);
  assertTrue(o[0] instanceof Boolean);
  assertTrue((Boolean) o[0]);
  assertTrue(o[1] instanceof Integer);
  assertEquals(new Integer(2), (Integer) o[1]);
  assertTrue(o[2] instanceof Double);
  assertEquals(new Double(0.45), (Double) o[2]);
}
 
Example 4
Source File: FeatureUtilsTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testStringArrayToList() {
  DocumentAnnotation da = (DocumentAnnotation) jCas.getDocumentAnnotationFs();
  StringArray rel = new StringArray(jCas, 3);
  rel.set(0, "ENG");
  rel.set(1, "WAL");
  rel.set(2, "SCO");
  da.setDocumentReleasability(rel);

  Feature f = da.getType().getFeatureByBaseName(DOCUMENT_RELEASABILITY);

  List<Object> o = FeatureUtils.featureToList(f, da);
  assertEquals(3, o.size());
  assertTrue(o.get(0) instanceof String);
  assertEquals("ENG", (String) o.get(0));
  assertTrue(o.get(1) instanceof String);
  assertEquals("WAL", (String) o.get(1));
  assertTrue(o.get(2) instanceof String);
  assertEquals("SCO", (String) o.get(2));
}
 
Example 5
Source File: MongoFieldMapping.java    From bluima with Apache License 2.0 6 votes vote down vote up
public static void readFieldFromDb(String fieldKey, String range,
        Annotation a, Feature f, BasicDBObject dbO, JCas jCas) {

    if (dbO.containsField(fieldKey)) {

        if (range.equals("String")) {
            a.setStringValue(f, dbO.getString(fieldKey));
        } else if (range.equals("StringArray")) {
            BasicDBList vals = (BasicDBList) dbO.get(fieldKey);
            StringArray sa = new StringArray(jCas, vals.size());
            for (int i = 0; i < vals.size(); i++) {
                sa.set(i, vals.get(i).toString());
            }
            a.setFeatureValue(f, sa);
        } else if (range.equals("Integer")) {
            a.setIntValue(f, dbO.getInt(fieldKey));
        } else if (range.equals("Float")) {
            a.setFloatValue(f, (float) dbO.getDouble(fieldKey));
        } else if (range.equals("Boolean")) {
            a.setBooleanValue(f, dbO.getBoolean(fieldKey));
        } else {
            LOG.warn("range not supported " + range);
        }
    }
}
 
Example 6
Source File: AbstractAhoCorasickAnnotator.java    From baleen with Apache License 2.0 5 votes vote down vote up
private StringArray listToStringArray(JCas jCas, List<Object> l) {
  StringArray sa = new StringArray(jCas, l.size());

  int index = 0;
  for (Object o : l) {
    sa.set(index, o.toString());
    index++;
  }

  return sa;
}
 
Example 7
Source File: FeatureUtilsTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testNull() {
  DocumentAnnotation da = (DocumentAnnotation) jCas.getDocumentAnnotationFs();
  StringArray rel = new StringArray(jCas, 3);
  rel.set(0, "ENG");
  rel.set(1, "WAL");
  rel.set(2, "SCO");
  da.setDocumentReleasability(rel);

  Feature f = da.getType().getFeatureByBaseName(DOCUMENT_RELEASABILITY);

  Object o = FeatureUtils.featureToObject(f, da);
  assertNull(o);
}
 
Example 8
Source File: UimaTypesUtilsTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testUimaToJava() {
  StringArray sa = new StringArray(jCas, 3);
  sa.set(0, "Foo");
  sa.set(1, "Bar");
  sa.set(2, "Baz");

  String[] s = UimaTypesUtils.toArray(sa);
  assertEquals(3, s.length);
  assertEquals("Foo", s[0]);
  assertEquals("Bar", s[1]);
  assertEquals("Baz", s[2]);
}
 
Example 9
Source File: NewFeatureUtils.java    From baleen with Apache License 2.0 5 votes vote down vote up
private static StringArray getStringArray(JCas jCas, Collection<?> list) {
  final StringArray a = new StringArray(jCas, list.size());
  int i = 0;
  for (final Object s : list) {
    a.set(i++, s.toString());
  }
  return a;
}
 
Example 10
Source File: GremlinConsumerTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
  jCas.setDocumentText("Hello James! Is your e-mail address [email protected]? 'No', said James.");

  DocumentAnnotation da = getDocumentAnnotation(jCas);
  da.setDocType("test");
  da.setSourceUri("http://www.example.com/hello.txt");

  StringArray sa = new StringArray(jCas, 2);
  sa.set(0, "UK");
  sa.set(1, "US");
  da.setDocumentCaveats(sa);

  ReferenceTarget rt = new ReferenceTarget(jCas);
  rt.addToIndexes();

  Entity e1 = new Person(jCas, 6, 11);
  e1.setValue("James");
  e1.setReferent(rt);
  e1.addToIndexes();

  Entity e1a = new Person(jCas, 60, 65);
  e1a.setValue("James");
  e1a.setReferent(rt);
  e1a.addToIndexes();

  Entity e2 = new CommsIdentifier(jCas, 36, 47);
  e2.setValue("[email protected]");
  e2.addToIndexes();

  processJCas(GremlinConsumer.PARAM_GRAPH_CONFIG, tmpConfig.getPath());

  // TODO: Write some proper tests that actually check something
}
 
Example 11
Source File: RegexSpotter.java    From termsuite-core with Apache License 2.0 5 votes vote down vote up
private void addOccurrenceToCas(JCas jCas, RegexOccurrence occurrence) {
	TermOccAnnotation annotation = (TermOccAnnotation) jCas
			.getCas().createAnnotation(
					jCas.getCasType(TermOccAnnotation.type),
					occurrence.getBegin(),
					occurrence.getEnd());
	
	StringArray patternFeature = new StringArray(jCas, occurrence.size());
	FSArray innerWords = new FSArray(jCas, occurrence.size());
	StringBuilder termLemma = new StringBuilder();
	int i = 0;
	for (LabelledAnnotation la:occurrence.getLabelledAnnotations()) {
		patternFeature.set(i, la.getLabel());
		WordAnnotation word = (WordAnnotation) la.getAnnotation();
		termLemma.append(word.getLemma());
		if(i<occurrence.size()-1)
			termLemma.append(TermSuiteConstants.WHITESPACE);
		WordAnnotation wordAnno = (WordAnnotation) la.getAnnotation();
		if(wordAnno.getRegexLabel() != null) {
			if(!wordAnno.getRegexLabel().equals(la.getLabel())) {
				LOGGER.warn("Another label has already been set for WordAnnotation "+wordAnno.getCoveredText()+":"+wordAnno.getRegexLabel()+" ["+wordAnno.getBegin()+","+wordAnno.getEnd()+"]. Ignoring the new label "+la.getLabel()+" (rule: "+occurrence.getRule().getName()+")");
			}
		} else
			wordAnno.setRegexLabel(la.getLabel());
		innerWords.set(i, wordAnno);
		i++;
	}
	
	annotation.setWords(innerWords);
	annotation.setPattern(patternFeature);
	annotation.setSpottingRuleName(occurrence.getRule().getName());
	annotation.setTermKey(TermSuiteUtils.getGroupingKey(annotation));
	annotation.addToIndexes();
}
 
Example 12
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 13
Source File: ExtractCoocurrences.java    From bluima with Apache License 2.0 5 votes vote down vote up
static StringArray convertToStringArray(JCas cas, String[] nativeStrArray) {

        int arrayLength = nativeStrArray.length;

        StringArray strArray = new StringArray(cas, arrayLength);
        for (int i = 0; i < arrayLength; i++) {
            strArray.set(i, nativeStrArray[i]);
        }
        return strArray;
    }
 
Example 14
Source File: DocumentAnnotationTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testToString() throws InvalidXMLException, IOException, ResourceInitializationException, CASException {
  File typeSystemFile = JUnitExtension.getFile("ExampleCas/testTypeSystem_docmetadata.xml");
  TypeSystemDescription typeSystem = UIMAFramework.getXMLParser().parseTypeSystemDescription(
          new XMLInputSource(typeSystemFile));
  
  source = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), null);
  jcas = source.getJCas();
  
  DocMeta d = new DocMeta(jcas);
  d.setFeat("a string");
  d.setFeat2("b string");
  d.setFeat3("c string");
  
  FSArray fsa = new FSArray(jcas, 2);
  fsa.set(0, new Annotation(jcas, 1,2));
  fsa.set(1, new Annotation(jcas, 3,4));
  d.setArrayFs(fsa);
  
  IntegerArray intarr = new IntegerArray(jcas, 2);
  intarr.set(0,  10);
  intarr.set(1,  -10);
  d.setArrayints(intarr);
  
  StringArray strarr = new StringArray(jcas, 2);
  strarr.set(0,  "first");
  strarr.set(1,  "second");
  d.setArraystr(strarr);
  
  System.out.println(d.toString());
}
 
Example 15
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 16
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
@Override
public void ll_setStringArrayValue(int fsRef, int position, String value) {
  StringArray array = getFsFromId_checked(fsRef);
  array.set(position,  value);  // that set operation does required journaling
}
 
Example 17
Source File: MongoTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testEntities() throws Exception {
  jCas.setDocumentText(
      "James went to London on 19th February 2015. His e-mail address is [email protected]");

  Person p = Annotations.createPerson(jCas, 0, 5, PERSON);
  Location l =
      Annotations.createLocation(
          jCas, 14, 20, LONDON, "{\"type\": \"Point\", \"coordinates\": [-0.1, 51.5]}");

  Temporal dt = Annotations.createTemporal(jCas, 24, 42, DATE);
  dt.setConfidence(1.0);

  CommsIdentifier ci = new CommsIdentifier(jCas);
  ci.setBegin(66);
  ci.setEnd(83);
  ci.setSubType("email");
  ci.setValue(EMAIL);
  ci.addToIndexes();

  Buzzword bw = new Buzzword(jCas);
  bw.setBegin(6);
  bw.setEnd(10);
  bw.setValue(WENT);

  StringArray tags = new StringArray(jCas, 2);
  tags.set(0, "verb");
  tags.set(1, "past");

  bw.setTags(tags);
  bw.addToIndexes();

  ae.process(jCas);

  assertEquals(1, documents.count());
  assertEquals(5, entities.count());

  Document a = entities.find(new Document(Mongo.FIELD_ENTITIES + "." + VALUE, PERSON)).first();
  Document person = ((List<Document>) a.get(Mongo.FIELD_ENTITIES)).get(0);
  assertEquals(getExpectedSize(p), person.size());
  assertEquals(0, person.get(BEGIN));
  assertEquals(5, person.get(END));
  assertEquals(0.0, person.get(CONFIDENCE));
  assertEquals(Person.class.getSimpleName(), person.get(TYPE));
  assertEquals(PERSON, person.get(VALUE));

  Document b = entities.find(new Document(Mongo.FIELD_ENTITIES + "." + VALUE, LONDON)).first();
  Document location = ((List<Document>) b.get(Mongo.FIELD_ENTITIES)).get(0);
  assertEquals(getExpectedSize(l), location.size());
  assertEquals(14, location.get(BEGIN));
  assertEquals(20, location.get(END));
  assertEquals(0.0, location.get(CONFIDENCE));
  assertEquals(Location.class.getSimpleName(), location.get(TYPE));
  assertEquals(LONDON, location.get(VALUE));

  assertEquals("Point", ((Document) location.get("geoJson")).get(TYPE));
  assertEquals(
      Arrays.asList(new Double(-0.1), new Double(51.5)),
      ((Document) location.get("geoJson")).get("coordinates"));

  Document c = entities.find(new Document(Mongo.FIELD_ENTITIES + "." + VALUE, DATE)).first();
  Document date = ((List<Document>) c.get(Mongo.FIELD_ENTITIES)).get(0);
  assertEquals(getExpectedSize(dt), date.size());
  assertEquals(24, date.get(BEGIN));
  assertEquals(42, date.get(END));
  assertEquals(1.0, date.get(CONFIDENCE));
  assertEquals(Temporal.class.getSimpleName(), date.get(TYPE));
  assertEquals(DATE, date.get(VALUE));

  Document d = entities.find(new Document(Mongo.FIELD_ENTITIES + "." + VALUE, EMAIL)).first();
  Document email = ((List<Document>) d.get(Mongo.FIELD_ENTITIES)).get(0);
  assertEquals(getExpectedSize(ci), email.size());
  assertEquals(66, email.get(BEGIN));
  assertEquals(83, email.get(END));
  assertEquals(0.0, email.get(CONFIDENCE));
  assertEquals(CommsIdentifier.class.getSimpleName(), email.get(TYPE));
  assertEquals("email", email.get("subType"));
  assertEquals(EMAIL, email.get(VALUE));

  Document e = entities.find(new Document(Mongo.FIELD_ENTITIES + "." + VALUE, WENT)).first();
  Document went = ((List<Document>) e.get(Mongo.FIELD_ENTITIES)).get(0);
  assertEquals(getExpectedSize(bw), went.size());
  assertEquals(6, went.get(BEGIN));
  assertEquals(10, went.get(END));
  assertEquals(0.0, went.get(CONFIDENCE));
  List<String> wentTags = (List<String>) went.get("tags");
  assertEquals("verb", wentTags.get(0));
  assertEquals("past", wentTags.get(1));
  assertEquals(WENT, went.get(VALUE));
}