Skip to content

Commit

Permalink
Add SemanticAnnot
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximPlusov committed Dec 5, 2023
1 parent 7b1be7a commit b6f4c57
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package org.verapdf.wcag.algorithms.entities;

import org.verapdf.wcag.algorithms.entities.enums.SemanticType;
import org.verapdf.wcag.algorithms.entities.content.InfoChunk;
import org.verapdf.wcag.algorithms.entities.geometry.BoundingBox;

public class AnnotationNode extends SemanticNode implements IAnnotation {
public class AnnotationNode extends InfoChunk implements IAnnotation {
private final String annotationType;
private final Integer destinationPageNumber;
private final Integer destinationObjectKeyNumber;

public AnnotationNode(String annotationType, BoundingBox boundingBox, Integer destinationPageNumber,
Integer destinationObjectKeyNumber) {
super(boundingBox, SemanticType.ANNOT, SemanticType.ANNOT);
super(boundingBox);
this.annotationType = annotationType;
this.destinationPageNumber = destinationPageNumber;
this.destinationObjectKeyNumber = destinationObjectKeyNumber;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.verapdf.wcag.algorithms.entities;

public interface IAnnotation extends INode {
import org.verapdf.wcag.algorithms.entities.content.IChunk;

public interface IAnnotation extends IChunk {
String getAnnotationType();

Integer getDestinationPageNumber();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.verapdf.wcag.algorithms.entities;

import org.verapdf.wcag.algorithms.entities.enums.SemanticType;

import java.util.LinkedList;
import java.util.List;

public class SemanticAnnot extends SemanticNode {

protected final List<AnnotationNode> annotationNodes = new LinkedList<>();

public SemanticAnnot(SemanticAnnot annotationNode) {
this.addAnnots(annotationNode.getAnnots());
setSemanticType(SemanticType.ANNOT);
}

public SemanticAnnot(AnnotationNode annotationNode) {
super(annotationNode.getBoundingBox());
this.annotationNodes.add(annotationNode);
setSemanticType(SemanticType.ANNOT);
}

public void addAnnots(List<AnnotationNode> annots) {
this.annotationNodes.addAll(annots);
for (AnnotationNode annotationNode : annots) {
getBoundingBox().union(annotationNode.getBoundingBox());
}
}

public List<AnnotationNode> getAnnots() {
return annotationNodes;
}

@Override
public boolean equals(Object o) {
if (!super.equals(o)) {
return false;
}
if (!(o instanceof SemanticAnnot)) {
return false;
}
SemanticAnnot that = (SemanticAnnot) o;
return this.annotationNodes.equals(that.getAnnots());
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + annotationNodes.size();
for (AnnotationNode annotationNode : annotationNodes) {
result = 31 * result + annotationNode.hashCode();
}
return result;
}

@Override
public String toString() {
StringBuilder result = new StringBuilder("SemanticAnnot{");
result.append("pageNumber=");
result.append(getBoundingBox().getPageNumber());
result.append(", boundingBox=");
result.append(getBoundingBox());
result.append("}");
return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ public void accept(INode node) {
boolean isLeafChild = node.getChildren()
.stream()
.allMatch(child -> ((child instanceof SemanticSpan) ||
(child instanceof SemanticFigure) ||
(child instanceof SemanticFigure) || (child instanceof SemanticAnnot) ||
child.getSemanticType() == null));

acceptSpanParagraphPart(node, isLeafChild);

acceptSemanticAnnot(node);

acceptSemanticImage(node);

checkSemanticSpanChildren(node);
Expand Down Expand Up @@ -105,13 +107,34 @@ private void acceptSemanticImage(INode node) {
}
}

private void acceptSemanticAnnot(INode node) {
SemanticAnnot annot = null;
for (INode child : node.getChildren()) {
INode accumulatedChild = StaticContainers.getAccumulatedNodeMapper().get(child);
if (accumulatedChild instanceof SemanticTextNode) {
if (!((SemanticTextNode)accumulatedChild).isEmpty() && !((SemanticTextNode)accumulatedChild).isSpaceNode()) {
return;
}
} else if (accumulatedChild instanceof SemanticAnnot) {
if (annot == null) {
annot = new SemanticAnnot((SemanticAnnot)accumulatedChild);
} else {
annot.addAnnots(((SemanticAnnot)accumulatedChild).getAnnots());
}
}
}
if (annot != null) {
StaticContainers.getAccumulatedNodeMapper().updateNode(node, annot, 1.0, SemanticType.ANNOT);
}
}

private void acceptSpanParagraphPart(INode node, boolean isLeafChild) {
double probability = 1;
SemanticPart part = null;
MultiBoundingBox boundingBox = new MultiBoundingBox();
for (INode child : node.getChildren()) {
if (child.getSemanticType() == null || SemanticType.isIgnoredStandardType(child.getInitialSemanticType()) ||
(child instanceof IAnnotation)) {
(child instanceof SemanticAnnot)) {
continue;
}
INode accumulatedChild = StaticContainers.getAccumulatedNodeMapper().get(child);
Expand Down Expand Up @@ -143,7 +166,7 @@ private void acceptSpanParagraphPart(INode node, boolean isLeafChild) {
SemanticType semanticType = SemanticType.PART;
INode accumulatedNode = part;
if (part != null && part.getColumns().stream().allMatch(TextColumn::hasOnlyOneBlock)) {
boolean isSpan = SemanticType.SPAN.equals(node.getInitialSemanticType()) &&
boolean isSpan = SemanticType.SPAN.equals(node.getInitialSemanticType()) &&
(isLeafChild || node.getChildren().stream()
.allMatch(AccumulatedNodeConsumer::isAppropriateSpanChild));
if (isSpan) {
Expand All @@ -158,7 +181,7 @@ private void acceptSpanParagraphPart(INode node, boolean isLeafChild) {
}

private static boolean isAppropriateSpanChild(INode child) {
return (child instanceof SemanticSpan) || (child instanceof SemanticFigure) ||
return (child instanceof SemanticSpan) || (child instanceof SemanticFigure) || (child instanceof SemanticAnnot) ||
child.getInitialSemanticType() == SemanticType.LINK ||
child.getSemanticType() == SemanticType.SPAN || child.getSemanticType() == null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,6 @@ public static boolean isNodeInsideTable(INode node, Long id, BoundingBox boundin
if (node.getRecognizedStructureId() == id) {
return true;
}
if (node instanceof SemanticFigure) {
return true;
}
if (node instanceof SemanticTextNode) {
SemanticTextNode textNode = (SemanticTextNode) node;
return isTextNodeInsideTable(textNode, boundingBox);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void accept(INode node) {
boolean isLeafChild = node.getChildren()
.stream()
.allMatch(child -> ((child instanceof SemanticSpan) ||
(child instanceof SemanticFigure) ||
(child instanceof SemanticFigure) || (child instanceof SemanticAnnot) ||
child.getSemanticType() == null));
if (isLeafChild) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void updateBoundingBoxes(ITree tree) {
for (INode node : tree) {
MultiBoundingBox boundingBox = new MultiBoundingBox();
for (INode child : node.getChildren()) {
if (!(child instanceof IAnnotation)) {
if (!(child instanceof SemanticAnnot)) {
boundingBox.union(child.getBoundingBox());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void accept(INode node) {
structElementsNumber++;
MultiBoundingBox boundingBox = new MultiBoundingBox(node.getBoundingBox());
for (INode child : node.getChildren()) {
if (!(child instanceof SemanticFigure) && !(child instanceof IAnnotation)) {
if (!(child instanceof SemanticFigure) && !(child instanceof SemanticAnnot)) {
boundingBox.union(child.getBoundingBox());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,8 @@ private List<TextChunk> getTextChunks(INode node, Integer pageNumber) {
}

private static List<IAnnotation> getInheritorAnnotations(INode node) {
if (node instanceof IAnnotation) {
return Collections.singletonList((IAnnotation)node);
if (node instanceof SemanticAnnot) {
return Collections.singletonList(((SemanticAnnot) node).getAnnots().get(0));
}
List<IAnnotation> annotations = new LinkedList<>();
for (INode child : node.getChildren()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ private static LineArtChunk getLineArtChunk(JsonLineArtChunk jsonNode) {
return new LineArtChunk(new BoundingBox(jsonNode.getPageNumber(), jsonNode.getBoundingBox()), lineChunks);
}

private static AnnotationNode getAnnotationNode(JsonAnnotationNode jsonNode) {
return new AnnotationNode(jsonNode.getAnnotationType(), new BoundingBox(jsonNode.getBoundingBox()),
jsonNode.getDestinationPageNumber(), jsonNode.getDestinationObjectKeyNumber());
private static SemanticAnnot getAnnotationNode(JsonAnnotationNode jsonNode) {
return new SemanticAnnot(new AnnotationNode(jsonNode.getAnnotationType(), new BoundingBox(jsonNode.getBoundingBox()),
jsonNode.getDestinationPageNumber(), jsonNode.getDestinationObjectKeyNumber()));
}

private static LineChunk getLineChunk(JsonLineChunk jsonNode) {
Expand Down
Loading

0 comments on commit b6f4c57

Please sign in to comment.