-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#4772 - Repair to trim whitespace off annotations
- Added repair - No longer force-enable server-side timings in dev mode - Show progress of checks and repairs in task tray
- Loading branch information
Showing
14 changed files
with
862 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...armstadt/ukp/clarin/webanno/diag/checks/AllAnnotationsStartAndEndWithCharactersCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Licensed to the Technische Universität Darmstadt under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The Technische Universität Darmstadt | ||
* licenses this file to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package de.tudarmstadt.ukp.clarin.webanno.diag.checks; | ||
|
||
import static java.lang.String.join; | ||
import static org.apache.commons.lang3.StringUtils.abbreviateMiddle; | ||
import static org.apache.commons.text.StringEscapeUtils.escapeJava; | ||
import static org.apache.uima.fit.util.CasUtil.getType; | ||
import static org.apache.uima.fit.util.CasUtil.select; | ||
import static org.springframework.util.CollectionUtils.isEmpty; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apache.uima.cas.CAS; | ||
import org.apache.uima.cas.Type; | ||
|
||
import de.tudarmstadt.ukp.clarin.webanno.model.Project; | ||
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.TrimUtils; | ||
import de.tudarmstadt.ukp.inception.schema.api.AnnotationSchemaService; | ||
import de.tudarmstadt.ukp.inception.support.logging.LogMessage; | ||
|
||
public class AllAnnotationsStartAndEndWithCharactersCheck | ||
implements Check | ||
{ | ||
private final AnnotationSchemaService annotationService; | ||
|
||
public AllAnnotationsStartAndEndWithCharactersCheck(AnnotationSchemaService aAnnotationService) | ||
{ | ||
annotationService = aAnnotationService; | ||
} | ||
|
||
@Override | ||
public boolean check(Project aProject, CAS aCas, List<LogMessage> aMessages) | ||
{ | ||
if (annotationService == null) { | ||
return true; | ||
} | ||
|
||
var allAnnoLayers = annotationService.listAnnotationLayer(aProject); | ||
if (isEmpty(allAnnoLayers)) { | ||
return true; | ||
} | ||
|
||
boolean ok = true; | ||
for (var layer : allAnnoLayers) { | ||
Type type; | ||
try { | ||
type = getType(aCas, layer.getName()); | ||
} | ||
catch (IllegalArgumentException e) { | ||
// If the type does not exist, the CAS has not been upgraded. In this case, we | ||
// can skip checking the layer because there will be no annotations anyway. | ||
continue; | ||
} | ||
|
||
if (!aCas.getTypeSystem().subsumes(aCas.getAnnotationType(), type)) { | ||
// Skip non-annotation types | ||
continue; | ||
} | ||
|
||
var docText = aCas.getDocumentText(); | ||
for (var ann : select(aCas, type)) { | ||
var offsets = new int[] { ann.getBegin(), ann.getEnd() }; | ||
TrimUtils.trim(docText, offsets); | ||
|
||
boolean startsWithWhitespace = offsets[0] != ann.getBegin(); | ||
boolean endsWithWhitespace = offsets[1] != ann.getEnd(); | ||
if (!startsWithWhitespace && !endsWithWhitespace) { | ||
continue; | ||
} | ||
|
||
var locations = new ArrayList<String>(); | ||
if (startsWithWhitespace) { | ||
locations.add("starts"); | ||
} | ||
if (endsWithWhitespace) { | ||
locations.add("ends"); | ||
} | ||
|
||
aMessages.add(LogMessage.error(this, "[%s] [%s]@[%d-%d] %s with whitespace", | ||
ann.getType().getName(), | ||
escapeJava(abbreviateMiddle(ann.getCoveredText(), "…", 20)), ann.getBegin(), | ||
ann.getEnd(), join(" and ", locations))); | ||
|
||
ok = false; | ||
} | ||
} | ||
|
||
return ok; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...g/src/main/java/de/tudarmstadt/ukp/clarin/webanno/diag/repairs/TrimAnnotationsRepair.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Licensed to the Technische Universität Darmstadt under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The Technische Universität Darmstadt | ||
* licenses this file to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package de.tudarmstadt.ukp.clarin.webanno.diag.repairs; | ||
|
||
import static java.lang.String.join; | ||
import static org.apache.commons.lang3.StringUtils.abbreviateMiddle; | ||
import static org.apache.commons.text.StringEscapeUtils.escapeJava; | ||
import static org.apache.uima.fit.util.CasUtil.getType; | ||
import static org.apache.uima.fit.util.CasUtil.select; | ||
import static org.springframework.util.CollectionUtils.isEmpty; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apache.uima.cas.CAS; | ||
import org.apache.uima.cas.Type; | ||
import org.apache.uima.jcas.tcas.Annotation; | ||
|
||
import de.tudarmstadt.ukp.clarin.webanno.model.Project; | ||
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.TrimUtils; | ||
import de.tudarmstadt.ukp.inception.schema.api.AnnotationSchemaService; | ||
import de.tudarmstadt.ukp.inception.support.logging.LogMessage; | ||
|
||
public class TrimAnnotationsRepair | ||
implements Repair | ||
{ | ||
private final AnnotationSchemaService annotationService; | ||
|
||
public TrimAnnotationsRepair(AnnotationSchemaService aAnnotationService) | ||
{ | ||
annotationService = aAnnotationService; | ||
} | ||
|
||
@Override | ||
public void repair(Project aProject, CAS aCas, List<LogMessage> aMessages) | ||
{ | ||
var allAnnoLayers = annotationService.listAnnotationLayer(aProject); | ||
if (isEmpty(allAnnoLayers)) { | ||
return; | ||
} | ||
|
||
for (var layer : allAnnoLayers) { | ||
Type type; | ||
try { | ||
type = getType(aCas, layer.getName()); | ||
} | ||
catch (IllegalArgumentException e) { | ||
// If the type does not exist, the CAS has not been upgraded. In this case, we | ||
// can skip checking the layer because there will be no annotations anyway. | ||
continue; | ||
} | ||
|
||
if (!aCas.getTypeSystem().subsumes(aCas.getAnnotationType(), type)) { | ||
// Skip non-annotation types | ||
continue; | ||
} | ||
|
||
var docText = aCas.getDocumentText(); | ||
for (var ann : select(aCas, type)) { | ||
var oldBegin = ann.getBegin(); | ||
var oldEnd = ann.getEnd(); | ||
|
||
TrimUtils.trim(docText, (Annotation) ann); | ||
|
||
boolean beginChanged = oldBegin != ann.getBegin(); | ||
boolean endChanged = oldEnd != ann.getEnd(); | ||
if (!beginChanged && !endChanged) { | ||
continue; | ||
} | ||
|
||
var locations = new ArrayList<String>(); | ||
if (beginChanged) { | ||
locations.add("start"); | ||
} | ||
if (endChanged) { | ||
locations.add("end"); | ||
} | ||
|
||
aMessages.add(LogMessage.info(this, "Trimmed whitespace of [%s] [%s]@[%d-%d] at %s", | ||
ann.getType().getName(), | ||
escapeJava(abbreviateMiddle(ann.getCoveredText(), "…", 20)), ann.getBegin(), | ||
ann.getEnd(), join(" and ", locations))); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.