org.apache.uima.jcas.cas.StringArray Java Examples

The following examples show how to use org.apache.uima.jcas.cas.StringArray. 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: XmiCompare.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
private void fixupTermMentionTypesUnknown(CASImpl cas) {
  TypeSystem ts = cas.getTypeSystem();
  Type type = ts.getType("com.ibm.watsonx.nlp_di.cas_term.Term");
  Feature f_mentionTypes = type.getFeatureByBaseName("mentionTypes");
  cas.select(type)
     .allViews()
     .map(fs -> (StringArray) fs.getFeatureValue(f_mentionTypes))
     .filter(fs -> fs != null && fs.contains("UNKNOWN"))
     .forEach(fs -> 
       {
         for (int i = 0; i < fs.size(); i++) {
           if ("UNKNOWN".equals(fs.get(i))) {
             fs.set(i, "CATEGORY");
           }
         }
       });
}
 
Example #2
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 #3
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 #4
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 #5
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public TOP createArray(TypeImpl array_type, int arrayLength) {
    TypeImpl_array tia = (TypeImpl_array) array_type;
    TypeImpl componentType = tia.getComponentType();
    if (componentType.isPrimitive()) {
      checkArrayPreconditions(arrayLength);
      switch (componentType.getCode()) {
      case intTypeCode: return new IntegerArray(array_type, this, arrayLength);
      case floatTypeCode: return new FloatArray(array_type, this, arrayLength);
      case booleanTypeCode: return new BooleanArray(array_type, this, arrayLength);
      case byteTypeCode: return new ByteArray(array_type, this, arrayLength);
      case shortTypeCode: return new ShortArray(array_type, this, arrayLength);
      case longTypeCode: return new LongArray(array_type, this, arrayLength);
      case doubleTypeCode: return new DoubleArray(array_type, this, arrayLength);
      case stringTypeCode: return new StringArray(array_type, this, arrayLength);
      default: throw Misc.internalError();
      }
    }
//    return (TOP) createArrayFS(/* array_type, */ arrayLength);  // for backwards compat w/ v2, don't create typed arrays
    if (IS_DISABLE_SUBTYPE_FSARRAY_CREATION) {
      return (TOP) createArrayFS(arrayLength);
    } else {
      return (TOP) createArrayFS(array_type, arrayLength);
    }
  }
 
Example #6
Source File: BinaryCasSerDes4.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * For delta, for each fsChange element, extract any strings
 * @param fsChange
 */
private void extractStringsFromModifications(FsChange fsChange) {
  final TOP fs = fsChange.fs;
  final TypeImpl type = fs._getTypeImpl();
  if (fsChange.arrayUpdates != null) {
    if (type.getComponentSlotKind() == SlotKind.Slot_StrRef) {
      String[] sa = ((StringArray)fs)._getTheArray();  
      fsChange.arrayUpdates.forAllInts(index -> {
        os.add(sa[index]);
      });
    } // end of is string array
  } else { // end of is array
    BitSet fm = fsChange.featuresModified;
    for (int offset = fm.nextSetBit(0); offset >= 0; offset = fm.nextSetBit(offset + 1)) {
      FeatureImpl feat = type.getFeatureImpls()[offset];
      if (feat.getSlotKind() == SlotKind.Slot_StrRef) {
        os.add(fs._getStringValueNc(feat));
      }
    } // end of iter over features
  } // end of is-not-array
}
 
Example #7
Source File: BlueCasUtil.java    From bluima with Apache License 2.0 5 votes vote down vote up
public static List<String> toList(StringArray tokens) {
    List<String> l = new ArrayList<String>(tokens.size());
    for (int i = 0; i < tokens.size(); i++) {
        l.add(tokens.get(i));
    }
    return l;
}
 
Example #8
Source File: XmiCompare.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
void canonicalizeStringFirstVariant(String t, String f, String v, CASImpl cas) {
  TypeSystem ts = cas.getTypeSystem();
  Type type = ts.getType(t);
  Feature feat = ts.getFeatureByFullName(t + ":" + f);
  Feature featv = ts.getFeatureByFullName(t + ":" + v);  // v is the variant array
  cas.select(type).allViews().forEach(fs ->
    { StringArray sa = (StringArray) fs.getFeatureValue(featv);
      if (sa != null && sa.size() > 2) {
        String item = fs.getStringValue(feat);
        if (sa.contains(item)) {
          fs.setStringValue(feat, sa.get(0));
        }
      }
    });
}
 
