use of de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Lemma in project webanno by webanno.
the class WebannoTsv3Reader method addAnnotations.
/**
* Importing span annotations including slot annotations.
*/
private void addAnnotations(JCas aJCas, Map<Type, Map<AnnotationUnit, List<AnnotationFS>>> aAnnosPerTypePerUnit) {
for (Type type : annotationsPerPostion.keySet()) {
Map<AnnotationUnit, Map<Integer, AnnotationFS>> multiTokUnits = new HashMap<>();
int ref = 1;
// to see if it is on multiple token
AnnotationFS prevAnnoFs = null;
for (AnnotationUnit unit : annotationsPerPostion.get(type).keySet()) {
int end = unit.end;
List<AnnotationFS> annos = aAnnosPerTypePerUnit.get(type).get(unit);
int j = 0;
Feature linkeF = null;
Map<AnnotationFS, List<FeatureStructure>> linkFSesPerSlotAnno = new HashMap<>();
if (allLayers.get(type).size() == 0) {
ref = addAnnotationWithNoFeature(aJCas, type, unit, annos, multiTokUnits, end, ref);
continue;
}
for (Feature feat : allLayers.get(type)) {
String anno = annotationsPerPostion.get(type).get(unit).get(j);
if (!anno.equals("_")) {
int i = 0;
// if it is a slot annotation (multiple slots per
// single annotation
// (Target1<--role1--Base--role2-->Target2)
int slot = 0;
boolean targetAdd = false;
String stackedAnnoRegex = "(?<!\\\\)" + Pattern.quote("|");
String[] stackedAnnos = anno.split(stackedAnnoRegex);
for (String mAnnos : stackedAnnos) {
String multipleSlotAnno = "(?<!\\\\)" + Pattern.quote(";");
for (String mAnno : mAnnos.split(multipleSlotAnno)) {
String depRef = "";
String multSpliter = "(?<!\\\\)" + Pattern.quote("[");
// is this slot target ambiguous?
boolean ambigTarget = false;
if (mAnno.split(multSpliter).length > 1) {
ambigTarget = true;
depRef = mAnno.substring(mAnno.indexOf("[") + 1, mAnno.length() - 1);
ref = depRef.contains("_") ? ref : Integer.valueOf(mAnno.substring(mAnno.indexOf("[") + 1, mAnno.length() - 1));
mAnno = mAnno.substring(0, mAnno.indexOf("["));
}
if (mAnno.equals("*")) {
mAnno = null;
}
boolean isMultitoken = false;
if (!multiTokUnits.isEmpty() && prevAnnoFs != null && prevAnnoFs.getBegin() != unit.begin) {
contAnno: for (AnnotationUnit u : multiTokUnits.keySet()) {
for (Integer r : multiTokUnits.get(u).keySet()) {
if (ref == r) {
isMultitoken = true;
prevAnnoFs = multiTokUnits.get(u).get(r);
break contAnno;
}
}
}
}
if (isMultitoken) {
Feature endF = type.getFeatureByBaseName(CAS.FEATURE_BASE_NAME_END);
prevAnnoFs.setIntValue(endF, end);
mAnno = getEscapeChars(mAnno);
prevAnnoFs.setFeatureValueFromString(feat, mAnno);
if (feat.getShortName().equals(REF_LINK)) {
// since REF_REL do not start with BIO,
// update it it...
annos.set(i, prevAnnoFs);
}
setAnnoRefPerUnit(unit, type, ref, prevAnnoFs);
} else {
if (roleLinks.containsKey(feat)) {
linkeF = feat;
FeatureStructure link = aJCas.getCas().createFS(slotLinkTypes.get(feat));
Feature roleFeat = link.getType().getFeatureByBaseName("role");
mAnno = getEscapeChars(mAnno);
link.setStringValue(roleFeat, mAnno);
linkFSesPerSlotAnno.putIfAbsent(annos.get(i), new ArrayList<>());
linkFSesPerSlotAnno.get(annos.get(i)).add(link);
} else if (roleTargets.containsKey(feat)) {
FeatureStructure link = linkFSesPerSlotAnno.get(annos.get(i)).get(slot);
int customTypeNumber = 0;
if (mAnno.split("-").length > 2) {
customTypeNumber = Integer.valueOf(mAnno.substring(mAnno.lastIndexOf("-") + 1));
mAnno = mAnno.substring(0, mAnno.lastIndexOf("-"));
}
AnnotationUnit targetUnit = token2Units.get(mAnno);
Type tType = null;
if (customTypeNumber == 0) {
tType = roleTargets.get(feat);
} else {
tType = layerMaps.get(customTypeNumber);
}
AnnotationFS targetFs;
if (ambigTarget) {
targetFs = annosPerRef.get(tType).get(targetUnit).get(ref);
} else {
targetFs = annosPerRef.get(tType).get(targetUnit).entrySet().iterator().next().getValue();
}
link.setFeatureValue(feat, targetFs);
addSlotAnnotations(linkFSesPerSlotAnno, linkeF);
targetAdd = true;
slot++;
} else if (feat.getShortName().equals(REF_REL)) {
int chainNo = Integer.valueOf(mAnno.split("->")[1].split("-")[0]);
int LinkNo = Integer.valueOf(mAnno.split("->")[1].split("-")[1]);
chainAnnosPerTyep.putIfAbsent(type, new TreeMap<>());
if (chainAnnosPerTyep.get(type).get(chainNo) != null && chainAnnosPerTyep.get(type).get(chainNo).get(LinkNo) != null) {
continue;
}
String refRel = mAnno.split("->")[0];
refRel = getEscapeChars(refRel);
if (refRel.equals("*")) {
refRel = null;
}
annos.get(i).setFeatureValueFromString(feat, refRel);
chainAnnosPerTyep.putIfAbsent(type, new TreeMap<>());
chainAnnosPerTyep.get(type).putIfAbsent(chainNo, new TreeMap<>());
chainAnnosPerTyep.get(type).get(chainNo).put(LinkNo, annos.get(i));
} else if (feat.getShortName().equals(REF_LINK)) {
mAnno = getEscapeChars(mAnno);
annos.get(i).setFeatureValueFromString(feat, mAnno);
aJCas.addFsToIndexes(annos.get(i));
} else if (depFeatures.get(type) != null && depFeatures.get(type).equals(feat)) {
int g = depRef.isEmpty() ? 0 : Integer.valueOf(depRef.split("_")[0]);
int d = depRef.isEmpty() ? 0 : Integer.valueOf(depRef.split("_")[1]);
Type depType = depTypess.get(type);
AnnotationUnit govUnit = token2Units.get(mAnno);
int l = annotationsPerPostion.get(type).get(unit).size();
String thisUnit = annotationsPerPostion.get(type).get(unit).get(l - 1);
AnnotationUnit depUnit = token2Units.get(thisUnit);
AnnotationFS govFs;
AnnotationFS depFs;
if (depType.getName().equals(POS.class.getName())) {
depType = aJCas.getCas().getTypeSystem().getType(Token.class.getName());
govFs = units2Tokens.get(govUnit);
depFs = units2Tokens.get(unit);
} else // in WebAnno world :)(!
if (depType.getName().equals(Token.class.getName())) {
govFs = units2Tokens.get(govUnit);
depFs = units2Tokens.get(unit);
} else if (g == 0 && d == 0) {
govFs = annosPerRef.get(depType).get(govUnit).entrySet().iterator().next().getValue();
depFs = annosPerRef.get(depType).get(depUnit).entrySet().iterator().next().getValue();
} else if (g == 0) {
govFs = annosPerRef.get(depType).get(govUnit).entrySet().iterator().next().getValue();
depFs = annosPerRef.get(depType).get(depUnit).get(d);
} else {
govFs = annosPerRef.get(depType).get(govUnit).get(g);
depFs = annosPerRef.get(depType).get(depUnit).entrySet().iterator().next().getValue();
}
annos.get(i).setFeatureValue(feat, depFs);
annos.get(i).setFeatureValue(type.getFeatureByBaseName(GOVERNOR), govFs);
if (depFs.getBegin() <= annos.get(i).getBegin()) {
Feature beginF = type.getFeatureByBaseName(CAS.FEATURE_BASE_NAME_BEGIN);
annos.get(i).setIntValue(beginF, depFs.getBegin());
} else {
Feature endF = type.getFeatureByBaseName(CAS.FEATURE_BASE_NAME_END);
annos.get(i).setIntValue(endF, depFs.getEnd());
}
aJCas.addFsToIndexes(annos.get(i));
} else {
mAnno = getEscapeChars(mAnno);
multiTokUnits.putIfAbsent(unit, new HashMap<>());
multiTokUnits.get(unit).put(ref, annos.get(i));
prevAnnoFs = annos.get(i);
annos.get(i).setFeatureValueFromString(feat, mAnno);
aJCas.addFsToIndexes(annos.get(i));
setAnnoRefPerUnit(unit, type, ref, annos.get(i));
}
}
if (stackedAnnos.length > 1) {
ref++;
}
}
if (type.getName().equals(POS.class.getName())) {
units2Tokens.get(unit).setPos((POS) annos.get(i));
}
if (type.getName().equals(Lemma.class.getName())) {
units2Tokens.get(unit).setLemma((Lemma) annos.get(i));
}
if (type.getName().equals(Stem.class.getName())) {
units2Tokens.get(unit).setStem((Stem) annos.get(i));
}
if (type.getName().equals(MorphologicalFeatures.class.getName())) {
units2Tokens.get(unit).setMorph((MorphologicalFeatures) annos.get(i));
}
i++;
}
if (targetAdd) {
linkFSesPerSlotAnno = new HashMap<>();
}
} else {
prevAnnoFs = null;
}
j++;
}
if (prevAnnoFs != null) {
ref++;
}
}
annosPerRef.put(type, multiTokUnits);
}
}
use of de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Lemma in project webanno by webanno.
the class WebannoTsv2Reader method createSpanAnnotation.
private void createSpanAnnotation(JCas aJcas, int aTokenStart, Map<Type, Set<Feature>> aLayers, Map<Type, Type> aRelationayers, Map<Type, Map<Integer, AnnotationFS>> aAnnotations, Map<Type, Map<Integer, String>> aBeginEndAnno, Map<Type, Map<String, List<AnnotationFS>>> aTokenAnnotations, Map<Type, Map<String, List<String>>> aRelationTargets, StringTokenizer lineTk, String aToken, String aTokenNumberColumn) {
for (Type layer : aLayers.keySet()) {
int lastIndex = 1;
// if a layer is bound to a single token but has multiple feature
// annotation is created once and feature values be appended
Map<Integer, AnnotationFS> singleTokenMultiFeature = new HashMap<>();
// The relation line number should be read once all feature columns
// are obtained
int numberOfFeaturesPerLayer = aLayers.get(layer).size();
for (Feature feature : aLayers.get(layer)) {
numberOfFeaturesPerLayer--;
int index = 1;
String multipleAnnotations = lineTk.nextToken();
String relationTargetNumbers = null;
if (aRelationayers.containsKey(layer) && numberOfFeaturesPerLayer == 0) {
relationTargetNumbers = lineTk.nextToken();
}
int i = 0;
String[] relationTargets = null;
if (relationTargetNumbers != null) {
relationTargets = relationTargetNumbers.split("\\|");
}
for (String annotation : multipleAnnotations.split("\\|")) {
// If annotation is not on multpile spans
if (!(annotation.startsWith("B-") || annotation.startsWith("I-") || annotation.startsWith("O-")) && !(annotation.equals("_") || annotation.equals("O"))) {
AnnotationFS newAnnotation;
// annotation only once
if (singleTokenMultiFeature.get(index) == null) {
newAnnotation = aJcas.getCas().createAnnotation(layer, aTokenStart, aTokenStart + aToken.length());
singleTokenMultiFeature.put(index, newAnnotation);
} else {
newAnnotation = singleTokenMultiFeature.get(index);
}
// stripped out - make it null
if (annotation.startsWith(layer.getName())) {
annotation = null;
}
newAnnotation.setFeatureValueFromString(feature, annotation);
aJcas.addFsToIndexes(newAnnotation);
// Set the POS to the token
if (layer.getName().equals(POS.class.getName())) {
indexedTokens.get(aTokenStart + "-" + aTokenStart + aToken.length()).setPos((POS) newAnnotation);
}
// Set the Lemma to the token
if (layer.getName().equals(Lemma.class.getName())) {
indexedTokens.get(aTokenStart + "-" + aTokenStart + aToken.length()).setLemma((Lemma) newAnnotation);
}
if (aRelationayers.containsKey(layer) && numberOfFeaturesPerLayer == 0) {
Map<String, List<String>> targets = aRelationTargets.get(layer);
if (targets == null) {
List<String> governors = new ArrayList<>();
governors.add(relationTargets[i]);
targets = new HashMap<>();
targets.put(aTokenNumberColumn, governors);
i++;
aRelationTargets.put(layer, targets);
} else {
List<String> governors = targets.get(aTokenNumberColumn);
if (governors == null) {
governors = new ArrayList<>();
}
governors.add(relationTargets[i]);
targets.put(aTokenNumberColumn, governors);
i++;
aRelationTargets.put(layer, targets);
}
}
Map<String, List<AnnotationFS>> tokenAnnotations = aTokenAnnotations.get(layer);
if (tokenAnnotations == null) {
tokenAnnotations = new HashMap<>();
}
List<AnnotationFS> relAnnos = tokenAnnotations.get(aTokenNumberColumn);
if (relAnnos == null) {
relAnnos = new ArrayList<>();
}
relAnnos.add(newAnnotation);
tokenAnnotations.put(aTokenNumberColumn, relAnnos);
aTokenAnnotations.put(layer, tokenAnnotations);
index++;
} else // O-_ is a position marker
if (annotation.equals("O-_") || annotation.equals("B-_") || annotation.equals("I-_")) {
index++;
} else if (annotation.startsWith("B-")) {
boolean isNewAnnotation = true;
Map<Integer, AnnotationFS> indexedAnnos = aAnnotations.get(layer);
Map<Integer, String> indexedBeginEndAnnos = aBeginEndAnno.get(layer);
AnnotationFS newAnnotation;
if (indexedAnnos == null) {
newAnnotation = aJcas.getCas().createAnnotation(layer, aTokenStart, aTokenStart + aToken.length());
indexedAnnos = new LinkedHashMap<>();
indexedBeginEndAnnos = new LinkedHashMap<>();
} else if (indexedAnnos.get(index) == null) {
newAnnotation = aJcas.getCas().createAnnotation(layer, aTokenStart, aTokenStart + aToken.length());
} else if (indexedAnnos.get(index) != null && indexedBeginEndAnnos.get(index).equals("E-")) {
newAnnotation = aJcas.getCas().createAnnotation(layer, aTokenStart, aTokenStart + aToken.length());
} else // annotation
if (indexedBeginEndAnnos.get(index).equals("I-")) {
newAnnotation = aJcas.getCas().createAnnotation(layer, aTokenStart, aTokenStart + aToken.length());
} else {
newAnnotation = indexedAnnos.get(index);
isNewAnnotation = false;
}
// remove prefixes such as B-/I- before creating the
// annotation
annotation = (annotation.substring(2));
if (annotation.startsWith(layer.getName())) {
annotation = null;
}
newAnnotation.setFeatureValueFromString(feature, annotation);
aJcas.addFsToIndexes(newAnnotation);
indexedAnnos.put(index, newAnnotation);
indexedBeginEndAnnos.put(index, "B-");
aAnnotations.put(layer, indexedAnnos);
if (aRelationayers.containsKey(layer)) {
Map<String, List<String>> targets = aRelationTargets.get(layer);
if (targets == null) {
List<String> governors = new ArrayList<>();
governors.add(relationTargets[i]);
targets = new HashMap<>();
targets.put(aTokenNumberColumn, governors);
i++;
aRelationTargets.put(layer, targets);
} else {
List<String> governors = targets.get(aTokenNumberColumn);
if (governors == null) {
governors = new ArrayList<>();
}
governors.add(relationTargets[i]);
targets.put(aTokenNumberColumn, governors);
i++;
aRelationTargets.put(layer, targets);
}
}
Map<String, List<AnnotationFS>> tokenAnnotations = aTokenAnnotations.get(layer);
if (isNewAnnotation) {
if (tokenAnnotations == null) {
tokenAnnotations = new HashMap<>();
}
List<AnnotationFS> relAnnos = tokenAnnotations.get(aTokenNumberColumn);
if (relAnnos == null) {
relAnnos = new ArrayList<>();
}
relAnnos.add(newAnnotation);
tokenAnnotations.put(aTokenNumberColumn, relAnnos);
aTokenAnnotations.put(layer, tokenAnnotations);
}
aBeginEndAnno.put(layer, indexedBeginEndAnnos);
index++;
} else if (annotation.startsWith("I-")) {
// beginEndAnnotation.put(layer, "I-");
Map<Integer, String> indexedBeginEndAnnos = aBeginEndAnno.get(layer);
indexedBeginEndAnnos.put(index, "I-");
aBeginEndAnno.put(layer, indexedBeginEndAnnos);
Map<Integer, AnnotationFS> indexedAnnos = aAnnotations.get(layer);
AnnotationFS newAnnotation = indexedAnnos.get(index);
((Annotation) newAnnotation).setEnd(aTokenStart + aToken.length());
index++;
} else {
aAnnotations.put(layer, null);
index++;
}
}
lastIndex = index - 1;
}
// tokens annotated as B-X B-X, no B-I means it is end by itself
for (int i = 1; i <= lastIndex; i++) {
if (aBeginEndAnno.get(layer) != null && aBeginEndAnno.get(layer).get(i) != null && aBeginEndAnno.get(layer).get(i).equals("B-")) {
aBeginEndAnno.get(layer).put(i, "E-");
}
}
}
}
use of de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Lemma in project webanno by webanno.
the class RemoveZeroSizeTokensAndSentencesRepair method repair.
@Override
public void repair(Project aProject, CAS aCas, List<LogMessage> aMessages) {
try {
for (Sentence s : select(aCas.getJCas(), Sentence.class)) {
if (s.getBegin() >= s.getEnd()) {
s.removeFromIndexes();
aMessages.add(new LogMessage(this, LogLevel.INFO, "Removed sentence with illegal span: %s", s));
}
}
for (Token t : select(aCas.getJCas(), Token.class)) {
if (t.getBegin() >= t.getEnd()) {
Lemma lemma = t.getLemma();
if (lemma != null) {
lemma.removeFromIndexes();
aMessages.add(new LogMessage(this, LogLevel.INFO, "Removed lemma attached to token with illegal span: %s", t));
}
POS pos = t.getPos();
if (pos != null) {
pos.removeFromIndexes();
aMessages.add(new LogMessage(this, LogLevel.INFO, "Removed POS attached to token with illegal span: %s", t));
}
Stem stem = t.getStem();
if (stem != null) {
stem.removeFromIndexes();
aMessages.add(new LogMessage(this, LogLevel.INFO, "Removed stem attached to token with illegal span: %s", t));
}
t.removeFromIndexes();
aMessages.add(new LogMessage(this, LogLevel.INFO, "Removed token with illegal span: %s", t));
}
}
} catch (CASException e) {
log.error("Unabled to access JCas", e);
aMessages.add(new LogMessage(this, LogLevel.ERROR, "Unabled to access JCas", e.getMessage()));
}
}
Aggregations