RED-6009: Document Tree Structure
* remove instanceof as much as possible * refactor access levels
This commit is contained in:
parent
926d507583
commit
a7896148df
@ -73,7 +73,7 @@ public class Document implements GenericSemanticNode {
|
||||
@Override
|
||||
public Headline getHeadline() {
|
||||
|
||||
return streamChildrenOfType(NodeType.HEADLINE).map(node -> (Headline) node).findFirst().orElseThrow(() -> new NotFoundException("No Headlines found in this document!"));
|
||||
return streamAllSubNodesOfType(NodeType.HEADLINE).map(node -> (Headline) node).findFirst().orElseThrow(() -> new NotFoundException("No Headlines found in this document!"));
|
||||
}
|
||||
|
||||
|
||||
@ -85,7 +85,7 @@ public class Document implements GenericSemanticNode {
|
||||
|
||||
public Stream<Image> streamAllImages() {
|
||||
|
||||
return streamAllSubNodes().filter(node -> node instanceof Image).map(node -> (Image) node);
|
||||
return streamAllSubNodesOfType(NodeType.IMAGE).map(node -> (Image) node);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -55,8 +55,7 @@ public class Section implements GenericSemanticNode {
|
||||
|
||||
public Headline getHeadline() {
|
||||
|
||||
return streamChildren()//
|
||||
.filter(node -> node instanceof Headline)//
|
||||
return streamChildrenOfType(NodeType.HEADLINE)//
|
||||
.map(node -> (Headline) node)//
|
||||
.findFirst()//
|
||||
.orElseGet(() -> getParent().getHeadline());
|
||||
|
||||
@ -323,7 +323,7 @@ public interface SemanticNode {
|
||||
|
||||
|
||||
/**
|
||||
* Streams all children located directly underneath this node in the DocumentTree.
|
||||
* Streams all children located directly underneath this node in the DocumentTree of the provided type.
|
||||
*
|
||||
* @return Stream of all children
|
||||
*/
|
||||
@ -344,6 +344,17 @@ public interface SemanticNode {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Recursively streams all SemanticNodes of the provided type located underneath this node in the DocumentTree in order.
|
||||
*
|
||||
* @return Stream of all SubNodes
|
||||
*/
|
||||
default Stream<SemanticNode> streamAllSubNodesOfType(NodeType nodeType) {
|
||||
|
||||
return getDocumentTree().streamAllSubEntriesInOrder(getTreeId()).filter(entry -> entry.getType().equals(nodeType)).map(DocumentTree.Entry::getNode);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The Boundary is the start and end string offsets in the reading order of the document.
|
||||
*
|
||||
|
||||
@ -222,7 +222,7 @@ public class EntityCreationService {
|
||||
}
|
||||
|
||||
|
||||
public boolean isValidEntityBoundary(TextBlock textBlock, Boundary boundary) {
|
||||
private boolean isValidEntityBoundary(TextBlock textBlock, Boundary boundary) {
|
||||
|
||||
return boundaryIsSurroundedBySeparators(textBlock, boundary) && !isWhiteSpacesOrSeparatorsOnly(textBlock, boundary);
|
||||
}
|
||||
@ -245,7 +245,7 @@ public class EntityCreationService {
|
||||
}
|
||||
|
||||
|
||||
public void addEntityToGraph(RedactionEntity entity, DocumentTree documentTree) {
|
||||
private void addEntityToGraph(RedactionEntity entity, DocumentTree documentTree) {
|
||||
|
||||
SemanticNode containingNode = documentTree.streamChildNodes(Collections.emptyList())
|
||||
.filter(node -> node.buildTextBlock().containsBoundary(entity.getBoundary()))
|
||||
|
||||
@ -14,11 +14,9 @@ import com.iqser.red.service.redaction.v1.server.layoutparsing.document.graph.en
|
||||
import com.iqser.red.service.redaction.v1.server.layoutparsing.document.graph.entity.RedactionEntity;
|
||||
import com.iqser.red.service.redaction.v1.server.layoutparsing.document.graph.nodes.Document;
|
||||
import com.iqser.red.service.redaction.v1.server.layoutparsing.document.graph.nodes.SemanticNode;
|
||||
import com.iqser.red.service.redaction.v1.server.layoutparsing.document.graph.textblock.TextBlock;
|
||||
import com.iqser.red.service.redaction.v1.server.layoutparsing.document.services.EntityCreationService;
|
||||
import com.iqser.red.service.redaction.v1.server.redaction.adapter.NerEntities;
|
||||
import com.iqser.red.service.redaction.v1.server.redaction.model.dictionary.Dictionary;
|
||||
import com.iqser.red.service.redaction.v1.server.redaction.model.dictionary.SearchImplementation;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@ -75,30 +73,21 @@ public class EntityRedactionService {
|
||||
public void addDictionaryEntities(Dictionary dictionary, SemanticNode node) {
|
||||
|
||||
for (var model : dictionary.getDictionaryModels()) {
|
||||
findEntitiesWithSearchImplementation(node, model.getEntriesSearch(), EntityType.ENTITY, model.isDossierDictionary(), model.getType());
|
||||
findEntitiesWithSearchImplementation(node, model.getFalsePositiveSearch(), EntityType.FALSE_POSITIVE, model.isDossierDictionary(), model.getType());
|
||||
findEntitiesWithSearchImplementation(node, model.getFalseRecommendationsSearch(), EntityType.FALSE_RECOMMENDATION, model.isDossierDictionary(), model.getType());
|
||||
entityCreationService.bySearchImplementation(model.getEntriesSearch(), model.getType(), EntityType.ENTITY, node)
|
||||
.forEach(entity -> setFields(entity, model.isDossierDictionary()));
|
||||
entityCreationService.bySearchImplementation(model.getFalsePositiveSearch(), model.getType(), EntityType.FALSE_POSITIVE, node)
|
||||
.forEach(entity -> setFields(entity, model.isDossierDictionary()));
|
||||
entityCreationService.bySearchImplementation(model.getFalseRecommendationsSearch(), model.getType(), EntityType.FALSE_RECOMMENDATION, node)
|
||||
.forEach(entity -> setFields(entity, model.isDossierDictionary()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void findEntitiesWithSearchImplementation(SemanticNode node,
|
||||
SearchImplementation searchImplementation,
|
||||
EntityType entityType,
|
||||
boolean isDossierDictionary,
|
||||
String type) {
|
||||
private void setFields(RedactionEntity entity, boolean isDossierDictionary) {
|
||||
|
||||
TextBlock textBlock = node.buildTextBlock();
|
||||
searchImplementation.getBoundaries(textBlock, textBlock.getBoundary())
|
||||
.stream()
|
||||
.filter(boundary -> entityCreationService.isValidEntityBoundary(textBlock, boundary))
|
||||
.map(bounds -> RedactionEntity.initialEntityNode(bounds, type, entityType))
|
||||
.forEach(entity -> {
|
||||
entity.setDossierDictionaryEntry(isDossierDictionary);
|
||||
entity.setDictionaryEntry(true);
|
||||
entity.addEngine(Engine.DICTIONARY);
|
||||
entityCreationService.addEntityToGraph(entity, node);
|
||||
});
|
||||
entity.setDictionaryEntry(true);
|
||||
entity.setDossierDictionaryEntry(isDossierDictionary);
|
||||
entity.addEngine(Engine.DICTIONARY);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package com.iqser.red.service.redaction.v1.server;
|
||||
package com.iqser.red.service.redaction.v1.server.document.services;
|
||||
|
||||
import static com.iqser.red.service.redaction.v1.server.redaction.utils.SeparatorUtils.boundaryIsSurroundedBySeparators;
|
||||
import static org.mockito.Mockito.when;
|
||||
Loading…
x
Reference in New Issue
Block a user