Example #9
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
@Override
public StringArray emptyStringArray() {
    if (null == svd.emptyStringArray) {
      svd.emptyStringArray = new StringArray(this.getJCas(), 0);
    }
    return svd.emptyStringArray;
  }
 
Example #10
Source File: JCasTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testStringArrayAPI() {
  StringArray sa = new StringArray(jcas, 3);
  String[] values = {"1", "2", "3"}; 
  sa.copyFromArray(values, 0, 0, 3);
  
   int i = 0;
   for (String s : sa) {
     assert(s.equals(values[i++]));
   }
}
 
Example #11
Source File: AllAnnotationsJsonConsumerTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws IOException {
  jCas.setDocumentText(TEXT);
  tempDirectory = Files.createTempDirectory(AllAnnotationsJsonConsumerTest.class.getSimpleName());
  tempDirectory.toFile().deleteOnExit();

  DocumentAnnotation documentAnnotation = (DocumentAnnotation) jCas.getDocumentAnnotationFs();
  documentAnnotation.setSourceUri(SOURCEURI);

  Paragraph paragraph1 = new Paragraph(jCas);
  paragraph1.setBegin(0);
  paragraph1.setDepth(1);
  paragraph1.setEnd(52);
  paragraph1.addToIndexes();

  Person entity1 = new Person(jCas);
  entity1.setBegin(70);
  entity1.setEnd(73);
  entity1.setValue("cat");
  entity1.addToIndexes();

  Event event = new Event(jCas);
  event.setBegin(53);
  event.setEnd(105);
  event.setArguments(new StringArray(jCas, 2));
  event.setArguments(0, "cat");
  event.setArguments(1, "dog");
  event.setEntities(new FSArray(jCas, 1));
  event.setEntities(0, entity1);
  event.addToIndexes();
}
 
Example #12
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 #13
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 #14
Source File: Postgres.java    From baleen with Apache License 2.0 5 votes vote down vote up
private Array createVarcharArray(Connection conn, StringArray s) throws SQLException {
  if (s == null) {
    return conn.createArrayOf(VARCHAR, new String[] {});
  } else {
    return conn.createArrayOf(VARCHAR, s.toArray());
  }
}
 
Example #15
Source File: UimaTypesUtilsTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testNulls() {
  String[] javaSa = UimaTypesUtils.toArray(null);
  assertEquals(0, javaSa.length);

  StringArray uimaSa = UimaTypesUtils.toArray(jCas, null);
  assertEquals(0, uimaSa.size());

  uimaSa = UimaTypesUtils.toArray(jCas, Collections.emptyList());
  assertEquals(0, uimaSa.size());
}
 
Example #16
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 #17
Source File: AbstractAhoCorasickAnnotator.java    From baleen with Apache License 2.0 5 votes vote down vote up
private void setPropertyArray(BaleenAnnotation entity, Method method, List<Object> obj) {
  if (StringArray.class.isAssignableFrom(method.getParameterTypes()[0])) {
    try {
      StringArray sa = listToStringArray(entity.getCAS().getJCas(), obj);
      method.invoke(entity, sa);
    } catch (Exception e) {
      getMonitor().error(ERROR_CANT_ASSIGN_ENTITY_PROPERTY, e);
    }
  } else {
    getMonitor()
        .error(
            "Unsupported array type {} - property will be skipped",
            method.getParameterTypes()[0].getName());
  }
}
 
Example #18
Source File: MongoTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuzzwordProperty() throws Exception {
  jCas.setDocumentText("Hello Sydney (Australia), this is a test");
  processJCas(MONGO, erd, COLLECTION, MONGO_COLL, TYPE, "Buzzword");

  assertEquals(1, JCasUtil.select(jCas, Buzzword.class).size());
  Buzzword b = JCasUtil.selectByIndex(jCas, Buzzword.class, 0);
  assertEquals("Sydney (Australia", b.getValue());
  assertEquals("Sydney (Australia", b.getCoveredText());

  StringArray tags = b.getTags();
  assertEquals(1, tags.size());
  assertEquals("broken_regex", tags.get(0));
}
 
Example #19
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 #20
Source File: UimaTypesUtils.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new List from a StringArray
 *
 * @param array the string array
 * @return the list (non-null)
 */
public static List<String> toList(StringArray array) {
  if (array == null) {
    return Collections.emptyList();
  } else {
    return Arrays.asList(array.toArray());
  }
}
 
