private void copyFeatures(AnnotationFS oldFS, FeatureStructure newFS, CAS cas) { List<?> features = oldFS.getType().getFeatures(); Type newType = newFS.getType(); for (Object object : features) { Feature feature = (Feature) object; String shortName = feature.getShortName(); Feature newFeature = newType.getFeatureByBaseName(shortName); if (newFeature != null) { if (feature.getRange().isPrimitive()) { String value = oldFS.getFeatureValueAsString(feature); newFS.setFeatureValueFromString(newFeature, value); } else { FeatureStructure value = oldFS.getFeatureValue(feature); newFS.setFeatureValue(newFeature, value); } } } }
/** * Checks if the given feature has the expected type otherwise * an exception is thrown. * * @param feature * @param expectedType * @throws AnalysisEngineProcessException - if type does not match */ private static void checkFeatureType(Feature feature, String expectedType) throws AnalysisEngineProcessException { if (!feature.getRange().getName().equals(expectedType)) { throw new OpenNlpAnnotatorProcessException( ExceptionMessages.WRONG_FEATURE_TYPE, new Object[] {feature.getName(), expectedType }); } }
private void annotate(CAS cas, Feature feature, int begin, int end, String value) { Type type = feature.getDomain(); AnnotationFS annotation = cas.createAnnotation(type, begin, end); annotation.setStringValue(feature,value); cas.addFsToIndexes(annotation); }
public PrimitiveColumn(Feature feature) { super(new String[] { feature.getShortName() }); this.datatype = Datatype.fromString(feature.getRange().getName()); this.feature = feature; }
cas.setDocumentText("I listen to lectures by Prof. Gurevych sometimes."); TypeSystem ts = cas.getTypeSystem(); Type profType = ts.getType("de.tud.Prof"); Feature profNameFeature = profType.getFeatureByBaseName("fullName"); Feature profBossFeature = profType.getFeatureByBaseName("boss"); AnnotationFS proemel = cas.createAnnotation(profType, 0, 0); proemel.setStringValue(profNameFeature, "Hans Juergen Proeml"); cas.addFsToIndexes(proemel); AnnotationFS gurevych = cas.createAnnotation(profType, 24, 38); gurevych.setStringValue(profNameFeature, "Iryna Gurevych"); gurevych.setFeatureValue(profBossFeature, proemel); cas.addFsToIndexes(gurevych); Feature someFeature = gurevych.getType().getFeatureByBaseName(feature); if (someFeature.getRange().isPrimitive()) { String value = gurevych.getFeatureValueAsString(someFeature); System.out.println(value); FeatureStructure value = gurevych.getFeatureValue(someFeature); System.out.printf("%s (%s)%n", value.getFeatureValueAsString(profNameFeature), value.getType());
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 void forceSetFeatureValue(FeatureStructure aFS, String aFeatureName, String aValue) { CASImpl casImpl = (CASImpl) aFS.getCAS().getLowLevelCAS(); TypeSystemImpl ts = (TypeSystemImpl) aFS.getCAS().getTypeSystem(); Feature feat = aFS.getType().getFeatureByBaseName(aFeatureName); int featCode = ((FeatureImpl) feat).getCode(); int thisType = ((TypeImpl) aFS.getType()).getCode(); if (!ts.isApprop(thisType, featCode)) { throw new IllegalArgumentException("Feature structure does not have that feature"); } if (!ts.subsumes(ts.getType(CAS.TYPE_NAME_STRING), feat.getRange())) { throw new IllegalArgumentException("Not a string feature!"); } casImpl.ll_setStringValue(casImpl.ll_getFSRef(aFS), featCode, aValue); }
/** * {@code FeatureCopier} used for features which are references to {@code FeatureStructure}s. * * @param fromFeature the {@link Feature} * @param fromFs the {@link FeatureStructure} to copy from * @param toFs the {@link FeatureStructure} to copy to */ private void defaultFeatureMapper(Feature fromFeature, FeatureStructure fromFs, FeatureStructure toFs) { TypeSystem typeSystem = fromFs.getCAS().getTypeSystem(); if (typeSystem.subsumes(typeSystem.getType(CAS.TYPE_NAME_STRING), fromFeature.getRange())) { STRING_COPIER.copy(fromFeature, fromFs, toFs); } else { FeatureStructure fromFeatureValue = fromFs.getFeatureValue(fromFeature); if (fromFeatureValue != null) { FeatureStructure toFeatureValue = fsEncounteredCallback.apply(fromFeatureValue); Feature toFeature = toFs.getType().getFeatureByBaseName(fromFeature.getShortName()); toFs.setFeatureValue(toFeature, toFeatureValue); } } }
public static void collect(Set<FeatureStructure> aFSes, FeatureStructure aFS) { if (aFS != null && !aFSes.contains(aFS)) { aFSes.add(aFS); for (Feature f : aFS.getType().getFeatures()) { if (!f.getRange().isPrimitive() && !CAS.FEATURE_BASE_NAME_SOFA.equals(f.getShortName())) { collect(aFSes, aFS.getFeatureValue(f)); } } } }
public void setStringValue(Feature feat, String val) { final TypeSystemImpl ts = this.getCASImpl().getTypeSystemImpl(); final int featCode = ((FeatureImpl) feat).getCode(); final int rangeType = ts.range(featCode); final int thisType = this.getCASImpl().getHeapValue(this.getAddress()); final int stringType = ((TypeImpl) this.getCASImpl().getTypeSystem().getType( CAS.TYPE_NAME_STRING)).getCode(); if (!ts.isApprop(thisType, featCode)) { CASRuntimeException e = new CASRuntimeException(CASRuntimeException.INAPPROP_FEAT, new String[] { feat.getName(), this.getType().getName() }); throw e; } if (!ts.subsumes(stringType, rangeType)) { CASRuntimeException e = new CASRuntimeException(CASRuntimeException.INAPPROP_TYPE, new String[] { feat.getRange().getName(), this.getCAS().getTypeSystem().getType(CAS.TYPE_NAME_STRING).getName() }); throw e; } this.getCAS().getLowLevelCAS().ll_setStringValue(this.getAddress(), featCode, val); }
Stem stemAnnot = new Stem(jcas, fs.getBegin(), fs.getEnd()); stemAnnot.setValue(stemValue); stemAnnot.addToIndexes(jcas); Feature feat = fs.getType().getFeatureByBaseName("stem"); if (feat != null && feat.getRange() != null && jcas.getTypeSystem().subsumes(feat.getRange(), stemAnnot.getType())) { fs.setFeatureValue(feat, stemAnnot);
public void process(CAS tcas) { FSIndex<AnnotationFS> sentenceIndex = tcas.getAnnotationIndex(mNameType); String language = tcas.getDocumentLanguage(); String text = nameAnnotation.getCoveredText(); if (CAS.TYPE_NAME_STRING.equals(mStructureFeature.getRange().getName())) { nameAnnotation.setStringValue(mStructureFeature, text); } else { if (CAS.TYPE_NAME_BYTE.equals(mStructureFeature.getRange().getName())) { nameAnnotation.setByteValue(mStructureFeature, number.byteValue()); } else if (CAS.TYPE_NAME_SHORT.equals(mStructureFeature.getRange() .getName())) { nameAnnotation.setShortValue(mStructureFeature, number.shortValue()); } else if (CAS.TYPE_NAME_INTEGER.equals(mStructureFeature.getRange() .getName())) { nameAnnotation.setIntValue(mStructureFeature, number.intValue()); } else if (CAS.TYPE_NAME_LONG.equals(mStructureFeature.getRange() .getName())) { nameAnnotation.setLongValue(mStructureFeature, number.longValue()); } else if (CAS.TYPE_NAME_FLOAT.equals(mStructureFeature.getRange() .getName())) { nameAnnotation.setFloatValue(mStructureFeature, number.floatValue()); } else if (CAS.TYPE_NAME_DOUBLE.equals(mStructureFeature.getRange() .getName())) { nameAnnotation
/** * Gets the sortable feature names. * * @param selectedTypeName the selected type name * @return an array of features whose range is primitive */ private String[] getSortableFeatureNames(String selectedTypeName) { Type selectedType = section.editor.getCurrentView().getTypeSystem().getType(selectedTypeName); List feats = selectedType.getFeatures(); Collection sortableFeatureNames = new ArrayList(); for (int i = 0; i < feats.size(); i++) { Feature feature = (Feature) feats.get(i); Type rangeType = feature.getRange(); if (AbstractSection.isIndexableRange(rangeType.getName())) { if (!alreadyUsedKeys.contains(feature.getShortName())) sortableFeatureNames.add(feature.getShortName()); } } String[] result = (String[]) sortableFeatureNames.toArray(stringArray0); Arrays.sort(result); return result; }
TypeSystem ts = structure.getCAS().getTypeSystem(); result = structure.getBooleanValue(feature); } else if (Byte.class.equals(primitiveClass)) { result = structure.getByteValue(feature); } else if (Short.class.equals(primitiveClass)) { result = structure.getShortValue(feature); } else { throw new IllegalStateException("Unexpected type: " + feature.getRange().getName());
@Override public List<String> getList(MatchContext context, RutaStream stream) { AnnotationFS annotation = context.getAnnotation(); Feature feature = this.fe.getFeature(context, stream); if (feature == null || !feature.getRange().isArray() || !StringUtils.equals(feature.getRange().getName(), CAS.TYPE_NAME_STRING_ARRAY)) { // throw runtime exception? return Collections.emptyList(); } List<AnnotationFS> list = this.getTargetAnnotation(annotation, this.fe, context, stream); Collection<? extends FeatureStructure> featureStructures = this.fe.getFeatureStructures(list, false, context, stream); List<String> result = new ArrayList<>(); for (FeatureStructure each : featureStructures) { if (each instanceof AnnotationFS && !each.getType().equals(annotation.getType())) { feature = this.fe.getFeature(new MatchContext((AnnotationFS) each, context.getElement(), context.getRuleMatch(), context.getDirection()), stream); } FeatureStructure featureValue = each.getFeatureValue(feature); if (featureValue instanceof StringArrayFS) { StringArrayFS array = (StringArrayFS) featureValue; for (int i = 0; i < array.size(); i++) { String b = array.get(i); result.add(b); } } } return result; }
public void setShortValue(Feature feat, short val) throws CASRuntimeException { final TypeSystemImpl ts = this.getCASImpl().getTypeSystemImpl(); if (!ts.subsumes(((TypeImpl) feat.getDomain()).getCode(), this.getCASImpl().getHeapValue( this.getAddress()))) { throwUndefinedFeatureExc(feat, getType()); } if (!ts.subsumes(((TypeImpl) feat.getRange()).getCode(), ((TypeImpl) ts .getType(CAS.TYPE_NAME_SHORT)).getCode())) { throwIllegalRangeExc(feat, ts.getType(CAS.TYPE_NAME_SHORT)); } final int featCode = ((FeatureImpl) feat).getCode(); this.getCASImpl().setFeatureValue(this.getAddress(), featCode, val); }
/** * Gets the feature names for type. * * @param aTypeName the a type name * @param cas the cas * @return the feature names for type */ // untimely ripped from UIMA since it does not work with a taeDescription private String[] getFeatureNamesForType(String aTypeName, CAS cas) { TypeSystem ts = cas.getTypeSystem(); Type t = ts.getType(aTypeName); if (t != null) { List features = t.getFeatures(); String[] featNames = new String[features.size()]; for (int i = 0; i < features.size(); i++) { Feature f = (Feature) features.get(i); featNames[i] = f.getShortName(); } return featNames; } else { return null; } }
private static void setAllRoleAnnosOnPosition(Map<String, JCas> aJCases, Map<String, ArrayFS> slotAnnosPerUser, Set<String> aUsers, FeatureStructure aBaseAnno, Feature aFeature) { Type t = aBaseAnno.getType(); int begin = ((AnnotationFS) aBaseAnno).getBegin(); int end = ((AnnotationFS) aBaseAnno).getEnd(); for (String user : aUsers) { for (AnnotationFS baseFS : selectCovered(aJCases.get(user).getCas(), t, begin, end)) { // if non-equal stacked annotations with slot feature exists, get the right one if (isSameAnno(aBaseAnno, baseFS)) { ArrayFS roleFs = (ArrayFS) WebAnnoCasUtil.getFeatureFS(baseFS, aFeature.getShortName()); slotAnnosPerUser.put(user, roleFs); break; } } } }
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; }