public static <T> T getFeature(FeatureStructure aFS, String aFeature, Class<T> aClazz) { Feature feat = getMandatoryFeature(aFS, aFeature); return getFeature(aFS, feat, aClazz); }
private static void writePrimitiveValue(PrintWriter aOut, TsvDocument aDoc, TsvColumn aCol, AnnotationFS aFS) { Object value = getFeature(aFS, aCol.uimaFeature, Object.class); value = value == null ? NULL_VALUE : escapeValue(String.valueOf(value)); aOut.print(value); }
private static void writePrimitiveValue(PrintWriter aOut, TsvDocument aDoc, TsvColumn aCol, AnnotationFS aFS) { Object value = getFeature(aFS, aCol.uimaFeature, Object.class); value = value == null ? NULL_VALUE : escapeValue(String.valueOf(value)); aOut.print(value); }
private static void writeSlotRole(PrintWriter aOut, TsvDocument aDoc, TsvColumn aCol, AnnotationFS aFS) { FeatureStructure[] links = getFeature(aFS, aCol.uimaFeature, FeatureStructure[].class); if (links != null && links.length > 0) { for (int i = 0; i < links.length; i++) { if (i > 0) { aOut.print(SLOT_SEP); } String value = getFeature(links[i], TsvSchema.FEAT_SLOT_ROLE, String.class); value = value == null ? NULL_VALUE : escapeValue(value); aOut.print(value); } } else { aOut.print(NULL_COLUMN); } writeDisambiguationId(aOut, aDoc, aFS); }
private static void writeSlotRole(PrintWriter aOut, TsvDocument aDoc, TsvColumn aCol, AnnotationFS aFS) { FeatureStructure[] links = getFeature(aFS, aCol.uimaFeature, FeatureStructure[].class); if (links != null && links.length > 0) { for (int i = 0; i < links.length; i++) { if (i > 0) { aOut.print(SLOT_SEP); } String value = getFeature(links[i], TsvSchema.FEAT_SLOT_ROLE, String.class); value = value == null ? NULL_VALUE : escapeValue(value); aOut.print(value); } } else { aOut.print(NULL_COLUMN); } writeDisambiguationId(aOut, aDoc, aFS); }
@Override public boolean check(JCas aJCas, List<Message> aMessages) { for (Constituent parent : select(aJCas, Constituent.class)) { Collection<Annotation> children = select(parent.getChildren(), Annotation.class); for (Annotation child : children) { Annotation declParent = FSUtil.getFeature(child, "parent", Annotation.class); if (declParent == null) { aMessages.add(new Message(this, ERROR, String.format( "Child without parent set: %s", child))); } else if (declParent != parent) { aMessages.add(new Message(this, ERROR, String.format( "Child points to wrong parent: %s", child))); } } } return aMessages.stream().anyMatch(m -> m.level == ERROR); } // end::check-example[]
public static boolean isRequiredFeatureMissing(AnnotationFeature aFeature, FeatureStructure aFS) { return aFeature.isRequired() && CAS.TYPE_NAME_STRING.equals(aFeature.getType()) && StringUtils.isBlank(FSUtil.getFeature(aFS, aFeature.getName(), String.class)); } }
@Override public boolean check(JCas aJCas, List<Message> aMessages) { for (Constituent parent : select(aJCas, Constituent.class)) { Collection<Annotation> children = select(parent.getChildren(), Annotation.class); for (Annotation child : children) { Annotation declParent = FSUtil.getFeature(child, "parent", Annotation.class); if (declParent == null) { aMessages.add(new Message(this, ERROR, String.format( "Child without parent set: %s", child))); } else if (declParent != parent) { aMessages.add(new Message(this, ERROR, String.format( "Child points to wrong parent: %s", child))); } } } return aMessages.stream().anyMatch(m -> m.level == ERROR); } // end::check-example[]
public static boolean isRequiredFeatureMissing(AnnotationFeature aFeature, FeatureStructure aFS) { return aFeature.isRequired() && CAS.TYPE_NAME_STRING.equals(aFeature.getType()) && StringUtils.isBlank(FSUtil.getFeature(aFS, aFeature.getName(), String.class)); } }
private static void writeRelationReference(PrintWriter aOut, TsvDocument aDoc, TsvColumn aCol, AnnotationFS aFS) { AnnotationFS targetFS = getFeature(aFS, FEAT_REL_TARGET, AnnotationFS.class); AnnotationFS sourceFS = getFeature(aFS, FEAT_REL_SOURCE, AnnotationFS.class); // The column contains the ID of the unit from which the relation is pointing to the // current unit, i.e. the sourceUnit of the relation. TsvUnit sourceUnit = aDoc.findIdDefiningUnit(sourceFS); aOut.print(sourceUnit.getId()); // If the source/target is ambiguous, add the disambiguation IDs Integer sourceId = aDoc.getDisambiguationId(sourceFS); Integer targetId = aDoc.getDisambiguationId(targetFS); if (sourceId != null || targetId != null) { sourceId = sourceId != null ? sourceId : 0; targetId = targetId != null ? targetId : 0; aOut.printf("[%d_%d]", sourceId, targetId); } }
private static void writeRelationReference(PrintWriter aOut, TsvDocument aDoc, TsvColumn aCol, AnnotationFS aFS) { AnnotationFS targetFS = getFeature(aFS, FEAT_REL_TARGET, AnnotationFS.class); AnnotationFS sourceFS = getFeature(aFS, FEAT_REL_SOURCE, AnnotationFS.class); // The column contains the ID of the unit from which the relation is pointing to the // current unit, i.e. the sourceUnit of the relation. TsvUnit sourceUnit = aDoc.findIdDefiningUnit(sourceFS); aOut.print(sourceUnit.getId()); // If the source/target is ambiguous, add the disambiguation IDs Integer sourceId = aDoc.getDisambiguationId(sourceFS); Integer targetId = aDoc.getDisambiguationId(targetFS); if (sourceId != null || targetId != null) { sourceId = sourceId != null ? sourceId : 0; targetId = targetId != null ? targetId : 0; aOut.printf("[%d_%d]", sourceId, targetId); } }
private static void writeChainElement(PrintWriter aOut, TsvDocument aDoc, TsvColumn aCol, AnnotationFS aFS) { String value = getFeature(aFS, COREFERENCE_TYPE_FEATURE, String.class); value = value == null ? NULL_VALUE : escapeValue(value); TsvChain chain = aDoc.getChain(aFS); aOut.printf("%s[%d]", value, chain.getId()); }
private static void writeChainElement(PrintWriter aOut, TsvDocument aDoc, TsvColumn aCol, AnnotationFS aFS) { String value = getFeature(aFS, COREFERENCE_TYPE_FEATURE, String.class); value = value == null ? NULL_VALUE : escapeValue(value); TsvChain chain = aDoc.getChain(aFS); aOut.printf("%s[%d]", value, chain.getId()); }
private static void writeChainLink(PrintWriter aOut, TsvDocument aDoc, TsvColumn aCol, AnnotationFS aFS) { String value = getFeature(aFS, COREFERENCE_RELATION_FEATURE, String.class); value = value == null ? NULL_VALUE : escapeValue(value); TsvChain chain = aDoc.getChain(aFS); aOut.printf("%s->%d-%d", value, chain.getId(), chain.indexOf(aFS) + 1); } }
private static void writeChainLink(PrintWriter aOut, TsvDocument aDoc, TsvColumn aCol, AnnotationFS aFS) { String value = getFeature(aFS, COREFERENCE_RELATION_FEATURE, String.class); value = value == null ? NULL_VALUE : escapeValue(value); TsvChain chain = aDoc.getChain(aFS); aOut.printf("%s->%d-%d", value, chain.getId(), chain.indexOf(aFS) + 1); } }
private Object getValue(FeatureStructure fs, AnnotationFeature aFeature) { Object value; Feature f = fs.getType().getFeatureByBaseName(aFeature.getName()); if (f.getRange().isPrimitive()) { value = FSUtil.getFeature(fs, aFeature.getName(), Object.class); } else if (FSUtil.isMultiValuedFeature(fs, f)) { value = FSUtil.getFeature(fs, aFeature.getName(), List.class); } else { value = FSUtil.getFeature(fs, aFeature.getName(), FeatureStructure.class); } return value; }
private Object getValue(FeatureStructure fs, AnnotationFeature aFeature) { Object value; Feature f = fs.getType().getFeatureByBaseName(aFeature.getName()); if (f.getRange().isPrimitive()) { value = FSUtil.getFeature(fs, aFeature.getName(), Object.class); } else if (FSUtil.isMultiValuedFeature(fs, f)) { value = FSUtil.getFeature(fs, aFeature.getName(), List.class); } else { value = FSUtil.getFeature(fs, aFeature.getName(), FeatureStructure.class); } return value; }
default <V> V getFeatureValue(AnnotationFeature aFeature, FeatureStructure aFS) { Object value; Feature f = aFS.getType().getFeatureByBaseName(aFeature.getName()); if (f.getRange().isPrimitive()) { value = FSUtil.getFeature(aFS, aFeature.getName(), Object.class); } else if (FSUtil.isMultiValuedFeature(aFS, f)) { value = FSUtil.getFeature(aFS, aFeature.getName(), List.class); } else { value = FSUtil.getFeature(aFS, aFeature.getName(), FeatureStructure.class); } return (V) wrapFeatureValue(aFeature, aFS.getCAS(), value); }
default <V> V getFeatureValue(AnnotationFeature aFeature, FeatureStructure aFS) { Object value; Feature f = aFS.getType().getFeatureByBaseName(aFeature.getName()); if (f.getRange().isPrimitive()) { value = FSUtil.getFeature(aFS, aFeature.getName(), Object.class); } else if (FSUtil.isMultiValuedFeature(aFS, f)) { value = FSUtil.getFeature(aFS, aFeature.getName(), List.class); } else { value = FSUtil.getFeature(aFS, aFeature.getName(), FeatureStructure.class); } return (V) wrapFeatureValue(aFeature, aFS.getCAS(), value); }
@Test public void thatRelationAttachmentBehaviorOnCreateWorks() throws Exception { TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class); builder.buildTokens(jcas, "This is a test ."); for (Token t : select(jcas, Token.class)) { POS pos = new POS(jcas, t.getBegin(), t.getEnd()); t.setPos(pos); pos.addToIndexes(); } RelationAdapter sut = new RelationAdapter(featureSupportRegistry, null, depLayer, FEAT_REL_TARGET, FEAT_REL_SOURCE, asList(dependencyLayerGovernor, dependencyLayerDependent), behaviors); List<POS> posAnnotations = new ArrayList<>(select(jcas, POS.class)); List<Token> tokens = new ArrayList<>(select(jcas, Token.class)); POS source = posAnnotations.get(0); POS target = posAnnotations.get(1); AnnotationFS dep = sut.add(document, username, source, target, jcas, 0, jcas.getDocumentText().length()); assertThat(FSUtil.getFeature(dep, FEAT_REL_SOURCE, Token.class)).isEqualTo(tokens.get(0)); assertThat(FSUtil.getFeature(dep, FEAT_REL_TARGET, Token.class)).isEqualTo(tokens.get(1)); }