use of org.apache.uima.cas.Feature in project webanno by webanno.
the class ComplexTypeTest method testProfType.
@Test
public void testProfType() throws Exception {
TypeSystemDescription tsd = TypeSystemDescriptionFactory.createTypeSystemDescription("desc.types.TestTypeSystemDescriptor");
CAS cas = CasCreationUtils.createCas(tsd, null, null);
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);
/*
* for (String feature : Arrays.asList("fullName", "boss")) { Feature someFeature =
* gurevych.getType().getFeatureByBaseName(feature); if
* (someFeature.getRange().isPrimitive()) { String value =
* gurevych.getFeatureValueAsString(someFeature); System.out.println(value); } else {
* FeatureStructure value = gurevych.getFeatureValue(someFeature);
* System.out.printf("%s (%s)%n", value.getFeatureValueAsString(profNameFeature),
* value.getType()); } }
*/
ConstraintsGrammar parser = new ConstraintsGrammar(new FileInputStream("src/test/resources/rules/prof.rules"));
Parse p = parser.Parse();
ParsedConstraints constraints = p.accept(new ParserVisitor());
Evaluator constraintsEvaluator = new ValuesGenerator();
List<PossibleValue> possibleValues = constraintsEvaluator.generatePossibleValues(gurevych, "professorName", constraints);
List<PossibleValue> exValues = new LinkedList<>();
exValues.add(new PossibleValue("Iryna Gurevych", false));
assertEquals(possibleValues, exValues);
}
use of org.apache.uima.cas.Feature in project webanno by webanno.
the class RelationRenderer method render.
@Override
public void render(final JCas aJcas, List<AnnotationFeature> aFeatures, VDocument aResponse, AnnotatorState aBratAnnotatorModel) {
List<AnnotationFeature> visibleFeatures = aFeatures.stream().filter(f -> f.isVisible() && f.isEnabled()).collect(Collectors.toList());
ArcAdapter typeAdapter = getTypeAdapter();
Type type = getType(aJcas.getCas(), typeAdapter.getAnnotationTypeName());
int windowBegin = aBratAnnotatorModel.getWindowBeginOffset();
int windowEnd = aBratAnnotatorModel.getWindowEndOffset();
Feature dependentFeature = type.getFeatureByBaseName(typeAdapter.getTargetFeatureName());
Feature governorFeature = type.getFeatureByBaseName(typeAdapter.getSourceFeatureName());
Type spanType = getType(aJcas.getCas(), typeAdapter.getAttachTypeName());
Feature arcSpanFeature = spanType.getFeatureByBaseName(typeAdapter.getAttachFeatureName());
FeatureStructure dependentFs;
FeatureStructure governorFs;
Map<Integer, Set<Integer>> relationLinks = getRelationLinks(aJcas, windowBegin, windowEnd, type, dependentFeature, governorFeature, arcSpanFeature);
// if this is a governor for more than one dependent, avoid duplicate yield
List<Integer> yieldDeps = new ArrayList<>();
for (AnnotationFS fs : selectCovered(aJcas.getCas(), type, windowBegin, windowEnd)) {
if (typeAdapter.getAttachFeatureName() != null) {
dependentFs = fs.getFeatureValue(dependentFeature).getFeatureValue(arcSpanFeature);
governorFs = fs.getFeatureValue(governorFeature).getFeatureValue(arcSpanFeature);
} else {
dependentFs = fs.getFeatureValue(dependentFeature);
governorFs = fs.getFeatureValue(governorFeature);
}
String bratTypeName = TypeUtil.getUiTypeName(typeAdapter);
Map<String, String> features = getFeatures(typeAdapter, fs, visibleFeatures);
if (dependentFs == null || governorFs == null) {
RequestCycle requestCycle = RequestCycle.get();
IPageRequestHandler handler = PageRequestHandlerTracker.getLastHandler(requestCycle);
Page page = (Page) handler.getPage();
StringBuilder message = new StringBuilder();
message.append("Relation [" + typeAdapter.getLayer().getName() + "] with id [" + getAddr(fs) + "] has loose ends - cannot render.");
if (typeAdapter.getAttachFeatureName() != null) {
message.append("\nRelation [" + typeAdapter.getLayer().getName() + "] attached to feature [" + typeAdapter.getAttachFeatureName() + "].");
}
message.append("\nDependent: " + dependentFs);
message.append("\nGovernor: " + governorFs);
page.warn(message.toString());
continue;
}
aResponse.add(new VArc(typeAdapter.getLayer(), fs, bratTypeName, governorFs, dependentFs, features));
// Render errors if required features are missing
renderRequiredFeatureErrors(visibleFeatures, fs, aResponse);
if (relationLinks.keySet().contains(getAddr(governorFs)) && !yieldDeps.contains(getAddr(governorFs))) {
yieldDeps.add(getAddr(governorFs));
// sort the annotations (begin, end)
List<Integer> sortedDepFs = new ArrayList<>(relationLinks.get(getAddr(governorFs)));
sortedDepFs.sort(comparingInt(arg0 -> selectByAddr(aJcas, arg0).getBegin()));
String cm = getYieldMessage(aJcas, sortedDepFs);
aResponse.add(new VComment(governorFs, VCommentType.YIELD, cm));
}
}
}
use of org.apache.uima.cas.Feature in project webanno by webanno.
the class Renderer method getFeatures.
default Map<String, String> getFeatures(TypeAdapter aAdapter, AnnotationFS aFs, List<AnnotationFeature> aFeatures) {
FeatureSupportRegistry fsr = getFeatureSupportRegistry();
Map<String, String> features = new LinkedHashMap<>();
for (AnnotationFeature feature : aFeatures) {
if (!feature.isEnabled() || !feature.isVisible() || !MultiValueMode.NONE.equals(feature.getMultiValueMode())) {
continue;
}
Feature labelFeature = aFs.getType().getFeatureByBaseName(feature.getName());
String label = defaultString(fsr.getFeatureSupport(feature).renderFeatureValue(feature, aFs, labelFeature));
features.put(feature.getName(), label);
}
return features;
}
use of org.apache.uima.cas.Feature in project webanno by webanno.
the class SlotFeatureSupport method getFeatureValue.
@Override
public <T> T getFeatureValue(AnnotationFeature aFeature, FeatureStructure aFS) {
// Get type and features - we need them later in the loop
Feature linkFeature = aFS.getType().getFeatureByBaseName(aFeature.getName());
Type linkType = aFS.getCAS().getTypeSystem().getType(aFeature.getLinkTypeName());
Feature roleFeat = linkType.getFeatureByBaseName(aFeature.getLinkTypeRoleFeatureName());
Feature targetFeat = linkType.getFeatureByBaseName(aFeature.getLinkTypeTargetFeatureName());
List<LinkWithRoleModel> links = new ArrayList<>();
ArrayFS array = (ArrayFS) aFS.getFeatureValue(linkFeature);
if (array != null) {
for (FeatureStructure link : array.toArray()) {
LinkWithRoleModel m = new LinkWithRoleModel();
m.role = link.getStringValue(roleFeat);
m.targetAddr = WebAnnoCasUtil.getAddr(link.getFeatureValue(targetFeat));
m.label = ((AnnotationFS) link.getFeatureValue(targetFeat)).getCoveredText();
links.add(m);
}
}
return (T) links;
}
use of org.apache.uima.cas.Feature in project webanno by webanno.
the class TypeAdapter_ImplBase method getValue.
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;
}
Aggregations