diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/document/EntityCreationService.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/document/EntityCreationService.java index 98072eef..b7a09411 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/document/EntityCreationService.java +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/service/document/EntityCreationService.java @@ -748,19 +748,19 @@ public class EntityCreationService { DocumentTree documentTree = node.getDocumentTree(); try { if (node.getEntities().contains(entity)) { - // If entity already exists but is missing textBefore/textAfter re-add it and enrich the entity with the missing values - if (entity.getTextBefore() == null || entity.getTextAfter() == null) { + // If entity already exists and it has a different text range, we add the text range to the list of duplicated text ranges + Optional optionalTextEntity = node.getEntities() + .stream()// + .filter(e -> e.equals(entity))// + .filter(e -> !e.getTextRange().equals(entity.getTextRange()))// + .findAny(); + if (optionalTextEntity.isPresent()) { + addDuplicateEntityToGraph(optionalTextEntity.get(), entity.getTextRange(), node); + } else { node.getEntities().remove(entity); addNewEntityToGraph(entity, documentTree); - } else { - // If entity already exists and it has a different text range, we add the text range to the list of duplicated text ranges - node.getEntities() - .stream()// - .filter(e -> e.equals(entity))// - .filter(e -> !e.getTextRange().equals(entity.getTextRange()))// - .findAny()// - .ifPresent(entityToDuplicate -> addDuplicateEntityToGraph(entityToDuplicate, entity.getTextRange(), node)); } + } else { entity.addIntersectingNode(documentTree.getRoot().getNode()); addEntityToGraph(entity, documentTree);