RED-6009 - Document Tree Structure
* fix Ner Adapter
This commit is contained in:
parent
06c4162018
commit
1bcb43e50e
@ -2,11 +2,9 @@ package com.iqser.red.service.redaction.v1.server.redaction.adapter;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -88,24 +86,16 @@ public class NerEntitiesAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
List<Boundary> boundaries = entityClusters.stream().filter(cluster -> cluster.size() >= MIN_PART_MATCHES).map(this::toContainingBoundary).distinct().toList();
|
||||
|
||||
return boundaries.stream()
|
||||
.map(boundary -> removeTrailingSeparator(boundary, textBlock))
|
||||
return entityClusters.stream()
|
||||
.filter(cluster -> cluster.size() >= MIN_PART_MATCHES)
|
||||
.map(this::toContainingBoundary)
|
||||
.distinct()
|
||||
.filter(boundary -> SeparatorUtils.boundaryIsSurroundedBySeparators(textBlock, boundary))
|
||||
.map(boundary -> new EntityRecognitionEntity(textBlock.subSequence(boundary).toString(), boundary.start(), boundary.end(), "CBI_address"))
|
||||
.toList();
|
||||
}
|
||||
|
||||
|
||||
private Boundary removeTrailingSeparator(Boundary boundary, TextBlock textBlock) {
|
||||
|
||||
if (SeparatorUtils.isSeparator(textBlock.charAt(boundary.end() - 1))) {
|
||||
return new Boundary(boundary.start(), boundary.end());
|
||||
}
|
||||
return boundary;
|
||||
}
|
||||
|
||||
|
||||
private static boolean isDuplicate(List<EntityRecognitionEntity> currentCluster, EntityRecognitionEntity entity) {
|
||||
|
||||
return ALLOW_DUPLICATES || currentCluster.stream().anyMatch(e -> e.getType().equals(entity.getType()));
|
||||
@ -121,40 +111,8 @@ public class NerEntitiesAdapter {
|
||||
public Boundary toContainingBoundary(Collection<EntityRecognitionEntity> entityRecognitionEntities) {
|
||||
|
||||
int minStart = entityRecognitionEntities.stream().mapToInt(EntityRecognitionEntity::getStartOffset).min().orElseThrow(IllegalArgumentException::new);
|
||||
int maxEnd = entityRecognitionEntities.stream().mapToInt(EntityRecognitionEntity::getStartOffset).max().orElseThrow(IllegalArgumentException::new);
|
||||
return new Boundary(minStart, maxEnd);
|
||||
}
|
||||
|
||||
|
||||
public Set<EntityRecognitionEntity> findFollowingEntitiesOfTypeCBIAddressPart(EntityRecognitionEntity entityOfCBIAddressEssentialType,
|
||||
Set<EntityRecognitionEntity> entitiesOfTypeCBIAddressPart) {
|
||||
|
||||
Set<EntityRecognitionEntity> concurrentParts = new HashSet<>();
|
||||
concurrentParts.add(entityOfCBIAddressEssentialType);
|
||||
|
||||
int currentNumberOfConcurrentParts;
|
||||
do {
|
||||
currentNumberOfConcurrentParts = concurrentParts.size();
|
||||
concurrentParts.addAll(entitiesOfTypeCBIAddressPart.stream()
|
||||
.filter(entity -> entity.getStartOffset() >= toContainingBoundary(concurrentParts).end())
|
||||
.filter(entity -> offsetBetweenPartsIsLowerThanMax(toContainingBoundary(concurrentParts), entity))
|
||||
.filter(entity -> ALLOW_DUPLICATES || entityTypeInAlreadyFound(concurrentParts, entity.getType()))
|
||||
.collect(Collectors.toSet()));
|
||||
} while (currentNumberOfConcurrentParts != concurrentParts.size());
|
||||
|
||||
return concurrentParts;
|
||||
}
|
||||
|
||||
|
||||
private static boolean entityTypeInAlreadyFound(Set<EntityRecognitionEntity> alreadyFound, String type) {
|
||||
|
||||
return !alreadyFound.stream().map(EntityRecognitionEntity::getType).collect(Collectors.toSet()).contains(type);
|
||||
}
|
||||
|
||||
|
||||
private static boolean offsetBetweenPartsIsLowerThanMax(Boundary currentBoundary, EntityRecognitionEntity entity) {
|
||||
|
||||
return Math.abs(entity.getStartOffset() - currentBoundary.end()) < MAX_DISTANCE_BETWEEN_PARTS;
|
||||
int maxEnd = entityRecognitionEntities.stream().mapToInt(EntityRecognitionEntity::getEndOffset).max().orElseThrow(IllegalArgumentException::new);
|
||||
return new Boundary(minStart, maxEnd - 1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user