Skip to content

Commit

Permalink
include new metrics in report
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Nov 7, 2024
1 parent 27cd733 commit dd333ac
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
13 changes: 12 additions & 1 deletion core/src/main/java/de/jplag/options/SimilarityMetric.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@
import java.util.function.ToDoubleFunction;

import de.jplag.JPlagComparison;
import de.jplag.Match;

public enum SimilarityMetric implements ToDoubleFunction<JPlagComparison> {
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<JPlagComparison> similarityFunction;
private final String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,13 +58,20 @@ private void writeComparisons(List<JPlagComparison> 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<String, Double> createSimilarityMap(JPlagComparison comparison) {
Map<String, Double> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public List<TopComparison> getTopComparisons(JPlagResult result) {
}

private Map<String, Double> getComparisonMetricMap(JPlagComparison comparison) {
return Map.of(SimilarityMetric.AVG.name(), comparison.similarity(), SimilarityMetric.MAX.name(), comparison.maximalSimilarity());
Map<String, Double> metricMap = new HashMap<>();
for (SimilarityMetric metric : SimilarityMetric.values()) {
metricMap.put(metric.name(), metric.applyAsDouble(comparison));
}
return metricMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
};
}

Expand Down

0 comments on commit dd333ac

Please sign in to comment.