Example #21
Source File: BinaryCasSerDes6.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Add all the strings ref'd by this FS.
 *   - if it is a string array, do all the array items
 *   - else scan the features and do all string-valued features, in feature offset order
 * For delta, this isn't done here - another routine driven by FsChange info does this.
 */
private void addStringsFromFS(TOP fs) {
  if (fs instanceof StringArray) {
    for (String s : ((StringArray)fs)._getTheArray()) {
      os.add(s);
    }
    return;
  }
  
  for (FeatureImpl fi : fs._getTypeImpl().getFeatureImpls()) {
    if (fi.getRange() instanceof TypeImpl_string) {
      os.add(fs._getStringValueNc(fi));
    }
  }
}
 
Example #22
Source File: PdfCollectionReader.java    From bluima with Apache License 2.0 5 votes vote down vote up
static void extractTables(TableExtractor extractor, File f, JCas jcas) {
    try {
        ArrayList<Table> tables = extractor.extract(f, null);
        if (tables != null) {
            for (Table table : tables) {

                DataTable dt = new DataTable(jcas);
                dt.setTableId(table.getOrder());
                dt.setPageNumber(table.getPageNumber());
                // dt.setRowCount(table.getRowNumber());
                dt.setRowCount(table.getTableBody().size());
                dt.setColumnCount(table.getColumnNumber());
                dt.setCaption(table.getCaption());
                dt.setReferenceText(table.getRefTextList());

                ArrayList<String> tHeadings = table.getHeading();
                dt.setHeadings(new StringArray(jcas, tHeadings.size()));
                for (int i = 0; i < tHeadings.size(); i++) {
                    dt.setHeadings(i, tHeadings.get(i));
                }

                ArrayList<String> tBody = table.getTableBody();
                dt.setBody(new StringArray(jcas, tBody.size()));
                for (int i = 0; i < tBody.size(); i++) {
                    dt.setBody(i, tBody.get(i));
                }
                dt.addToIndexes();
            }
        }
    } catch (Throwable t) {
        LOG.warn("cannot extract tables from {}: {}", f.getAbsolutePath(),
                t);
    }
}
 
Example #23
Source File: NYTCollectionReader.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
private static StringArray toStringArray(List<String> stringList, JCas aJCas) {
    if (!stringList.isEmpty()) {
        String[] strings = stringList.toArray(new String[0]);
        int length = strings.length;
        StringArray stringArray = new StringArray(aJCas, length);
        stringArray.copyFromArray(strings, 0, 0, length);
        return stringArray;
    } else {
        return new StringArray(aJCas, 0);
    }
}
 
Example #24
Source File: BinaryCasSerDes.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Doing updates for delta cas for existing objects.
 * Cases:
 *   - item in heap-stored-array = update the corresponding item in the FS
 *   - non-ref in feature slot - update the corresponding feature
 *   - ref (to long/double value, to string)
 *       -- these always reference entries in long/string tables that are new (above the line)
 *       -- these have already been deserialized
 *   - ref (to main heap) - can update this directly       
 *   NOTE: entire aux arrays never have their refs to the aux heaps updated, for 
 *           arrays of boolean, byte, short, long, double
 *   NOTE: Slot updates for FS refs always point to addr which are in the addr2fs table or are 0 (null),
 *           because if the ref is to a new one, those have been already deserialized by this point, and
 *                   if the ref is to a below-the-line one, those are already put into the addr2fs table
 * @param bds - helper data
 * @param slotAddr - the main heap slot addr being updated
 * @param slotValue - the new value
 */
