use of org.apache.uima.cas.Feature in project webanno by webanno.
the class WebannoTsv3Writer method setSpanAnnotation.
private void setSpanAnnotation(JCas aJCas) {
int i = 0;
// store slot targets for each slot features
for (String l : spanLayers) {
Type type = getType(aJCas.getCas(), l);
for (Feature f : type.getFeatures()) {
if (slotFeatures != null && slotFeatures.contains(f.getName())) {
slotFeatureTypes.put(f, getType(aJCas.getCas(), slotTargets.get(i)));
i++;
}
}
}
for (String l : spanLayers) {
if (l.equals(Token.class.getName())) {
continue;
}
Map<AnnotationUnit, List<List<String>>> annotationsPertype;
if (annotationsPerPostion.get(l) == null) {
annotationsPertype = new HashMap<>();
} else {
annotationsPertype = annotationsPerPostion.get(l);
}
Type type = getType(aJCas.getCas(), l);
for (AnnotationFS fs : CasUtil.select(aJCas.getCas(), type)) {
AnnotationUnit unit = new AnnotationUnit(fs.getBegin(), fs.getEnd(), false, fs.getCoveredText());
// annotation is per Token
if (units.contains(unit)) {
setSpanAnnoPerFeature(annotationsPertype, type, fs, unit, false, false);
} else // Annotation is on sub-token or multiple tokens
{
SubTokenAnno sta = new SubTokenAnno();
sta.setBegin(fs.getBegin());
sta.setEnd(fs.getEnd());
sta.setText(fs.getCoveredText());
boolean isMultiToken = isMultiToken(fs);
boolean isFirst = true;
Set<AnnotationUnit> sus = new LinkedHashSet<>();
for (AnnotationUnit newUnit : getSubUnits(sta, sus)) {
setSpanAnnoPerFeature(annotationsPertype, type, fs, newUnit, isMultiToken, isFirst);
isFirst = false;
}
}
}
if (annotationsPertype.keySet().size() > 0) {
annotationsPerPostion.put(l, annotationsPertype);
}
}
}
use of org.apache.uima.cas.Feature in project webanno by webanno.
the class WebannoTsv3Writer method setChainAnnotation.
private void setChainAnnotation(JCas aJCas) {
for (String l : chainLayers) {
if (l.equals(Token.class.getName())) {
continue;
}
Map<AnnotationUnit, List<List<String>>> annotationsPertype = null;
Type type = getType(aJCas.getCas(), l + CHAIN);
Feature chainFirst = type.getFeatureByBaseName(FIRST);
int chainNo = 1;
for (FeatureStructure chainFs : selectFS(aJCas.getCas(), type)) {
AnnotationFS linkFs = (AnnotationFS) chainFs.getFeatureValue(chainFirst);
AnnotationUnit unit = getUnit(linkFs.getBegin(), linkFs.getEnd(), linkFs.getCoveredText());
Type lType = linkFs.getType();
// this is the layer with annotations
l = lType.getName();
if (annotationsPerPostion.get(l) == null) {
annotationsPertype = new HashMap<>();
} else {
annotationsPertype = annotationsPerPostion.get(l);
}
Feature linkNext = linkFs.getType().getFeatureByBaseName(NEXT);
int linkNo = 1;
while (linkFs != null) {
AnnotationFS nextLinkFs = (AnnotationFS) linkFs.getFeatureValue(linkNext);
if (nextLinkFs != null) {
addChinFeatureAnno(annotationsPertype, lType, linkFs, unit, linkNo, chainNo);
} else {
addChinFeatureAnno(annotationsPertype, lType, linkFs, unit, linkNo, chainNo);
}
linkFs = nextLinkFs;
linkNo++;
if (nextLinkFs != null) {
unit = getUnit(linkFs.getBegin(), linkFs.getEnd(), linkFs.getCoveredText());
}
}
if (annotationsPertype.keySet().size() > 0) {
annotationsPerPostion.put(l, annotationsPertype);
}
chainNo++;
}
}
}
use of org.apache.uima.cas.Feature in project webanno by webanno.
the class Tsv3XDeserializer method setPrimitiveValue.
private void setPrimitiveValue(TsvColumn aCol, AnnotationFS aAnnotation, String aValue) {
// after determining whether the values is a null value.
if (!NULL_VALUE.equals(aValue)) {
String value = Escaping.unescapeValue(aValue);
Feature feat = aAnnotation.getType().getFeatureByBaseName(aCol.uimaFeature.getShortName());
if (feat == null) {
throw new IllegalArgumentException("CAS type [" + aAnnotation.getType() + "] does not have a feature called [" + aCol.uimaFeature.getShortName() + "]");
}
aAnnotation.setFeatureValueFromString(feat, value);
}
}
use of org.apache.uima.cas.Feature in project webanno by webanno.
the class WebannoTsv2Reader method createRelationLayer.
/**
* Creates a relation layer. For every token, store the governor positions and the dependent
* annotation
*/
private void createRelationLayer(JCas aJcas, Map<Type, Type> relationayers, Map<Type, Map<String, List<AnnotationFS>>> tokenAnnotations, Map<Type, Map<String, List<String>>> relationTargets) {
for (Type layer : relationayers.keySet()) {
if (relationTargets.get(layer) == null) {
continue;
}
Feature dependentFeature = layer.getFeatureByBaseName("Dependent");
Feature governorFeature = layer.getFeatureByBaseName("Governor");
Map<String, List<String>> tokenIdMaps = relationTargets.get(layer);
Map<String, List<AnnotationFS>> tokenAnnos = tokenAnnotations.get(relationayers.get(layer));
Map<String, List<AnnotationFS>> relationAnnos = tokenAnnotations.get(layer);
for (String dependnetId : tokenIdMaps.keySet()) {
int i = 0;
for (String governorId : tokenIdMaps.get(dependnetId)) {
AnnotationFS relationAnno = relationAnnos.get(dependnetId).get(i);
AnnotationFS dependentAnno = tokenAnnos.get(dependnetId).get(0);
AnnotationFS governorAnno = tokenAnnos.get(governorId).get(0);
if (layer.getName().equals(Dependency.class.getName())) {
Type tokenType = getType(aJcas.getCas(), Token.class.getName());
Feature attachFeature = tokenType.getFeatureByBaseName("pos");
AnnotationFS posDependentAnno = dependentAnno;
dependentAnno = CasUtil.selectCovered(aJcas.getCas(), tokenType, dependentAnno.getBegin(), dependentAnno.getEnd()).get(0);
dependentAnno.setFeatureValue(attachFeature, posDependentAnno);
AnnotationFS posGovernorAnno = governorAnno;
governorAnno = CasUtil.selectCovered(aJcas.getCas(), tokenType, governorAnno.getBegin(), governorAnno.getEnd()).get(0);
governorAnno.setFeatureValue(attachFeature, posGovernorAnno);
}
// update begin/end of relation annotation
if (dependentAnno.getEnd() <= governorAnno.getEnd()) {
((Annotation) relationAnno).setBegin(dependentAnno.getBegin());
((Annotation) relationAnno).setEnd(governorAnno.getEnd());
} else {
((Annotation) relationAnno).setBegin(governorAnno.getBegin());
((Annotation) relationAnno).setEnd(dependentAnno.getEnd());
}
relationAnno.setFeatureValue(dependentFeature, dependentAnno);
relationAnno.setFeatureValue(governorFeature, governorAnno);
i++;
}
}
}
}
use of org.apache.uima.cas.Feature in project webanno by webanno.
the class WebannoTsv2Reader method getLayerAndFeature.
private int getLayerAndFeature(JCas aJcas, int columns, Map<Type, Set<Feature>> spanLayers, Map<Type, Type> relationayers, String line) throws IOException {
StringTokenizer headerTk = new StringTokenizer(line, "#");
while (headerTk.hasMoreTokens()) {
String layerNames = headerTk.nextToken().trim();
StringTokenizer layerTk = new StringTokenizer(layerNames, "|");
Set<Feature> features = new LinkedHashSet<>();
String layerName = layerTk.nextToken().trim();
Iterator<Type> types = aJcas.getTypeSystem().getTypeIterator();
boolean layerExists = false;
while (types.hasNext()) {
if (types.next().getName().equals(layerName)) {
layerExists = true;
break;
}
}
if (!layerExists) {
throw new IOException(fileName + " This is not a valid TSV File. The layer " + layerName + " is not created in the project.");
}
Type layer = CasUtil.getType(aJcas.getCas(), layerName);
while (layerTk.hasMoreTokens()) {
String ft = layerTk.nextToken().trim();
if (ft.startsWith("AttachTo=")) {
Type attachLayer = CasUtil.getType(aJcas.getCas(), ft.substring(9));
relationayers.put(layer, attachLayer);
columns++;
continue;
}
Feature feature = layer.getFeatureByBaseName(ft);
if (feature == null) {
throw new IOException(fileName + " This is not a valid TSV File. The feature " + ft + " is not created for the layer " + layerName);
}
features.add(feature);
columns++;
}
spanLayers.put(layer, features);
}
return columns;
}
Aggregations