diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/redaction/model/Section.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/redaction/model/Section.java index 049349cc..8bb99977 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/redaction/model/Section.java +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/redaction/model/Section.java @@ -25,13 +25,17 @@ public class Section { private String headline; + public boolean contains(String type) { + return entities.stream().anyMatch(entity -> entity.getType().equals(type)); } - public void redact(String type, int ruleNumber, String reason){ + + public void redact(String type, int ruleNumber, String reason) { + entities.forEach(entity -> { - if(entity.getType().equals(type)){ + if (entity.getType().equals(type)) { entity.setRedaction(true); entity.setMatchedRule(ruleNumber); entity.setRedactionReason(reason); @@ -39,9 +43,11 @@ public class Section { }); } - public void redactNot(String type, int ruleNumber, String reason){ + + public void redactNot(String type, int ruleNumber, String reason) { + entities.forEach(entity -> { - if(entity.getType().equals(type)){ + if (entity.getType().equals(type)) { entity.setRedaction(false); entity.setMatchedRule(ruleNumber); entity.setRedactionReason(reason); @@ -49,18 +55,19 @@ public class Section { }); } - public void redactLineAfter(String start, String asType, int ruleNumber, String reason){ + + public void redactLineAfter(String start, String asType, int ruleNumber, String reason) { String value = StringUtils.substringBetween(text, start, "\n"); - if(value != null){ - Set found = findEntity(value.trim(), asType); + if (value != null) { + Set found = findEntity(value.trim(), asType); entities.addAll(found); } // TODO No need to iterate entities.forEach(entity -> { - if(entity.getType().equals(asType)){ + if (entity.getType().equals(asType)) { entity.setRedaction(true); entity.setMatchedRule(ruleNumber); entity.setRedactionReason(reason); @@ -70,19 +77,18 @@ public class Section { } - - public void redactBetween(String start, String stop, String asType, int ruleNumber, String reason){ + public void redactBetween(String start, String stop, String asType, int ruleNumber, String reason) { String value = StringUtils.substringBetween(searchText, start, stop); - if(value != null){ - Set found = findEntity(value.trim(), asType); + if (value != null) { + Set found = findEntity(value.trim(), asType); entities.addAll(found); } // TODO No need to iterate entities.forEach(entity -> { - if(entity.getType().equals(asType)){ + if (entity.getType().equals(asType)) { entity.setRedaction(true); entity.setMatchedRule(ruleNumber); entity.setRedactionReason(reason); @@ -91,25 +97,21 @@ public class Section { } - - private Set findEntity(String value, String asType) { Set found = new HashSet<>(); - int startIndex; - int stopIndex = 0; - do { - startIndex = searchText.indexOf(value, stopIndex); - stopIndex = startIndex + value.length(); - - if (startIndex > -1 && - (startIndex == 0 || Character.isWhitespace(searchText.charAt(startIndex - 1)) || isSeparator(searchText.charAt(startIndex - 1))) && - (stopIndex == searchText.length() || isSeparator(searchText.charAt(stopIndex)))) { - found.add(new Entity(searchText.substring(startIndex, stopIndex), asType, startIndex, stopIndex, headline)); - } - } while (startIndex > -1); + int startIndex; + int stopIndex = 0; + do { + startIndex = searchText.indexOf(value, stopIndex); + stopIndex = startIndex + value.length(); + if (startIndex > -1 && (startIndex == 0 || Character.isWhitespace(searchText.charAt(startIndex - 1)) || isSeparator(searchText + .charAt(startIndex - 1))) && (stopIndex == searchText.length() || isSeparator(searchText.charAt(stopIndex)))) { + found.add(new Entity(searchText.substring(startIndex, stopIndex), asType, startIndex, stopIndex, headline)); + } + } while (startIndex > -1); removeEntitiesContainedInLarger(found); @@ -118,14 +120,18 @@ public class Section { private boolean isSeparator(char c) { + return Character.isWhitespace(c) || Pattern.matches("\\p{Punct}", String.valueOf(c)) || c == '\"' || c == '‘' || c == '’'; } + public void removeEntitiesContainedInLarger(Set entities) { + List wordsToRemove = new ArrayList<>(); for (Entity word : entities) { for (Entity inner : entities) { - if (inner.getWord().length() < word.getWord().length() && inner.getStart() >= word.getStart() && inner.getEnd() <= word.getEnd() && word != inner) { + if (inner.getWord().length() < word.getWord() + .length() && inner.getStart() >= word.getStart() && inner.getEnd() <= word.getEnd() && word != inner) { wordsToRemove.add(inner); } } diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/redaction/service/DictionaryService.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/redaction/service/DictionaryService.java index 4a02ad89..d245c5d0 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/redaction/service/DictionaryService.java +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/redaction/service/DictionaryService.java @@ -2,6 +2,7 @@ package com.iqser.red.service.redaction.v1.server.redaction.service; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -55,16 +56,14 @@ public class DictionaryService { try { TypeResponse typeResponse = dictionaryClient.getAllTypes(); - if (typeResponse != null && !CollectionUtils.isEmpty(typeResponse.getTypes())) { + if (typeResponse != null && CollectionUtils.isNotEmpty(typeResponse.getTypes())) { entryColors = typeResponse.getTypes() .stream() .collect(Collectors.toMap(TypeResult::getType, TypeResult::getColor)); dictionary = entryColors.keySet() .stream() - .collect(Collectors.toMap(type -> type, s -> dictionaryClient.getDictionaryForType(s) - .getEntries() - .stream() - .collect(Collectors.toSet()))); + .collect(Collectors.toMap(type -> type, s -> new HashSet<>(dictionaryClient.getDictionaryForType(s) + .getEntries()))); hintTypes = typeResponse.getTypes() .stream() .filter(TypeResult::isHint)