public static Type getComponentType(Type colType) { if (!isCollectionType(colType)) { throw new IllegalArgumentException(String.format( "Type %s is not a collection type", colType)); } return colType.getComponentType(); }
public static Type getCollectionElementType(Feature targetFeat) { // TODO handle lists if (targetFeat.getRange().isArray()) { return targetFeat.getRange().getComponentType(); } else { throw new UnsupportedOperationException(); } }
private Object getPrimitiveComponent(FeatureStructure array, int index) { Type cType = array.getType().getComponentType(); if (cType.getName().equals("uima.cas.String")) return ((StringArray) array).get(index); else if (cType.getName().equals("uima.cas.Double")) return ((DoubleArray) array).get(index); else if (cType.getName().equals("uima.cas.Integer")) return ((IntegerArray) array).get(index); return null; }
/** * Set the array value for the given feature on the given annotation * * @param annotation to add the feature * @param feature to add * @param value to add */ public static void setPrimitiveArray( final JCas jCas, final BaleenAnnotation annotation, final Feature feature, final Object value) { final Type componentType = feature.getRange().getComponentType(); Collection<?> list; if (value instanceof Collection) { list = (Collection<?>) value; } else { list = Collections.singletonList(value); } CommonArrayFS fs = getCommonArrayFS(jCas, componentType, list); if (fs != null) { annotation.setFeatureValue(feature, fs); } }
Type linkType = aFeature.getRange().getComponentType(); if (linkType == null) { return false;
public static boolean isValidPath(Type type, String path, JCas view) { String[] pathMembers = path.split("/"); Type pathMemberType = type; // will be set to type of last path member // feature type for (String pathMember : pathMembers) { Feature feature = pathMemberType.getFeatureByBaseName(pathMember); if (feature == null) { return false; } pathMemberType = feature.getRange(); if (pathMemberType.isArray()) pathMemberType = pathMemberType.getComponentType(); } return isValidType(pathMemberType, view.getTypeSystem()); }
Type linkType = aFeature.getRange().getComponentType(); if (linkType == null) { return false;
private static boolean isSlotFeature(TypeSystem aTypeSystem, Feature feat) { // This could be written more efficiently using a single conjunction. The reason this // has not been done is to facilitate debugging. boolean multiValued = feat.getRange().isArray() || aTypeSystem .subsumes(aTypeSystem.getType(CAS.TYPE_NAME_LIST_BASE), feat.getRange()); if (!multiValued) { return false; } boolean linkInheritsFromTop = CAS.TYPE_NAME_TOP .equals(aTypeSystem.getParent(feat.getRange().getComponentType()).getName()); boolean hasTargetFeature = feat.getRange().getComponentType() .getFeatureByBaseName(FEAT_SLOT_TARGET) != null; boolean hasRoleFeature = feat.getRange().getComponentType() .getFeatureByBaseName(FEAT_SLOT_ROLE) != null; return linkInheritsFromTop && hasTargetFeature && hasRoleFeature; }
private static Object[] getPrimitiveArrayFeatureValue( JCas view, FeatureStructure featureStructure, Feature feature) throws CleartkExtractorException { TypeSystem typeSystem = view.getTypeSystem(); Type type = feature.getRange(); if (type.isArray()) { Type componentType = type.getComponentType(); FeatureStructure featureValue = featureStructure.getFeatureValue(feature); if (componentType.equals(typeSystem.getType("uima.cas.String"))) { return ((StringArray) featureValue).toArray(); } else if (componentType.equals(typeSystem.getType("uima.cas.Boolean"))) { return Arrays.asList(((BooleanArray) featureValue).toArray()).toArray(); } else if (componentType.equals(typeSystem.getType("uima.cas.Double"))) { return Arrays.asList(((DoubleArray) featureValue).toArray()).toArray(); } else if (componentType.equals(typeSystem.getType("uima.cas.Float"))) { return Arrays.asList(((FloatArray) featureValue).toArray()).toArray(); } else if (componentType.equals(typeSystem.getType("uima.cas.Byte"))) { return Arrays.asList(((ByteArray) featureValue).toArray()).toArray(); } else if (componentType.equals(typeSystem.getType("uima.cas.Short"))) { return Arrays.asList(((ShortArray) featureValue).toArray()).toArray(); } else if (componentType.equals(typeSystem.getType("uima.cas.Integer"))) { return Arrays.asList(((IntegerArray) featureValue).toArray()).toArray(); } else if (componentType.equals(typeSystem.getType("uima.cas.Long"))) { return Arrays.asList(((LongArray) featureValue).toArray()).toArray(); } } else throw CleartkExtractorException.notPrimitiveArray(feature); return null; }
private static boolean isSlotFeature(TypeSystem aTypeSystem, Feature feat) { // This could be written more efficiently using a single conjunction. The reason this // has not been done is to facilitate debugging. boolean multiValued = feat.getRange().isArray() || aTypeSystem .subsumes(aTypeSystem.getType(CAS.TYPE_NAME_LIST_BASE), feat.getRange()); if (!multiValued) { return false; } boolean linkInheritsFromTop = CAS.TYPE_NAME_TOP .equals(aTypeSystem.getParent(feat.getRange().getComponentType()).getName()); boolean hasTargetFeature = feat.getRange().getComponentType() .getFeatureByBaseName(FEAT_SLOT_TARGET) != null; boolean hasRoleFeature = feat.getRange().getComponentType() .getFeatureByBaseName(FEAT_SLOT_ROLE) != null; return linkInheritsFromTop && hasTargetFeature && hasRoleFeature; }
private void populateFeature( final JCas jCas, final Map<String, Object> map, final BaleenAnnotation annotation, final Feature f, final List<ReferencedFeatures> featuresToDereference) { final String name = JsonJCas.normalizeFeatureName(f); if (f.getRange().isPrimitive()) { // Straight primitive... NewFeatureUtils.setPrimitive(annotation, f, map.get(name)); } else if (f.getRange().isArray() && f.getRange().getComponentType() != null && f.getRange().getComponentType().isPrimitive()) { NewFeatureUtils.setPrimitiveArray(jCas, annotation, f, map.get(name)); } else { // Not a primitive or an array of primitives... looks if its references to other annotators final String reference = JsonJCas.makeReference(name); if (map.containsKey(reference) && map.get(reference) != null) { // TODO: Save annotation, f and a Long / Collection<Long> (depending on if an array) to be // resolved later featuresToDereference.add(new ReferencedFeatures(annotation, f, map.get(reference))); } } }
public static boolean isSlotFeature(Feature aFeature) { if (aFeature.getRange().isArray()) { Type elementType = aFeature.getRange().getComponentType(); return elementType.getFeatureByBaseName(FEAT_SLOT_TARGET) != null && elementType.getFeatureByBaseName(FEAT_SLOT_ROLE) != null; } return false; }
public static boolean isSlotFeature(Feature aFeature) { if (aFeature.getRange().isArray()) { Type elementType = aFeature.getRange().getComponentType(); return elementType.getFeatureByBaseName(FEAT_SLOT_TARGET) != null && elementType.getFeatureByBaseName(FEAT_SLOT_ROLE) != null; } return false; }
/** * Convert a {@link Feature} to an equivalent {@link FeatureDescription}. * * @param aFeature * feature object to convert * @return a FeatureDescription that is equivalent to <code>aFeature</code> */ public static FeatureDescription feature2FeatureDescription(Feature aFeature) { FeatureDescription featDesc = UIMAFramework.getResourceSpecifierFactory() .createFeatureDescription(); featDesc.setName(aFeature.getShortName()); if (aFeature.isMultipleReferencesAllowed()) { featDesc.setMultipleReferencesAllowed(true); } Type rangeType = aFeature.getRange(); //special check for array range types, which are represented in the CAS as //elementType[] but in the descriptor as an FSArray with an <elementType> if (rangeType.isArray() && !rangeType.getComponentType().isPrimitive()) { featDesc.setRangeTypeName(CAS.TYPE_NAME_FS_ARRAY); String elementTypeName = rangeType.getComponentType().getName(); if (!CAS.TYPE_NAME_TOP.equals(elementTypeName)) { featDesc.setElementType(elementTypeName); } } else { featDesc.setRangeTypeName(rangeType.getName()); } return featDesc; }
private Object apply(FeatureStructure a, String[] path) { FeatureStructure current = a; for (int i = 0; i < path.length; i++) { if (current == null) return null; String featureName = path[i]; if (featureName.contains("(")) { return getPrimitiveFunction(current, featureName); } else if (current.getType().isArray()) { int arrayIndex = Integer.valueOf(featureName); if (current.getType().getComponentType().isPrimitive()) { return getPrimitiveComponent(current, arrayIndex); } else { FSArray arr = (FSArray) current; current = arr.get(arrayIndex); } } else { Feature feature = current.getType().getFeatureByBaseName(featureName); if (feature.getRange().isPrimitive()) { return getPrimitive(current, feature); } else { current = current.getFeatureValue(feature); } } } return null; }
private static void generateColumns(TypeSystem aTypeSystem, TsvSchema aSchema, LayerType aLayerType, Type aType) { List<String> specialFeatures = asList( CAS.FEATURE_FULL_NAME_BEGIN, CAS.FEATURE_FULL_NAME_END, CAS.FEATURE_FULL_NAME_SOFA); for (Feature feat : aType.getFeatures()) { if (specialFeatures.contains(feat.getName())) { continue; } if (isPrimitiveFeature(feat)) { aSchema.addColumn(new TsvColumn(aType, aLayerType, feat, FeatureType.PRIMITIVE)); } else if (SPAN.equals(aLayerType) && isSlotFeature(aTypeSystem, feat)) { aSchema.addColumn(new TsvColumn(aType, aLayerType, feat, FeatureType.SLOT_ROLE)); Type slotTargetType = feat.getRange().getComponentType() .getFeatureByBaseName(FEAT_SLOT_TARGET).getRange(); TsvColumn targetColumn = new TsvColumn(aType, aLayerType, feat, FeatureType.SLOT_TARGET); targetColumn.setTargetTypeHint(slotTargetType); aSchema.addColumn(targetColumn); } } }
private static void generateColumns(TypeSystem aTypeSystem, TsvSchema aSchema, LayerType aLayerType, Type aType) { List<String> specialFeatures = asList( CAS.FEATURE_FULL_NAME_BEGIN, CAS.FEATURE_FULL_NAME_END, CAS.FEATURE_FULL_NAME_SOFA); for (Feature feat : aType.getFeatures()) { if (specialFeatures.contains(feat.getName())) { continue; } if (isPrimitiveFeature(feat)) { aSchema.addColumn(new TsvColumn(aType, aLayerType, feat, FeatureType.PRIMITIVE)); } else if (SPAN.equals(aLayerType) && isSlotFeature(aTypeSystem, feat)) { aSchema.addColumn(new TsvColumn(aType, aLayerType, feat, FeatureType.SLOT_ROLE)); Type slotTargetType = feat.getRange().getComponentType() .getFeatureByBaseName(FEAT_SLOT_TARGET).getRange(); TsvColumn targetColumn = new TsvColumn(aType, aLayerType, feat, FeatureType.SLOT_TARGET); targetColumn.setTargetTypeHint(slotTargetType); aSchema.addColumn(targetColumn); } } }
private AnnotationFeature analyzeFeature(TypeSystem aTS, FeatureDescription aFD, Feature aFeat) { AnnotationFeature feat = new AnnotationFeature(); feat.setType(aFeat.getRange().getName()); feat.setName(aFeat.getShortName()); feat.setUiName(aFeat.getShortName()); feat.setDescription(trimToNull(aFD.getDescription())); feat.setEnabled(true); if (isSlotFeature(aTS, aFeat)) { feat.setType(aFeat.getRange().getComponentType().getFeatureByBaseName("target") .getRange().getName()); feat.setMode(MultiValueMode.ARRAY); feat.setLinkMode(LinkMode.WITH_ROLE); // Need to strip the "[]" marking the type as multi-valued off the type name feat.setLinkTypeName(removeEnd(aFeat.getRange().getName(), "[]")); // FIXME Instead of hard-coding the feature names here, try to auto-detect them by // looking for a String feature and a feature whose type is subsumed by Annotation feat.setLinkTypeRoleFeatureName("role"); feat.setLinkTypeTargetFeatureName("target"); } return feat; }
private AnnotationFeature analyzeFeature(TypeSystem aTS, FeatureDescription aFD, Feature aFeat) { AnnotationFeature feat = new AnnotationFeature(); feat.setType(aFeat.getRange().getName()); feat.setName(aFeat.getShortName()); feat.setUiName(aFeat.getShortName()); feat.setDescription(trimToNull(aFD.getDescription())); feat.setEnabled(true); if (isSlotFeature(aTS, aFeat)) { feat.setType(aFeat.getRange().getComponentType().getFeatureByBaseName("target") .getRange().getName()); feat.setMode(MultiValueMode.ARRAY); feat.setLinkMode(LinkMode.WITH_ROLE); // Need to strip the "[]" marking the type as multi-valued off the type name feat.setLinkTypeName(removeEnd(aFeat.getRange().getName(), "[]")); // FIXME Instead of hard-coding the feature names here, try to auto-detect them by // looking for a String feature and a feature whose type is subsumed by Annotation feat.setLinkTypeRoleFeatureName("role"); feat.setLinkTypeTargetFeatureName("target"); } return feat; }
protected Tree<FeatureStructure> extendArrays(Tree<FeatureStructure> tree) { Type type = tree.getPayload().getType(); String[] aFeatures = getArrayMultiplierForType(type); for (String s : aFeatures) { Feature feature = type.getFeatureByBaseName(s); List<Tree<FeatureStructure>> children = tree.getChildren(); FSArray arr = (FSArray) tree.getPayload().getFeatureValue(feature); Type cType = feature.getRange().getComponentType();// arr.getType().getComponentType(); if (arr == null || arr.size() == 0) { tree.setChildren(new ArrayList<Tree<FeatureStructure>>()); FeatureStructure fs = tree.getPayload().getCAS().createFS(cType); tree.getPayload().getCAS().addFsToIndexes(fs); Tree<FeatureStructure> nChild = new Tree<FeatureStructure>(fs); nChild.setChildren(children); tree.add(nChild); } else { tree.setChildren(new ArrayList<Tree<FeatureStructure>>(arr.size())); for (int i = 0; i < arr.size(); i++) { Tree<FeatureStructure> nChild = new Tree<FeatureStructure>(arr.get(i)); nChild.setChildren(children); tree.add(nChild); } } } for (Tree<FeatureStructure> child : tree.getChildren()) { extendArrays(child); } return tree; }