JPlagComparison.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package de.jplag;
  2. import java.util.Collections;
  3. import java.util.List;
  4. /**
  5. * This record represents the whole result of a comparison between two submissions.
  6. * @param firstSubmission is the first of the two submissions.
  7. * @param secondSubmission is the second of the two submissions.
  8. * @param matches is the unmodifiable list of all matches between the two submissions.
  9. */
  10. public record JPlagComparison(Submission firstSubmission, Submission secondSubmission, List<Match> matches) {
  11. /**
  12. * Initializes a new comparison.
  13. * @param firstSubmission is the first of the two submissions.
  14. * @param secondSubmission is the second of the two submissions.
  15. * @param matches is the list of all matches between the two submissions.
  16. */
  17. public JPlagComparison(Submission firstSubmission, Submission secondSubmission, List<Match> matches) {
  18. this.firstSubmission = firstSubmission;
  19. this.secondSubmission = secondSubmission;
  20. this.matches = Collections.unmodifiableList(matches);
  21. }
  22. /**
  23. * Get the total number of matched tokens for this comparison.
  24. */
  25. public final int getNumberOfMatchedTokens() {
  26. return matches.stream().mapToInt(Match::length).sum();
  27. }
  28. /**
  29. * @return Maximum similarity in percent of both submissions.
  30. */
  31. public final double maximalSimilarity() {
  32. return Math.max(similarityOfFirst(), similarityOfSecond());
  33. }
  34. /**
  35. * @return Minimum similarity in percent of both submissions.
  36. */
  37. public final double minimalSimilarity() {
  38. return Math.min(similarityOfFirst(), similarityOfSecond());
  39. }
  40. /**
  41. * @return Similarity in percent (what percentage of tokens across both submissions are matched).
  42. */
  43. public final double similarity() {
  44. boolean subtractBaseCode = firstSubmission.hasBaseCodeMatches() && secondSubmission.hasBaseCodeMatches();
  45. int divisorA = firstSubmission.getSimilarityDivisor(subtractBaseCode);
  46. int divisorB = secondSubmission.getSimilarityDivisor(subtractBaseCode);
  47. return 2 * similarity(divisorA + divisorB);
  48. }
  49. /**
  50. * @return Similarity in percent for the first submission (what percent of the first submission is similar to the
  51. * second).
  52. */
  53. public final double similarityOfFirst() {
  54. int divisor = firstSubmission.getSimilarityDivisor(true);
  55. return similarity(divisor);
  56. }
  57. /**
  58. * @return Similarity in percent for the second submission (what percent of the second submission is similar to the
  59. * first).
  60. */
  61. public final double similarityOfSecond() {
  62. int divisor = secondSubmission.getSimilarityDivisor(true);
  63. return similarity(divisor);
  64. }
  65. @Override
  66. public String toString() {
  67. return firstSubmission.getName() + " <-> " + secondSubmission.getName();
  68. }
  69. private double similarity(int divisor) {
  70. return (divisor == 0 ? 0.0 : (getNumberOfMatchedTokens() * 100 / (double) divisor));
  71. }
  72. }