private void updateHeapSlot(BinDeserSupport bds, int slotAddr, int slotValue, Int2ObjHashMap<TOP, TOP> addr2fs) {
  TOP fs = bds.fs;
  TypeImpl type = fs._getTypeImpl();
  if (type.isArray()) {
    // only heap stored arrays have mod updates.  
    final int hsai = slotAddr - bds.fsStartAddr - arrayContentOffset;  // heap stored array index
    switch(type.getComponentSlotKind()) {
    // heap stored arrays
    case Slot_Int: ((IntegerArray)fs).set(hsai,                   slotValue);  break;
    case Slot_Float: ((FloatArray)fs).set(hsai, CASImpl.int2float(slotValue)); break;
    
    case Slot_StrRef: ((StringArray)fs).set(hsai,  stringHeap.getStringForCode(slotValue)); break;
    case Slot_HeapRef:((FSArray    )fs).set(hsai, addr2fs.get(slotValue)); break;
    
    default: Misc.internalError();
    } // end of switch for component type of arrays
  } else { // end of arrays
    // is plain fs with fields
    final int offset0 = slotAddr - bds.fsStartAddr - 1;  // 0 based offset of feature, -1 for type code word
    FeatureImpl feat = type.getFeatureImpls()[offset0];
    SlotKind slotKind = feat.getSlotKind();
    switch(slotKind) {
    case Slot_Boolean:
    case Slot_Byte:   
    case Slot_Short:  
    case Slot_Int:    
    case Slot_Float: fs._setIntLikeValue(slotKind, feat, slotValue); break;
    
    case Slot_LongRef:   fs.setLongValue(feat, longHeap.getHeapValue(slotValue)); break;
    case Slot_DoubleRef: fs.setDoubleValue(feat, CASImpl.long2double(longHeap.getHeapValue(slotValue))); break;
    case Slot_StrRef: {
      String s = stringHeap.getStringForCode(slotValue);        
      if (updateStringFeature(fs, feat, s, null)) {
        fs.setStringValue(feat, stringHeap.getStringForCode(slotValue));
      }
      break;
    }
      
    case Slot_HeapRef:   fs.setFeatureValue(feat, addr2fs.get(slotValue)); break;
    default: Misc.internalError();
    }
  }
}
 
Example #25
Source File: ManualDescriptor.java    From bluima with Apache License 2.0 4 votes vote down vote up
/** getter for geneSymbolList - gets GeneSymbolList in PubMed
 * @generated
 * @return value of the feature 
 */
public StringArray getGeneSymbolList() {
  if (ManualDescriptor_Type.featOkTst && ((ManualDescriptor_Type)jcasType).casFeat_geneSymbolList == null)
    jcasType.jcas.throwFeatMissing("geneSymbolList", "de.julielab.jules.types.pubmed.ManualDescriptor");
  return (StringArray)(jcasType.ll_cas.ll_getFSForRef(jcasType.ll_cas.ll_getRefValue(addr, ((ManualDescriptor_Type)jcasType).casFeatCode_geneSymbolList)));}
 
Example #26
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
@Override
public StringArrayFS createStringArrayFS(int length) {
  checkArrayPreconditions(length);
  return new StringArray(getTypeSystemImpl().stringArrayType, this, length);
}
 
Example #27
Source File: CasCompare.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public List<Runnable> sortStringArray(String typeName, String featureBaseName) {
//    stringArraysToSort.add(typeName + ":" + featureBaseName);
    return type_feature_to_runnable(typeName, featureBaseName, (fs, feat) -> 
      sortStringArray((StringArray)fs.getFeatureValue(feat)));
  }
 
Example #28
Source File: DataTable.java    From bluima with Apache License 2.0 4 votes vote down vote up
/** setter for body - sets the body of the table that contains data 
 * @generated
 * @param v value to set into the feature 
 */
public void setBody(StringArray v) {
  if (DataTable_Type.featOkTst && ((DataTable_Type)jcasType).casFeat_body == null)
    jcasType.jcas.throwFeatMissing("body", "ch.epfl.bbp.uima.types.DataTable");
  jcasType.ll_cas.ll_setRefValue(addr, ((DataTable_Type)jcasType).casFeatCode_body, jcasType.ll_cas.ll_getFSRef(v));}
 
Example #29
Source File: JsonCasSerializer.java    From termsuite-core with Apache License 2.0 4 votes vote down vote up
private static void writeStringFSArrayField(JsonGenerator jg, String fieldName, StringArray value) throws IOException {
    if(value == null)
        return;
    jg.writeStringField(fieldName, Joiner.on(" ").join(value.toArray()));
}
 
Example #30
Source File: FeatureTokens.java    From bluima with Apache License 2.0 4 votes vote down vote up
/** getter for tokens - gets 
 * @generated
 * @return value of the feature 
 */
public StringArray getTokens() {
  if (FeatureTokens_Type.featOkTst && ((FeatureTokens_Type)jcasType).casFeat_tokens == null)
    jcasType.jcas.throwFeatMissing("tokens", "ch.epfl.bbp.uima.types.FeatureTokens");
  return (StringArray)(jcasType.ll_cas.ll_getFSForRef(jcasType.ll_cas.ll_getRefValue(addr, ((FeatureTokens_Type)jcasType).casFeatCode_tokens)));}