Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
de266dcfe5 | ||
|
|
10e525f0de | ||
|
|
e0e5e35b30 | ||
|
|
e1d8d1ea3b | ||
|
|
1546c05dd8 | ||
|
|
7c88c30ca7 | ||
|
|
50427d08dc | ||
|
|
338c6c5dd0 |
@ -119,14 +119,18 @@ public class LayoutParsingPipeline {
|
|||||||
log.info("Starting layout parsing for {}", layoutParsingRequest.identifier());
|
log.info("Starting layout parsing for {}", layoutParsingRequest.identifier());
|
||||||
|
|
||||||
File originFile = layoutParsingStorageService.getOriginFile(layoutParsingRequest.originFileStorageId());
|
File originFile = layoutParsingStorageService.getOriginFile(layoutParsingRequest.originFileStorageId());
|
||||||
File viewerDocumentFile = layoutParsingStorageService.getViewerDocFile(layoutParsingRequest.viewerDocumentStorageId()).orElse(originFile);
|
File viewerDocumentFile = layoutParsingStorageService.getViewerDocFile(layoutParsingRequest.viewerDocumentStorageId())
|
||||||
|
.orElse(originFile);
|
||||||
|
|
||||||
VisualLayoutParsingResponse visualLayoutParsingResponse = layoutParsingRequest.visualLayoutParsingFileId()
|
VisualLayoutParsingResponse visualLayoutParsingResponse = layoutParsingRequest.visualLayoutParsingFileId()
|
||||||
.map(layoutParsingStorageService::getVisualLayoutParsingFile).orElse(new VisualLayoutParsingResponse());
|
.map(layoutParsingStorageService::getVisualLayoutParsingFile)
|
||||||
|
.orElse(new VisualLayoutParsingResponse());
|
||||||
ImageServiceResponse imageServiceResponse = layoutParsingRequest.imagesFileStorageId()
|
ImageServiceResponse imageServiceResponse = layoutParsingRequest.imagesFileStorageId()
|
||||||
.map(layoutParsingStorageService::getImagesFile).orElse(new ImageServiceResponse());
|
.map(layoutParsingStorageService::getImagesFile)
|
||||||
|
.orElse(new ImageServiceResponse());
|
||||||
TableServiceResponse tableServiceResponse = layoutParsingRequest.tablesFileStorageId()
|
TableServiceResponse tableServiceResponse = layoutParsingRequest.tablesFileStorageId()
|
||||||
.map(layoutParsingStorageService::getTablesFile).orElse(new TableServiceResponse());
|
.map(layoutParsingStorageService::getTablesFile)
|
||||||
|
.orElse(new TableServiceResponse());
|
||||||
|
|
||||||
ClassificationDocument classificationDocument = parseLayout(settings.getLayoutParsingTypeOverride() == null //
|
ClassificationDocument classificationDocument = parseLayout(settings.getLayoutParsingTypeOverride() == null //
|
||||||
? layoutParsingRequest.layoutParsingType() : settings.getLayoutParsingTypeOverride(),
|
? layoutParsingRequest.layoutParsingType() : settings.getLayoutParsingTypeOverride(),
|
||||||
@ -143,13 +147,20 @@ public class LayoutParsingPipeline {
|
|||||||
|
|
||||||
log.info("Creating viewer document for {}", layoutParsingRequest.identifier());
|
log.info("Creating viewer document for {}", layoutParsingRequest.identifier());
|
||||||
|
|
||||||
layoutGridService.addLayoutGrid(viewerDocumentFile, documentGraph, viewerDocumentFile, false, layoutParsingRequest.visualLayoutParsingFileId().isPresent());
|
layoutGridService.addLayoutGrid(viewerDocumentFile,
|
||||||
|
documentGraph,
|
||||||
|
viewerDocumentFile,
|
||||||
|
false,
|
||||||
|
layoutParsingRequest.visualLayoutParsingFileId()
|
||||||
|
.isPresent());
|
||||||
|
|
||||||
log.info("Storing resulting files for {}", layoutParsingRequest.identifier());
|
log.info("Storing resulting files for {}", layoutParsingRequest.identifier());
|
||||||
|
|
||||||
layoutParsingStorageService.storeDocumentData(layoutParsingRequest, DocumentDataMapper.toDocumentData(documentGraph));
|
layoutParsingStorageService.storeDocumentData(layoutParsingRequest, DocumentDataMapper.toDocumentData(documentGraph));
|
||||||
if (layoutParsingRequest.documentMarkdownFileStorageId().isPresent()) {
|
if (layoutParsingRequest.documentMarkdownFileStorageId()
|
||||||
layoutParsingStorageService.storeMarkdownFile(layoutParsingRequest.documentMarkdownFileStorageId().get(), new MarkdownMapper().toMarkdownContent(documentGraph));
|
.isPresent()) {
|
||||||
|
layoutParsingStorageService.storeMarkdownFile(layoutParsingRequest.documentMarkdownFileStorageId()
|
||||||
|
.get(), new MarkdownMapper().toMarkdownContent(documentGraph));
|
||||||
}
|
}
|
||||||
layoutParsingStorageService.storeSimplifiedText(layoutParsingRequest, simplifiedSectionTextService.toSimplifiedText(documentGraph));
|
layoutParsingStorageService.storeSimplifiedText(layoutParsingRequest, simplifiedSectionTextService.toSimplifiedText(documentGraph));
|
||||||
layoutParsingStorageService.storeViewerDocument(layoutParsingRequest, viewerDocumentFile);
|
layoutParsingStorageService.storeViewerDocument(layoutParsingRequest, viewerDocumentFile);
|
||||||
@ -246,7 +257,7 @@ public class LayoutParsingPipeline {
|
|||||||
OutlineObject lastProcessedOutlineObject = null;
|
OutlineObject lastProcessedOutlineObject = null;
|
||||||
|
|
||||||
// parsing the structure elements could be useful as well
|
// parsing the structure elements could be useful as well
|
||||||
if (layoutParsingType != LayoutParsingType.REDACT_MANAGER_OLD && layoutParsingType != LayoutParsingType.DOCUMINE_OLD) {
|
if (layoutParsingType != LayoutParsingType.REDACT_MANAGER_OLD) {
|
||||||
classificationDocument.setOutlineObjectTree(outlineExtractorService.getOutlineObjectTree(originDocument));
|
classificationDocument.setOutlineObjectTree(outlineExtractorService.getOutlineObjectTree(originDocument));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -324,7 +335,7 @@ public class LayoutParsingPipeline {
|
|||||||
classificationPage.setPageWidth(cropbox.getWidth());
|
classificationPage.setPageWidth(cropbox.getWidth());
|
||||||
classificationPage.setPageHeight(cropbox.getHeight());
|
classificationPage.setPageHeight(cropbox.getHeight());
|
||||||
|
|
||||||
if (layoutParsingType != LayoutParsingType.REDACT_MANAGER_OLD && layoutParsingType != LayoutParsingType.DOCUMINE_OLD) {
|
if (layoutParsingType != LayoutParsingType.REDACT_MANAGER_OLD) {
|
||||||
List<OutlineObject> outlineObjects = classificationDocument.getOutlineObjectTree().getOutlineObjectsPerPage().getOrDefault(pageNumber - 1, new ArrayList<>());
|
List<OutlineObject> outlineObjects = classificationDocument.getOutlineObjectTree().getOutlineObjectsPerPage().getOrDefault(pageNumber - 1, new ArrayList<>());
|
||||||
|
|
||||||
OutlineObject notFoundOutlineObject = null;
|
OutlineObject notFoundOutlineObject = null;
|
||||||
@ -379,6 +390,12 @@ public class LayoutParsingPipeline {
|
|||||||
case CLARIFYND -> clarifyndClassificationService.classifyDocument(classificationDocument);
|
case CLARIFYND -> clarifyndClassificationService.classifyDocument(classificationDocument);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (layoutParsingType.equals(LayoutParsingType.DOCUMINE_OLD)) {
|
||||||
|
for (ClassificationPage page : classificationDocument.getPages()) {
|
||||||
|
docuMineBlockificationService.mergeblocks(page, page.getCleanRulings().withoutTextRulings(), 0, 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
List<TextPageBlock> headlines = classificationDocument.getPages()
|
List<TextPageBlock> headlines = classificationDocument.getPages()
|
||||||
.stream()
|
.stream()
|
||||||
.flatMap(classificationPage -> classificationPage.getTextBlocks()
|
.flatMap(classificationPage -> classificationPage.getTextBlocks()
|
||||||
|
|||||||
@ -133,7 +133,7 @@ public abstract class BoundingBox {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private boolean intersectsX(BoundingBox other, float threshold) {
|
public boolean intersectsX(BoundingBox other, float threshold) {
|
||||||
|
|
||||||
return this.getX() - threshold <= other.getMaxX() && this.getMaxX() + threshold >= other.getX();
|
return this.getX() - threshold <= other.getMaxX() && this.getMaxX() + threshold >= other.getX();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,15 @@
|
|||||||
package com.knecon.fforesight.service.layoutparser.processor.model.graph.nodes;
|
package com.knecon.fforesight.service.layoutparser.processor.model.graph.nodes;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.ClassificationPage;
|
import com.knecon.fforesight.service.layoutparser.processor.model.ClassificationPage;
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.graph.entity.TextEntity;
|
import com.knecon.fforesight.service.layoutparser.processor.model.graph.entity.TextEntity;
|
||||||
|
import com.knecon.fforesight.service.layoutparser.processor.model.graph.textblock.AtomicTextBlock;
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.graph.textblock.TextBlock;
|
import com.knecon.fforesight.service.layoutparser.processor.model.graph.textblock.TextBlock;
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.graph.textblock.TextBlockCollector;
|
import com.knecon.fforesight.service.layoutparser.processor.model.graph.textblock.TextBlockCollector;
|
||||||
|
|
||||||
@ -29,9 +32,8 @@ public class Page {
|
|||||||
Integer height;
|
Integer height;
|
||||||
Integer width;
|
Integer width;
|
||||||
Integer rotation;
|
Integer rotation;
|
||||||
|
|
||||||
@EqualsAndHashCode.Exclude
|
@EqualsAndHashCode.Exclude
|
||||||
List<SemanticNode> mainBody;
|
List<AtomicTextBlock> textBlocksOnPage;
|
||||||
@EqualsAndHashCode.Exclude
|
@EqualsAndHashCode.Exclude
|
||||||
Header header;
|
Header header;
|
||||||
@EqualsAndHashCode.Exclude
|
@EqualsAndHashCode.Exclude
|
||||||
@ -53,20 +55,43 @@ public class Page {
|
|||||||
.width((int) classificationPage.getPageWidth())
|
.width((int) classificationPage.getPageWidth())
|
||||||
.number(classificationPage.getPageNumber())
|
.number(classificationPage.getPageNumber())
|
||||||
.rotation(classificationPage.getRotation())
|
.rotation(classificationPage.getRotation())
|
||||||
.mainBody(new LinkedList<>())
|
.textBlocksOnPage(new LinkedList<>())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs and returns a {@link TextBlock} representing the concatenated text of all leaf semantic nodes in the main body.
|
||||||
|
*
|
||||||
|
* @return The main body text block.
|
||||||
|
*/
|
||||||
public TextBlock getMainBodyTextBlock() {
|
public TextBlock getMainBodyTextBlock() {
|
||||||
|
|
||||||
return mainBody.stream()
|
return textBlocksOnPage.stream()
|
||||||
.filter(SemanticNode::isLeaf)
|
|
||||||
.map(SemanticNode::getLeafTextBlock)
|
|
||||||
.collect(new TextBlockCollector());
|
.collect(new TextBlockCollector());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public List<SemanticNode> getMainBody() {
|
||||||
|
|
||||||
|
return textBlocksOnPage.stream()
|
||||||
|
.map(AtomicTextBlock::getParent)
|
||||||
|
.map(this::getHighestParentOnPage)
|
||||||
|
.distinct()
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private SemanticNode getHighestParentOnPage(SemanticNode node) {
|
||||||
|
|
||||||
|
SemanticNode currentNode = node;
|
||||||
|
while (currentNode.getParent().onlyOnPage(this)) {
|
||||||
|
currentNode = currentNode.getParent();
|
||||||
|
}
|
||||||
|
return currentNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
|
||||||
|
|||||||
@ -74,7 +74,8 @@ public interface SemanticNode {
|
|||||||
|
|
||||||
return getTextBlock().getPages()
|
return getTextBlock().getPages()
|
||||||
.stream()
|
.stream()
|
||||||
.min(Comparator.comparingInt(Page::getNumber)).orElseThrow(() -> new IllegalStateException("SemanticNode has no Page!"));
|
.min(Comparator.comparingInt(Page::getNumber))
|
||||||
|
.orElseThrow(() -> new IllegalStateException("SemanticNode has no Page!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -504,4 +505,17 @@ public interface SemanticNode {
|
|||||||
|
|
||||||
void accept(NodeVisitor visitor);
|
void accept(NodeVisitor visitor);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks wether this SemanticNode appears on a single page only, and if that page is the provided one.
|
||||||
|
*
|
||||||
|
* @param page the page to check
|
||||||
|
* @return true, when SemanticNode is on a single page only and the page is the provided page. Otherwise, false.
|
||||||
|
*/
|
||||||
|
default boolean onlyOnPage(Page page) {
|
||||||
|
|
||||||
|
Set<Page> pages = getPages();
|
||||||
|
return pages.size() == 1 && pages.contains(page);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -2,19 +2,23 @@ package com.knecon.fforesight.service.layoutparser.processor.services.blockifica
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.ListIterator;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import com.knecon.fforesight.service.layoutparser.internal.api.data.redaction.LayoutEngine;
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.AbstractPageBlock;
|
import com.knecon.fforesight.service.layoutparser.processor.model.AbstractPageBlock;
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.ClassificationPage;
|
import com.knecon.fforesight.service.layoutparser.processor.model.ClassificationPage;
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.Orientation;
|
import com.knecon.fforesight.service.layoutparser.processor.model.Orientation;
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.table.CleanRulings;
|
import com.knecon.fforesight.service.layoutparser.processor.model.table.CleanRulings;
|
||||||
|
import com.knecon.fforesight.service.layoutparser.processor.model.table.TablePageBlock;
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.text.TextPageBlock;
|
import com.knecon.fforesight.service.layoutparser.processor.model.text.TextPageBlock;
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.text.TextPositionSequence;
|
import com.knecon.fforesight.service.layoutparser.processor.model.text.TextPositionSequence;
|
||||||
|
|
||||||
|
@SuppressWarnings("all")
|
||||||
@Service
|
@Service
|
||||||
public class DocuMineBlockificationService {
|
public class DocuMineBlockificationService {
|
||||||
|
|
||||||
@ -57,8 +61,11 @@ public class DocuMineBlockificationService {
|
|||||||
boolean isSplitByRuling = prev != null && usedRulings.lineBetween(prev, word);
|
boolean isSplitByRuling = prev != null && usedRulings.lineBetween(prev, word);
|
||||||
boolean splitByDir = prev != null && !prev.getDir().equals(word.getDir());
|
boolean splitByDir = prev != null && !prev.getDir().equals(word.getDir());
|
||||||
boolean splitByOtherFontAndOtherY = prev != null && Math.abs(prev.getMaxYDirAdj() - word.getMaxYDirAdj()) > word.getTextHeight() * 0.2 //
|
boolean splitByOtherFontAndOtherY = prev != null && Math.abs(prev.getMaxYDirAdj() - word.getMaxYDirAdj()) > word.getTextHeight() * 0.2 //
|
||||||
&& (word.getFontStyle().contains("bold") && !prev.getFontStyle().contains("bold") //
|
&& (word.getFontStyle().contains("bold") && !prev.getFontStyle().contains("bold")
|
||||||
|| prev.getFontStyle().contains("bold") && !word.getFontStyle().contains("bold"));
|
//
|
||||||
|
|| prev.getFontStyle().contains("bold") && !word.getFontStyle().contains("bold")
|
||||||
|
|| Math.abs(prev.getFontSize() - word.getFontSize()) >= 1
|
||||||
|
|| Math.abs(word.getTextHeight() - prev.getTextHeight()) > 0.8);
|
||||||
|
|
||||||
Matcher matcher = pattern.matcher(chunkWords.stream()
|
Matcher matcher = pattern.matcher(chunkWords.stream()
|
||||||
.collect(Collectors.joining(" ")).toString());
|
.collect(Collectors.joining(" ")).toString());
|
||||||
@ -120,5 +127,77 @@ public class DocuMineBlockificationService {
|
|||||||
return new ClassificationPage(textPageBlocks);
|
return new ClassificationPage(textPageBlocks);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void mergeblocks(ClassificationPage page, CleanRulings usedRulings, float xThreshold, float yThreshold) {
|
||||||
|
|
||||||
|
var blocks = page.getTextBlocks();
|
||||||
|
ListIterator<AbstractPageBlock> itty = blocks.listIterator();
|
||||||
|
while (itty.hasNext()) {
|
||||||
|
AbstractPageBlock block = itty.next();
|
||||||
|
if (block == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (block instanceof TablePageBlock) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
TextPageBlock current = (TextPageBlock) block;
|
||||||
|
|
||||||
|
for (int i = 0; i < blocks.size(); i++) {
|
||||||
|
|
||||||
|
AbstractPageBlock abstractPageBlock = blocks.get(i);
|
||||||
|
if (abstractPageBlock == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (abstractPageBlock == current) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (abstractPageBlock instanceof TablePageBlock) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isHeadlineFromOutline(current) || isHeadlineFromOutline(abstractPageBlock)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
TextPageBlock inner = (TextPageBlock) abstractPageBlock;
|
||||||
|
|
||||||
|
if (usedRulings.lineBetween(current, blocks.get(i))) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current.getDir() == inner.getDir() && current.intersects(inner, yThreshold, xThreshold) && (current.getClassification() == null || current.getClassification()
|
||||||
|
.equals(inner.getClassification()))) {
|
||||||
|
|
||||||
|
boolean toDuplicate = current.isToDuplicate() || inner.isToDuplicate();
|
||||||
|
current.getSequences().addAll(inner.getSequences());
|
||||||
|
current = buildTextBlock(current.getSequences(), 0);
|
||||||
|
current.setClassification(inner.getClassification());
|
||||||
|
current.setToDuplicate(toDuplicate);
|
||||||
|
blocks.set(i, null);
|
||||||
|
itty.set(current);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var blocksIterator = blocks.iterator();
|
||||||
|
while (blocksIterator.hasNext()) {
|
||||||
|
if (blocksIterator.next() == null) {
|
||||||
|
blocksIterator.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private boolean isHeadlineFromOutline(AbstractPageBlock abstractPageBlock) {
|
||||||
|
|
||||||
|
return abstractPageBlock.getEngines().contains(LayoutEngine.OUTLINE) && abstractPageBlock.getClassification() != null && abstractPageBlock.getClassification().isHeadline();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static TextPageBlock buildTextBlock(List<TextPositionSequence> wordBlockList, int indexOnPage) {
|
||||||
|
|
||||||
|
return new TextPageBlock(wordBlockList);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -74,7 +74,7 @@ public class DocuMineClassificationService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (document.getFontSizeCounter().getMostPopular() == null) {
|
if (document.getFontSizeCounter().getMostPopular() == null) {
|
||||||
textBlock.setClassification(PageBlockType.OTHER);
|
textBlock.setClassification(PageBlockType.PARAGRAPH);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (MarkedContentUtils.intersects(textBlock, page.getMarkedContentBboxPerType(), MarkedContentUtils.HEADER) //
|
if (MarkedContentUtils.intersects(textBlock, page.getMarkedContentBboxPerType(), MarkedContentUtils.HEADER) //
|
||||||
@ -108,7 +108,10 @@ public class DocuMineClassificationService {
|
|||||||
&& Character.isDigit(textBlock.toString().charAt(0))
|
&& Character.isDigit(textBlock.toString().charAt(0))
|
||||||
&& atLeast3Matcher.reset().find()
|
&& atLeast3Matcher.reset().find()
|
||||||
&& !textBlock.toString().contains(":") //
|
&& !textBlock.toString().contains(":") //
|
||||||
|| textBlock.toString().equals(textBlock.toString().toUpperCase(Locale.ROOT)) && atLeast3Matcher.reset().find() && !textBlock.toString().contains(":") //
|
|| textBlock.toString().equals(textBlock.toString().toUpperCase(Locale.ROOT))
|
||||||
|
&& atLeast3Matcher.reset().find()
|
||||||
|
&& !textBlock.toString().contains(":")
|
||||||
|
&& !textBlock.toString().startsWith("(")//
|
||||||
|| textBlock.toString().startsWith("APPENDIX") //
|
|| textBlock.toString().startsWith("APPENDIX") //
|
||||||
|| textBlock.toString().startsWith("FIGURE") //
|
|| textBlock.toString().startsWith("FIGURE") //
|
||||||
|| textBlock.toString().startsWith("Continued TABLE") //
|
|| textBlock.toString().startsWith("Continued TABLE") //
|
||||||
@ -143,9 +146,9 @@ public class DocuMineClassificationService {
|
|||||||
&& PositionUtils.getApproxLineCount(textBlock) < 2.9) {
|
&& PositionUtils.getApproxLineCount(textBlock) < 2.9) {
|
||||||
textBlock.setClassification(PageBlockType.PARAGRAPH_ITALIC);
|
textBlock.setClassification(PageBlockType.PARAGRAPH_ITALIC);
|
||||||
} else if (PositionUtils.isWithinBodyTextFrame(bodyTextFrame, textBlock)) {
|
} else if (PositionUtils.isWithinBodyTextFrame(bodyTextFrame, textBlock)) {
|
||||||
textBlock.setClassification(PageBlockType.PARAGRAPH_UNKNOWN);
|
textBlock.setClassification(PageBlockType.PARAGRAPH);
|
||||||
} else {
|
} else {
|
||||||
textBlock.setClassification(PageBlockType.OTHER);
|
textBlock.setClassification(PageBlockType.PARAGRAPH);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import static java.util.stream.Collectors.toList;
|
|||||||
|
|
||||||
import java.awt.geom.Rectangle2D;
|
import java.awt.geom.Rectangle2D;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -15,6 +16,7 @@ import java.util.Optional;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import com.knecon.fforesight.service.layoutparser.internal.api.data.redaction.NodeType;
|
||||||
import com.knecon.fforesight.service.layoutparser.internal.api.queue.LayoutParsingType;
|
import com.knecon.fforesight.service.layoutparser.internal.api.queue.LayoutParsingType;
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.AbstractPageBlock;
|
import com.knecon.fforesight.service.layoutparser.processor.model.AbstractPageBlock;
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.ClassificationDocument;
|
import com.knecon.fforesight.service.layoutparser.processor.model.ClassificationDocument;
|
||||||
@ -32,7 +34,9 @@ import com.knecon.fforesight.service.layoutparser.processor.model.graph.nodes.He
|
|||||||
import com.knecon.fforesight.service.layoutparser.processor.model.graph.nodes.Image;
|
import com.knecon.fforesight.service.layoutparser.processor.model.graph.nodes.Image;
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.graph.nodes.Page;
|
import com.knecon.fforesight.service.layoutparser.processor.model.graph.nodes.Page;
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.graph.nodes.Paragraph;
|
import com.knecon.fforesight.service.layoutparser.processor.model.graph.nodes.Paragraph;
|
||||||
|
import com.knecon.fforesight.service.layoutparser.processor.model.graph.nodes.SemanticNode;
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.graph.textblock.AtomicTextBlock;
|
import com.knecon.fforesight.service.layoutparser.processor.model.graph.textblock.AtomicTextBlock;
|
||||||
|
import com.knecon.fforesight.service.layoutparser.processor.model.graph.textblock.TextBlock;
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.image.ClassifiedImage;
|
import com.knecon.fforesight.service.layoutparser.processor.model.image.ClassifiedImage;
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.outline.TableOfContentItem;
|
import com.knecon.fforesight.service.layoutparser.processor.model.outline.TableOfContentItem;
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.text.TextPageBlock;
|
import com.knecon.fforesight.service.layoutparser.processor.model.text.TextPageBlock;
|
||||||
@ -68,10 +72,25 @@ public class DocumentGraphFactory {
|
|||||||
documentGraph.setPages(context.pages.keySet());
|
documentGraph.setPages(context.pages.keySet());
|
||||||
documentGraph.setDocumentTree(context.documentTree);
|
documentGraph.setDocumentTree(context.documentTree);
|
||||||
documentGraph.setTextBlock(documentGraph.getTextBlock());
|
documentGraph.setTextBlock(documentGraph.getTextBlock());
|
||||||
|
addTextBlocksToPages(documentGraph);
|
||||||
|
|
||||||
return documentGraph;
|
return documentGraph;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void addTextBlocksToPages(Document documentGraph) {
|
||||||
|
|
||||||
|
documentGraph.streamAllSubNodes()
|
||||||
|
.filter(SemanticNode::isLeaf)
|
||||||
|
.filter(node -> !node.getType().equals(NodeType.HEADER))
|
||||||
|
.filter(node -> !node.getType().equals(NodeType.FOOTER))
|
||||||
|
.map(SemanticNode::getTextBlock)
|
||||||
|
.map(TextBlock::getAtomicTextBlocks)
|
||||||
|
.flatMap(Collection::stream)
|
||||||
|
.forEach(atb -> atb.getPage().getTextBlocksOnPage().add(atb));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private void addSections(LayoutParsingType layoutParsingType, ClassificationDocument classificationDocument, Context context, Document document) {
|
private void addSections(LayoutParsingType layoutParsingType, ClassificationDocument classificationDocument, Context context, Document document) {
|
||||||
|
|
||||||
for (TableOfContentItem tocItem : classificationDocument.getTableOfContents()) {
|
for (TableOfContentItem tocItem : classificationDocument.getTableOfContents()) {
|
||||||
@ -105,8 +124,6 @@ public class DocumentGraphFactory {
|
|||||||
node = Paragraph.builder().documentTree(context.getDocumentTree()).build();
|
node = Paragraph.builder().documentTree(context.getDocumentTree()).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
page.getMainBody().add(node);
|
|
||||||
|
|
||||||
List<TextPageBlock> textBlocks = new ArrayList<>();
|
List<TextPageBlock> textBlocks = new ArrayList<>();
|
||||||
textBlocks.add(originalTextBlock);
|
textBlocks.add(originalTextBlock);
|
||||||
textBlocks.addAll(textBlocksToMerge);
|
textBlocks.addAll(textBlocksToMerge);
|
||||||
@ -141,7 +158,7 @@ public class DocumentGraphFactory {
|
|||||||
|
|
||||||
Rectangle2D position = image.getPosition();
|
Rectangle2D position = image.getPosition();
|
||||||
Page page = context.getPage(image.getPage());
|
Page page = context.getPage(image.getPage());
|
||||||
Image imageNode = Image.builder()
|
return Image.builder()
|
||||||
.id(IdBuilder.buildId(Set.of(page), List.of(position)))
|
.id(IdBuilder.buildId(Set.of(page), List.of(position)))
|
||||||
.imageType(image.getImageType())
|
.imageType(image.getImageType())
|
||||||
.position(position)
|
.position(position)
|
||||||
@ -150,8 +167,6 @@ public class DocumentGraphFactory {
|
|||||||
.representationHash(image.getRepresentation())
|
.representationHash(image.getRepresentation())
|
||||||
.documentTree(context.getDocumentTree())
|
.documentTree(context.getDocumentTree())
|
||||||
.build();
|
.build();
|
||||||
page.getMainBody().add(imageNode);
|
|
||||||
return imageNode;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -51,9 +51,6 @@ public class SectionNodeFactory {
|
|||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<Integer, List<AbstractPageBlock>> blocksPerPage = pageBlocks.stream()
|
|
||||||
.collect(groupingBy(AbstractPageBlock::getPage));
|
|
||||||
|
|
||||||
AbstractSemanticNode section;
|
AbstractSemanticNode section;
|
||||||
boolean containsTablesAndTextBlocks = containsTablesAndTextBlocks(pageBlocks);
|
boolean containsTablesAndTextBlocks = containsTablesAndTextBlocks(pageBlocks);
|
||||||
if (isLeaf && !containsTablesAndTextBlocks) {
|
if (isLeaf && !containsTablesAndTextBlocks) {
|
||||||
@ -63,8 +60,6 @@ public class SectionNodeFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
context.getSections().add(section);
|
context.getSections().add(section);
|
||||||
blocksPerPage.keySet()
|
|
||||||
.forEach(pageNumber -> addSectionNodeToPageNode(context, section, pageNumber));
|
|
||||||
|
|
||||||
section.setTreeId(getTreeId(parentNode, context, section));
|
section.setTreeId(getTreeId(parentNode, context, section));
|
||||||
|
|
||||||
@ -242,10 +237,5 @@ public class SectionNodeFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void addSectionNodeToPageNode(DocumentGraphFactory.Context context, AbstractSemanticNode section, Integer pageNumber) {
|
|
||||||
|
|
||||||
Page page = context.getPage(pageNumber);
|
|
||||||
page.getMainBody().add(section);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -51,8 +51,6 @@ public class TableNodeFactory {
|
|||||||
.numberOfRows(mergedRows.size())
|
.numberOfRows(mergedRows.size())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
pages.forEach(page -> addTableToPage(page, parentNode, table));
|
|
||||||
|
|
||||||
List<Integer> treeId = context.getDocumentTree().createNewChildEntryAndReturnId(parentNode, table);
|
List<Integer> treeId = context.getDocumentTree().createNewChildEntryAndReturnId(parentNode, table);
|
||||||
table.setTreeId(treeId);
|
table.setTreeId(treeId);
|
||||||
addTableCells(layoutParsingType, mergedRows, table, context, document);
|
addTableCells(layoutParsingType, mergedRows, table, context, document);
|
||||||
@ -82,17 +80,6 @@ public class TableNodeFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("PMD.UnusedPrivateMethod") // PMD actually flags this wrong
|
|
||||||
private void addTableToPage(Page page, SemanticNode parentNode, Table table) {
|
|
||||||
|
|
||||||
if (!page.getMainBody().contains(parentNode)) {
|
|
||||||
parentNode.getPages().add(page);
|
|
||||||
}
|
|
||||||
|
|
||||||
page.getMainBody().add(table);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void ifTableHasNoHeadersSetFirstRowAsHeaders(Table table) {
|
private void ifTableHasNoHeadersSetFirstRowAsHeaders(Table table) {
|
||||||
|
|
||||||
if (table.streamHeaders()
|
if (table.streamHeaders()
|
||||||
@ -107,14 +94,7 @@ public class TableNodeFactory {
|
|||||||
|
|
||||||
for (int rowIndex = 0; rowIndex < rows.size(); rowIndex++) {
|
for (int rowIndex = 0; rowIndex < rows.size(); rowIndex++) {
|
||||||
for (int colIndex = 0; colIndex < rows.get(rowIndex).size(); colIndex++) {
|
for (int colIndex = 0; colIndex < rows.get(rowIndex).size(); colIndex++) {
|
||||||
addTableCell(layoutParsingType,
|
addTableCell(layoutParsingType, rows.get(rowIndex).get(colIndex), rowIndex, colIndex, table, context, document);
|
||||||
rows.get(rowIndex)
|
|
||||||
.get(colIndex),
|
|
||||||
rowIndex,
|
|
||||||
colIndex,
|
|
||||||
table,
|
|
||||||
context,
|
|
||||||
document);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -131,14 +111,7 @@ public class TableNodeFactory {
|
|||||||
|
|
||||||
Page page = context.getPage(cell.getPageNumber());
|
Page page = context.getPage(cell.getPageNumber());
|
||||||
|
|
||||||
TableCell tableCell = TableCell.builder()
|
TableCell tableCell = TableCell.builder().documentTree(context.getDocumentTree()).row(rowIndex).col(colIndex).header(cell.isHeaderCell()).bBox(cell.getBBoxPdf()).build();
|
||||||
.documentTree(context.getDocumentTree())
|
|
||||||
.row(rowIndex)
|
|
||||||
.col(colIndex)
|
|
||||||
.header(cell.isHeaderCell())
|
|
||||||
.bBox(cell.getBBoxPdf())
|
|
||||||
.build();
|
|
||||||
page.getMainBody().add(tableCell);
|
|
||||||
|
|
||||||
List<Integer> treeId = context.getDocumentTree().createNewTableChildEntryAndReturnId(tableNode, tableCell);
|
List<Integer> treeId = context.getDocumentTree().createNewTableChildEntryAndReturnId(tableNode, tableCell);
|
||||||
tableCell.setTreeId(treeId);
|
tableCell.setTreeId(treeId);
|
||||||
@ -147,9 +120,7 @@ public class TableNodeFactory {
|
|||||||
if (cell.getTextBlocks().isEmpty()) {
|
if (cell.getTextBlocks().isEmpty()) {
|
||||||
tableCell.setLeafTextBlock(context.getTextBlockFactory().emptyTextBlock(tableNode, context, page));
|
tableCell.setLeafTextBlock(context.getTextBlockFactory().emptyTextBlock(tableNode, context, page));
|
||||||
} else if (cell.getTextBlocks().size() == 1) {
|
} else if (cell.getTextBlocks().size() == 1) {
|
||||||
textBlock = context.getTextBlockFactory()
|
textBlock = context.getTextBlockFactory().buildAtomicTextBlock2(cell.getTextBlocks().get(0).getSequences(), tableCell, context, page);
|
||||||
.buildAtomicTextBlock2(cell.getTextBlocks()
|
|
||||||
.get(0).getSequences(), tableCell, context, page);
|
|
||||||
tableCell.setLeafTextBlock(textBlock);
|
tableCell.setLeafTextBlock(textBlock);
|
||||||
} else if (firstTextBlockIsHeadline(cell)) {
|
} else if (firstTextBlockIsHeadline(cell)) {
|
||||||
SectionNodeFactory.addSection(layoutParsingType,
|
SectionNodeFactory.addSection(layoutParsingType,
|
||||||
@ -181,8 +152,7 @@ public class TableNodeFactory {
|
|||||||
|
|
||||||
private boolean firstTextBlockIsHeadline(Cell cell) {
|
private boolean firstTextBlockIsHeadline(Cell cell) {
|
||||||
|
|
||||||
return cell.getTextBlocks()
|
return cell.getTextBlocks().get(0).isHeadline();
|
||||||
.get(0).isHeadline();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -40,7 +40,7 @@ public class TextBlockFactory {
|
|||||||
orientation = sequences.get(0).getDir().toString();
|
orientation = sequences.get(0).getDir().toString();
|
||||||
textRotation = sequences.get(0).getDir().getRotation();
|
textRotation = sequences.get(0).getDir().getRotation();
|
||||||
}
|
}
|
||||||
return AtomicTextBlock.fromSearchTextWithTextPosition(searchTextWithTextPositionDto.getSearchText(),
|
var atb = AtomicTextBlock.fromSearchTextWithTextPosition(searchTextWithTextPositionDto.getSearchText(),
|
||||||
searchTextWithTextPositionDto.getLineBreaks(),
|
searchTextWithTextPositionDto.getLineBreaks(),
|
||||||
searchTextWithTextPositionDto.getBoldTextBoundaries(),
|
searchTextWithTextPositionDto.getBoldTextBoundaries(),
|
||||||
searchTextWithTextPositionDto.getItalicTextBoundaries(),
|
searchTextWithTextPositionDto.getItalicTextBoundaries(),
|
||||||
@ -53,14 +53,13 @@ public class TextBlockFactory {
|
|||||||
offset,
|
offset,
|
||||||
orientation,
|
orientation,
|
||||||
textRotation);
|
textRotation);
|
||||||
|
return atb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public AtomicTextBlock emptyTextBlock(SemanticNode parent, DocumentGraphFactory.Context context, Page page) {
|
public AtomicTextBlock emptyTextBlock(SemanticNode parent, DocumentGraphFactory.Context context, Page page) {
|
||||||
|
|
||||||
long idx = textBlockIdx;
|
return emptyTextBlock(parent, context.getAndIncrementTextBlockNumberOnPage(page), page);
|
||||||
textBlockIdx++;
|
|
||||||
return AtomicTextBlock.empty(idx, stringOffset, page, context.getAndIncrementTextBlockNumberOnPage(page), parent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -68,7 +67,8 @@ public class TextBlockFactory {
|
|||||||
|
|
||||||
long idx = textBlockIdx;
|
long idx = textBlockIdx;
|
||||||
textBlockIdx++;
|
textBlockIdx++;
|
||||||
return AtomicTextBlock.empty(idx, stringOffset, page, numberOnPage, parent);
|
var atb = AtomicTextBlock.empty(idx, stringOffset, page, numberOnPage, parent);
|
||||||
|
return atb;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -41,7 +41,9 @@ public class DocumentGraphMapper {
|
|||||||
DocumentTree documentTree = new DocumentTree(document);
|
DocumentTree documentTree = new DocumentTree(document);
|
||||||
Context context = new Context(documentData, documentTree);
|
Context context = new Context(documentData, documentTree);
|
||||||
|
|
||||||
context.pages.addAll(Arrays.stream(documentData.getDocumentPages()).map(DocumentGraphMapper::buildPage).toList());
|
context.pages.addAll(Arrays.stream(documentData.getDocumentPages())
|
||||||
|
.map(DocumentGraphMapper::buildPage)
|
||||||
|
.toList());
|
||||||
|
|
||||||
context.documentTree.getRoot().getChildren().addAll(buildEntries(documentData.getDocumentStructure().getRoot().getChildren(), context));
|
context.documentTree.getRoot().getChildren().addAll(buildEntries(documentData.getDocumentStructure().getRoot().getChildren(), context));
|
||||||
|
|
||||||
@ -59,7 +61,9 @@ public class DocumentGraphMapper {
|
|||||||
List<DocumentTree.Entry> newEntries = new LinkedList<>();
|
List<DocumentTree.Entry> newEntries = new LinkedList<>();
|
||||||
for (DocumentStructure.EntryData entryData : entries) {
|
for (DocumentStructure.EntryData entryData : entries) {
|
||||||
|
|
||||||
List<Page> pages = Arrays.stream(entryData.getPageNumbers()).map(pageNumber -> getPage(pageNumber, context)).toList();
|
List<Page> pages = Arrays.stream(entryData.getPageNumbers())
|
||||||
|
.map(pageNumber -> getPage(pageNumber, context))
|
||||||
|
.toList();
|
||||||
|
|
||||||
SemanticNode node = switch (entryData.getType()) {
|
SemanticNode node = switch (entryData.getType()) {
|
||||||
case SECTION -> buildSection(context);
|
case SECTION -> buildSection(context);
|
||||||
@ -77,15 +81,16 @@ public class DocumentGraphMapper {
|
|||||||
if (entryData.getAtomicBlockIds().length > 0) {
|
if (entryData.getAtomicBlockIds().length > 0) {
|
||||||
TextBlock textBlock = toTextBlock(entryData.getAtomicBlockIds(), context, node);
|
TextBlock textBlock = toTextBlock(entryData.getAtomicBlockIds(), context, node);
|
||||||
node.setLeafTextBlock(textBlock);
|
node.setLeafTextBlock(textBlock);
|
||||||
}
|
|
||||||
List<Integer> treeId = Arrays.stream(entryData.getTreeId()).boxed().toList();
|
|
||||||
node.setTreeId(treeId);
|
|
||||||
|
|
||||||
switch (entryData.getType()) {
|
switch (entryData.getType()) {
|
||||||
case HEADER -> pages.forEach(page -> page.setHeader((Header) node));
|
case HEADER -> pages.forEach(page -> page.setHeader((Header) node));
|
||||||
case FOOTER -> pages.forEach(page -> page.setFooter((Footer) node));
|
case FOOTER -> pages.forEach(page -> page.setFooter((Footer) node));
|
||||||
default -> pages.forEach(page -> page.getMainBody().add(node));
|
default -> textBlock.getAtomicTextBlocks()
|
||||||
|
.forEach(atb -> atb.getPage().getTextBlocksOnPage().add(atb));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
List<Integer> treeId = Arrays.stream(entryData.getTreeId()).boxed()
|
||||||
|
.toList();
|
||||||
|
node.setTreeId(treeId);
|
||||||
|
|
||||||
newEntries.add(DocumentTree.Entry.builder().treeId(treeId).children(buildEntries(entryData.getChildren(), context)).node(node).build());
|
newEntries.add(DocumentTree.Entry.builder().treeId(treeId).children(buildEntries(entryData.getChildren(), context)).node(node).build());
|
||||||
}
|
}
|
||||||
@ -142,6 +147,7 @@ public class DocumentGraphMapper {
|
|||||||
return Section.builder().documentTree(context.documentTree).build();
|
return Section.builder().documentTree(context.documentTree).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private SuperSection buildSuperSection(Context context) {
|
private SuperSection buildSuperSection(Context context) {
|
||||||
|
|
||||||
return SuperSection.builder().documentTree(context.documentTree).build();
|
return SuperSection.builder().documentTree(context.documentTree).build();
|
||||||
@ -166,7 +172,9 @@ public class DocumentGraphMapper {
|
|||||||
|
|
||||||
private TextBlock toTextBlock(Long[] atomicTextBlockIds, Context context, SemanticNode parent) {
|
private TextBlock toTextBlock(Long[] atomicTextBlockIds, Context context, SemanticNode parent) {
|
||||||
|
|
||||||
return Arrays.stream(atomicTextBlockIds).map(atomicTextBlockId -> getAtomicTextBlock(context, parent, atomicTextBlockId)).collect(new TextBlockCollector());
|
return Arrays.stream(atomicTextBlockIds)
|
||||||
|
.map(atomicTextBlockId -> getAtomicTextBlock(context, parent, atomicTextBlockId))
|
||||||
|
.collect(new TextBlockCollector());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -181,7 +189,7 @@ public class DocumentGraphMapper {
|
|||||||
|
|
||||||
private Page buildPage(DocumentPage p) {
|
private Page buildPage(DocumentPage p) {
|
||||||
|
|
||||||
return Page.builder().rotation(p.getRotation()).height(p.getHeight()).width(p.getWidth()).number(p.getNumber()).mainBody(new LinkedList<>()).build();
|
return Page.builder().rotation(p.getRotation()).height(p.getHeight()).width(p.getWidth()).number(p.getNumber()).textBlocksOnPage(new LinkedList<>()).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -206,8 +214,10 @@ public class DocumentGraphMapper {
|
|||||||
|
|
||||||
this.documentTree = documentTree;
|
this.documentTree = documentTree;
|
||||||
this.pages = new LinkedList<>();
|
this.pages = new LinkedList<>();
|
||||||
this.documentTextDataBlockData = Arrays.stream(documentData.getDocumentTextData()).toList();
|
this.documentTextDataBlockData = Arrays.stream(documentData.getDocumentTextData())
|
||||||
this.atomicPositionBlockData = Arrays.stream(documentData.getDocumentPositions()).toList();
|
.toList();
|
||||||
|
this.atomicPositionBlockData = Arrays.stream(documentData.getDocumentPositions())
|
||||||
|
.toList();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,9 +1,10 @@
|
|||||||
package com.knecon.fforesight.service.layoutparser.processor.utils;
|
package com.knecon.fforesight.service.layoutparser.processor.utils;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.AbstractPageBlock;
|
import com.knecon.fforesight.service.layoutparser.processor.model.AbstractPageBlock;
|
||||||
import com.knecon.fforesight.service.layoutparser.processor.model.table.Cell;
|
import com.knecon.fforesight.service.layoutparser.processor.model.table.Cell;
|
||||||
@ -22,29 +23,83 @@ public class TableMergingUtility {
|
|||||||
List<TablePageBlock> consecutiveTables = pageBlocks.stream()
|
List<TablePageBlock> consecutiveTables = pageBlocks.stream()
|
||||||
.map(abstractPageBlock -> (TablePageBlock) abstractPageBlock)
|
.map(abstractPageBlock -> (TablePageBlock) abstractPageBlock)
|
||||||
.filter(tablePageBlock -> !tablePageBlock.equals(originalTablePageBlock))
|
.filter(tablePageBlock -> !tablePageBlock.equals(originalTablePageBlock))
|
||||||
|
.sorted(Comparator.comparingInt(TablePageBlock::getPage).thenComparing(TablePageBlock::getY).thenComparing(TablePageBlock::getX))
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
assert consecutiveTables.size() == pageBlocks.size() - 1;
|
assert consecutiveTables.size() == pageBlocks.size() - 1;
|
||||||
|
var currentTable = originalTablePageBlock;
|
||||||
|
int currentTableIndex = 0;
|
||||||
|
|
||||||
List<TablePageBlock> consecutiveTablesWithSameColCountAndHeaders = new LinkedList<>();
|
List<TablePageBlock> consecutiveTablesWithSameColCountAndHeaders = new LinkedList<>();
|
||||||
for (TablePageBlock consecutiveTable : consecutiveTables) {
|
consecutiveTablesWithSameColCountAndHeaders.add(originalTablePageBlock);
|
||||||
if (consecutiveTable.getColCount() == originalTablePageBlock.getColCount() && !hasTableHeader(consecutiveTable) && outerBoundaryAlignsX(originalTablePageBlock,
|
for (int i = 0; i < consecutiveTables.size(); i++) {
|
||||||
consecutiveTable)) {
|
TablePageBlock consecutiveTable = consecutiveTables.get(i);
|
||||||
|
|
||||||
|
if (consecutiveTable.getColCount() == originalTablePageBlock.getColCount() //
|
||||||
|
&& headersMatch(originalTablePageBlock, consecutiveTable) //
|
||||||
|
&& outerBoundaryAlignsX(originalTablePageBlock, consecutiveTable) //
|
||||||
|
&& consecutiveOrSamePage(currentTable, consecutiveTable) //
|
||||||
|
&& !tableBetween(currentTable, consecutiveTable, findTablesBetween(consecutiveTables, currentTableIndex, i))) {
|
||||||
|
|
||||||
|
currentTable = consecutiveTable;
|
||||||
|
currentTableIndex = i;
|
||||||
consecutiveTablesWithSameColCountAndHeaders.add(consecutiveTable);
|
consecutiveTablesWithSameColCountAndHeaders.add(consecutiveTable);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Stream.concat(Stream.of(originalTablePageBlock), consecutiveTablesWithSameColCountAndHeaders.stream()).toList();
|
return consecutiveTablesWithSameColCountAndHeaders;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static List<TablePageBlock> findTablesBetween(List<TablePageBlock> consecutiveTables, int currentTableIndex, int i) {
|
||||||
|
|
||||||
|
if (currentTableIndex + 1 == consecutiveTables.size() || currentTableIndex + 1 >= i) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
return consecutiveTables.subList(currentTableIndex + 1, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static boolean consecutiveOrSamePage(TablePageBlock currentTable, TablePageBlock consecutiveTable) {
|
||||||
|
|
||||||
|
return currentTable.getPage() == consecutiveTable.getPage() || currentTable.getPage() + 1 == consecutiveTable.getPage();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static boolean tableBetween(TablePageBlock currentTable, TablePageBlock consecutiveTable, List<TablePageBlock> tablesBetween) {
|
||||||
|
|
||||||
|
if (tablesBetween.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// assumes the tables are on the same page or on consecutive pages, all tables on pages in between are ignored.
|
||||||
|
return tablesBetween.stream()
|
||||||
|
.filter(tableBetween -> tableBetween.getPage() == currentTable.getPage())
|
||||||
|
.anyMatch(tableBetween -> tableBetween.isBelow(currentTable)) //
|
||||||
|
|| tablesBetween.stream()
|
||||||
|
.filter(tableBetween -> tableBetween.getPage() == consecutiveTable.getPage())
|
||||||
|
.anyMatch(tableBetween -> tableBetween.isAbove(consecutiveTable));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static boolean headersMatch(TablePageBlock originalTable, TablePageBlock consecutiveTable) {
|
||||||
|
|
||||||
|
return getHeaders(consecutiveTable).isEmpty() || getHeaders(originalTable).equals(getHeaders(consecutiveTable));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static boolean outerBoundaryAlignsX(TablePageBlock originalTablePageBlock, TablePageBlock consecutiveTable) {
|
private static boolean outerBoundaryAlignsX(TablePageBlock originalTablePageBlock, TablePageBlock consecutiveTable) {
|
||||||
|
|
||||||
return Math.abs(consecutiveTable.getMinX() - originalTablePageBlock.getMinX()) < TABLE_ALIGNMENT_THRESHOLD && Math.abs(consecutiveTable.getMaxX() - originalTablePageBlock.getMaxX()) < TABLE_ALIGNMENT_THRESHOLD;
|
return Math.abs(consecutiveTable.getMinX() - originalTablePageBlock.getMinX()) < TABLE_ALIGNMENT_THRESHOLD
|
||||||
|
&& Math.abs(consecutiveTable.getMaxX() - originalTablePageBlock.getMaxX()) < TABLE_ALIGNMENT_THRESHOLD;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private boolean hasTableHeader(TablePageBlock table) {
|
private List<Cell> getHeaders(TablePageBlock table) {
|
||||||
|
|
||||||
return table.getRows().stream().flatMap(Collection::stream).anyMatch(Cell::isHeaderCell);
|
return table.getRows()
|
||||||
|
.stream()
|
||||||
|
.flatMap(Collection::stream)
|
||||||
|
.filter(Cell::isHeaderCell)
|
||||||
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -27,7 +27,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class LayoutparserEnd2EndTest extends AbstractTest {
|
public class LayoutparserEnd2EndTest extends AbstractTest {
|
||||||
|
|
||||||
public static final LayoutParsingType LAYOUT_PARSING_TYPE = LayoutParsingType.DOCUMINE;
|
public static final LayoutParsingType LAYOUT_PARSING_TYPE = LayoutParsingType.DOCUMINE_OLD;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private LayoutParsingPipeline layoutParsingPipeline;
|
private LayoutParsingPipeline layoutParsingPipeline;
|
||||||
@ -37,7 +37,7 @@ public class LayoutparserEnd2EndTest extends AbstractTest {
|
|||||||
@Disabled
|
@Disabled
|
||||||
public void testLayoutParserEndToEnd() {
|
public void testLayoutParserEndToEnd() {
|
||||||
|
|
||||||
String filePath = "/home/kschuettler/Downloads/55974b3de7ed2915718a10458206bbd8.ORIGIN.pdf";
|
String filePath = "/home/kschuettler/Dokumente/Ticket Related/RED-9964/17a25133-e098-4610-b553-d1bf11a56d96/560e6ab1ab4754b9a62fd2e6d4d71327/560e6ab1ab4754b9a62fd2e6d4d71327.ORIGIN.pdf";
|
||||||
|
|
||||||
runForFile(filePath);
|
runForFile(filePath);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user