Merge branch 'DM-307' into 'master'

DM-307: Revert getAsSentences change and changed getLongestBlock to...

Closes DM-307

See merge request redactmanager/redaction-report-service!6
This commit is contained in:
Dominique Eifländer 2023-07-12 09:36:38 +02:00
commit 498e091bf6

View File

@ -198,7 +198,7 @@ public class RSSPoc2Service {
}
if (oecdIn(oecd, Set.of("402"))) {
resultMap.put(NECROPSY_FINDINGS, getLongestBlockOrElse(redactionLog, "necropsy_findings", ""));
resultMap.put(NECROPSY_FINDINGS, getLongestCombinedSectionBlockOrElse(redactionLog, "necropsy_findings", ""));
resultMap.put(DOSES_MG_PER_KG_BW, getAsOneBlock(redactionLog, "doses_(mg_kg_bw)"));
}
@ -561,28 +561,22 @@ public class RSSPoc2Service {
String transformation = String.format("Values of type '%s' as sentences", type);
List<SCMComponent> sentences = new ArrayList<>();
var typeStringsEntries = redactionLog.getRedactionLogEntry().stream().filter(r -> r.getType().equals(type)).collect(Collectors.toList());
Map<Integer, List<RedactionLogEntry>> entriesPerSection = new HashMap<>();
redactionLog.getRedactionLogEntry().stream().filter(r -> r.getType().equals(type)).forEach(e -> {
entriesPerSection.computeIfAbsent(e.getSectionNumber(), (x) -> new ArrayList<>()).add(e);
});
if (entriesPerSection.isEmpty()) {
if (typeStringsEntries.isEmpty()) {
return sentences;
}
for (Map.Entry<Integer, List<RedactionLogEntry>> entriesInSection : entriesPerSection.entrySet()) {
String combinedString = entriesInSection.getValue().stream().map(RedactionLogEntry::getValue).collect(Collectors.joining(" ")).trim();
for (RedactionLogEntry typeStringEntry : typeStringsEntries) {
BreakIterator iterator = BreakIterator.getSentenceInstance(Locale.US);
iterator.setText(combinedString);
iterator.setText(typeStringEntry.getValue());
int start = iterator.first();
for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator.next()) {
sentences.add(SCMComponent.builder()
.originalValue(combinedString.substring(start, end).replaceAll("\\n", "").trim())
.scmAnnotations(entriesInSection.getValue().stream().map(this::toScmAnnotations).toList())
.originalValue(typeStringEntry.getValue().substring(start, end).replaceAll("\\n", "").trim())
.scmAnnotations(List.of(toScmAnnotations(typeStringEntry)))
.transformation(transformation)
.build());
@ -593,24 +587,35 @@ public class RSSPoc2Service {
}
private SCMComponent getLongestBlockOrElse(RedactionLog redactionLog, String type, String elseValue) {
private SCMComponent getLongestCombinedSectionBlockOrElse(RedactionLog redactionLog, String type, String elseValue) {
String transformation = String.format("Longest value of type '%s' if present or else '%s'", type, elseValue);
String transformation = String.format("Longest combined section value of type '%s' if present or else '%s'", type, elseValue);
var entries = redactionLog.getRedactionLogEntry()
.stream()
.filter(r -> r.getType().equals(type))
.sorted(Comparator.comparing(s -> s.getValue().length()))
.collect(Collectors.toList());
Map<Integer, List<RedactionLogEntry>> entriesPerSection = new HashMap<>();
redactionLog.getRedactionLogEntry().stream().filter(r -> r.getType().equals(type)).forEach(e -> {
entriesPerSection.computeIfAbsent(e.getSectionNumber(), (x) -> new ArrayList<>()).add(e);
});
if (!entries.isEmpty()) {
var firstEntry = entries.get(entries.size() - 1);
return SCMComponent.builder().originalValue(firstEntry.getValue()).scmAnnotations(List.of(toScmAnnotations(firstEntry))).transformation(transformation).build();
if (entriesPerSection.isEmpty()) {
return SCMComponent.builder().originalValue(elseValue).transformation(transformation).build();
}
return SCMComponent.builder().originalValue(elseValue).transformation(transformation).build();
int sectionNrOfLongestValue = 0;
String longestValue = "";
for (Map.Entry<Integer, List<RedactionLogEntry>> sectionEntry : entriesPerSection.entrySet()) {
String value = sectionEntry.getValue().stream().map(RedactionLogEntry::getValue).collect(Collectors.joining(" ")).trim();
if (value.length() > longestValue.length()) {
longestValue = value;
sectionNrOfLongestValue = sectionEntry.getKey();
}
}
return SCMComponent.builder()
.originalValue(longestValue)
.scmAnnotations(entriesPerSection.get(sectionNrOfLongestValue).stream().map(this::toScmAnnotations).toList())
.transformation(transformation)
.build();
}