core_testing2/docdiff.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 08 Dec 2023 00:54:36 +0000
changeset 481 e03a0100ec46
parent 401 9471c3b7ea02
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
     1
// Core Part 2 about Code Similarity
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
     2
//===================================
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 283
diff changeset
     4
401
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
     5
object C2 { 
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
     7
// ADD YOUR CODE BELOW
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
     8
//======================
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
     9
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    10
//(1)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    11
def clean(s: String) : List[String] = """(\w+)""".r.findAllIn(s).toList
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    12
  
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    14
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    15
//(2)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    16
def occurrences(xs: List[String]): Map[String, Int] = {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    17
    val ls = xs.distinct
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    18
    val occLs = for (s <- ls) yield (s, xs.count(_.equals(s)))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    19
    occLs.toMap
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    20
}
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 283
diff changeset
    21
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    23
//(3)
401
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    24
def prod(lst1: List[String], lst2: List[String]) : Int = {
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    25
    val occM1 = occurrences(lst1)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    26
    val occM2 = occurrences(lst2)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    27
    (for (s <- occM1) yield s._2 * occM2.getOrElse(s._1,0)).sum
401
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    28
}
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    29
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    30
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    31
//(4)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    32
def overlap(lst1: List[String], lst2: List[String]) : Double = prod(lst1,lst2) / prod(lst1,lst1).max(prod(lst2,lst2))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    33
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    34
def similarity(s1: String, s2: String) : Double = overlap(clean(s1), clean(s2))
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    37
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    38
/* Test cases
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 283
diff changeset
    39
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 283
diff changeset
    40
401
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    41
val list1 = List("a", "b", "b", "c", "d") 
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
val list2 = List("d", "b", "d", "b", "d")
401
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    43
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    44
occurrences(List("a", "b", "b", "c", "d"))   // Map(a -> 1, b -> 2, c -> 1, d -> 1)
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    45
occurrences(List("d", "b", "d", "b", "d"))   // Map(d -> 3, b -> 2)
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    46
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    47
prod(list1,list2) // 7 
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    48
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
overlap(list1, list2)   // 0.5384615384615384
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
overlap(list2, list1)   // 0.5384615384615384
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
overlap(list1, list1)   // 1.0
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
overlap(list2, list2)   // 1.0
401
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    53
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    54
// Plagiarism examples from 
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
// https://desales.libguides.com/avoidingplagiarism/examples
401
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    56
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
val orig1 = """There is a strong market demand for eco-tourism in
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
Australia. Its rich and diverse natural heritage ensures Australia's
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
capacity to attract international ecotourists and gives Australia a
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
comparative advantage in the highly competitive tourism industry."""
401
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    61
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
val plag1 = """There is a high market demand for eco-tourism in
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
Australia. Australia has a comparative advantage in the highly
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
competitive tourism industry due to its rich and varied natural
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
heritage which ensures Australia's capacity to attract international
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
ecotourists."""
401
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    67
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    68
similarity(orig1, plag1) // 0.8679245283018868
401
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    69
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    70
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    71
// Plagiarism examples from 
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
// https://www.utc.edu/library/help/tutorials/plagiarism/examples-of-plagiarism.php
401
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    73
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
val orig2 = """No oil spill is entirely benign. Depending on timing and
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
location, even a relatively minor spill can cause significant harm to
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
individual organisms and entire populations. Oil spills can cause
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
impacts over a range of time scales, from days to years, or even
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
decades for certain spills. Impacts are typically divided into acute
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
(short-term) and chronic (long-term) effects. Both types are part of a
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
complicated and often controversial equation that is addressed after
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
an oil spill: ecosystem recovery."""
401
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    82
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
val plag2 = """There is no such thing as a "good" oil spill. If the
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
time and place are just right, even a small oil spill can cause damage
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
to sensitive ecosystems. Further, spills can cause harm days, months,
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
years, or even decades after they occur. Because of this, spills are
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
usually broken into short-term (acute) and long-term (chronic)
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
effects. Both of these types of harm must be addressed in ecosystem
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
recovery: a controversial tactic that is often implemented immediately
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
following an oil spill."""
401
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    91
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    92
overlap(clean(orig2), clean(plag2))  // 0.728
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    93
similarity(orig2, plag2)             // 0.728
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    94
401
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    95
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    96
 
401
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    97
// The punchline: everything above 0.6 looks suspicious and 
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    98
// should be investigated by staff.
401
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 346
diff changeset
    99
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
*/
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 283
diff changeset
   102
}