Skip to content

Commit

Permalink
Merge pull request #319 from OCR4all/fix/subtypeRemoval
Browse files Browse the repository at this point in the history
allow saving the removal of subtypes from TextRegion elements
  • Loading branch information
maxnth authored Apr 12, 2022
2 parents 15674e8 + e3530bd commit eba0cc4
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/main/java/de/uniwue/web/io/PageXMLWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,20 @@ private static void mergeElementChangesIntoLayout(PageAnnotations result, PageLa
Region physicalRegion = layout.getRegion(id);
String physicalRegionType = physicalRegion.getType().toString();

if(isTextRegion(elementType) && isTextRegion(physicalRegionType)){
if(isTextRegion(elementType, true) && isTextRegion(physicalRegionType, true)){
TextRegion textRegion = (TextRegion) physicalRegion;
textRegion.setCoords(elementCoords);
String physicalSubtype = textRegion.getTextType();

if(isTextRegionSubtype(elementType)){
String subType = TypeConverter.stringToStringSubType(elementType);
textRegion.setTextType(subType);
}

if(physicalSubtype != null && isTextRegion(elementType, false)){
textRegion.setTextType(null);
}

Map<String, TextLine> physicalTextLines = getTextLines(textRegion);

for(Map.Entry<String, de.uniwue.web.model.TextLine> _textLine : element.getTextlines().entrySet()){
Expand All @@ -256,7 +261,7 @@ private static void mergeElementChangesIntoLayout(PageAnnotations result, PageLa
physicalTextLine.setCoords(textLineCoords);

Map<Integer, String> textLineContent = textLine.getText().entrySet().stream().collect(Collectors.toMap(Entry::getKey, Entry::getValue));

// Sync existing TextContentVariants with content from annotation
for(int i = 0; i < physicalTextLine.getTextContentVariantCount(); i++){
TextContent textContent = physicalTextLine.getTextContentVariant(i);
Expand All @@ -269,15 +274,15 @@ private static void mergeElementChangesIntoLayout(PageAnnotations result, PageLa
textContent.setText(textLineContent.get(textContentIndex));
textLineContent.remove(textContentIndex);
}else{
// If index 0 isn't contained in the annotation but exists physically it was
// If index 0 isn't contained in the annotation but exists physically it was
// discarded in the UI and therefore should set to empty
if(textContentIndex == 0){
textContent.setText("");
}
}
}
}

// Add TextContentVariants which exist in annotation but not physically
for(Integer index : textLineContent.keySet()){
TextContent textContent = physicalTextLine.addTextContentVariant();
Expand Down Expand Up @@ -493,14 +498,17 @@ private static boolean isRegion(String type, boolean includeSubtypes) {
/**
* Returns whether a type is a TextRegion (including subtypes)
* @param type
* @param includeSubtypes
* @return
*/
private static boolean isTextRegion(String type) {
private static boolean isTextRegion(String type, boolean includeSubtypes) {
if(type.equals(RegionType.TextRegion.getName()))
return true;
for (de.uniwue.algorithm.geometry.regions.type.RegionSubType c : de.uniwue.algorithm.geometry.regions.type.RegionSubType.values()) {
if (c.name().equals(type)) {
return true;
if(includeSubtypes) {
for (de.uniwue.algorithm.geometry.regions.type.RegionSubType c : de.uniwue.algorithm.geometry.regions.type.RegionSubType.values()) {
if (c.name().equals(type)) {
return true;
}
}
}
return false;
Expand Down

0 comments on commit eba0cc4

Please sign in to comment.