diff --git a/core/src/main/java/de/jplag/options/SimilarityMetric.java b/core/src/main/java/de/jplag/options/SimilarityMetric.java index 0f38cb83d8..08d00f78d0 100644 --- a/core/src/main/java/de/jplag/options/SimilarityMetric.java +++ b/core/src/main/java/de/jplag/options/SimilarityMetric.java @@ -3,12 +3,23 @@ import java.util.function.ToDoubleFunction; import de.jplag.JPlagComparison; +import de.jplag.Match; public enum SimilarityMetric implements ToDoubleFunction { AVG("average similarity", JPlagComparison::similarity), MIN("minimum similarity", JPlagComparison::minimalSimilarity), MAX("maximal similarity", JPlagComparison::maximalSimilarity), - INTERSECTION("matched tokens", it -> (double) it.getNumberOfMatchedTokens()); + INTERSECTION("matched tokens", it -> (double) it.getNumberOfMatchedTokens()), + SYMMETRIC("symmetric similarity", it -> { + int divisor = it.firstSubmission().getNumberOfTokens() + it.secondSubmission().getNumberOfTokens(); + if (divisor != 0) { + return 2.0 * it.getNumberOfMatchedTokens() / divisor; + } else { + return .0; + } + }), + LONGEST_MATCH("number of tokens in the longest match", it -> it.matches().stream().mapToInt(Match::length).max().orElse(0)), + OVERALL("Sum of both submission lengths", it -> it.firstSubmission().getNumberOfTokens() + it.secondSubmission().getNumberOfTokens()); private final ToDoubleFunction similarityFunction; private final String description; diff --git a/core/src/main/java/de/jplag/reporting/jsonfactory/ComparisonReportWriter.java b/core/src/main/java/de/jplag/reporting/jsonfactory/ComparisonReportWriter.java index 35ef87de59..832289a477 100644 --- a/core/src/main/java/de/jplag/reporting/jsonfactory/ComparisonReportWriter.java +++ b/core/src/main/java/de/jplag/reporting/jsonfactory/ComparisonReportWriter.java @@ -2,6 +2,7 @@ import java.nio.file.Path; import java.util.Comparator; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -57,13 +58,20 @@ private void writeComparisons(List comparisons) { String secondSubmissionId = submissionToIdFunction.apply(comparison.secondSubmission()); String fileName = generateComparisonName(firstSubmissionId, secondSubmissionId); addToLookUp(firstSubmissionId, secondSubmissionId, fileName); - var comparisonReport = new ComparisonReport(firstSubmissionId, secondSubmissionId, - Map.of(SimilarityMetric.AVG.name(), comparison.similarity(), SimilarityMetric.MAX.name(), comparison.maximalSimilarity()), + var comparisonReport = new ComparisonReport(firstSubmissionId, secondSubmissionId, createSimilarityMap(comparison), convertMatchesToReportMatches(comparison), comparison.similarityOfFirst(), comparison.similarityOfSecond()); resultWriter.addJsonEntry(comparisonReport, Path.of(fileName)); } } + private Map createSimilarityMap(JPlagComparison comparison) { + Map result = new HashMap<>(); + for (SimilarityMetric metric : SimilarityMetric.values()) { + result.put(metric.name(), metric.applyAsDouble(comparison)); + } + return result; + } + private void addToLookUp(String firstSubmissionId, String secondSubmissionId, String fileName) { writeToMap(secondSubmissionId, firstSubmissionId, fileName); writeToMap(firstSubmissionId, secondSubmissionId, fileName); diff --git a/core/src/main/java/de/jplag/reporting/reportobject/mapper/MetricMapper.java b/core/src/main/java/de/jplag/reporting/reportobject/mapper/MetricMapper.java index 0868cf20ac..890f0ff374 100644 --- a/core/src/main/java/de/jplag/reporting/reportobject/mapper/MetricMapper.java +++ b/core/src/main/java/de/jplag/reporting/reportobject/mapper/MetricMapper.java @@ -47,6 +47,10 @@ public List getTopComparisons(JPlagResult result) { } private Map getComparisonMetricMap(JPlagComparison comparison) { - return Map.of(SimilarityMetric.AVG.name(), comparison.similarity(), SimilarityMetric.MAX.name(), comparison.maximalSimilarity()); + Map metricMap = new HashMap<>(); + for (SimilarityMetric metric : SimilarityMetric.values()) { + metricMap.put(metric.name(), metric.applyAsDouble(comparison)); + } + return metricMap; } } diff --git a/endtoend-testing/src/main/java/de/jplag/endtoend/model/ExpectedResult.java b/endtoend-testing/src/main/java/de/jplag/endtoend/model/ExpectedResult.java index ff2408adb5..10b2b8dc47 100644 --- a/endtoend-testing/src/main/java/de/jplag/endtoend/model/ExpectedResult.java +++ b/endtoend-testing/src/main/java/de/jplag/endtoend/model/ExpectedResult.java @@ -24,6 +24,7 @@ public double getSimilarityForMetric(SimilarityMetric metric) { case MIN -> resultSimilarityMinimum(); case MAX -> resultSimilarityMaximum(); case INTERSECTION -> resultMatchedTokenNumber(); + default -> throw new IllegalArgumentException(String.format("Similarity metric %s not supported for end to end tests", metric.name())); }; }