marking2/docdiff.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 24 Nov 2020 09:04:06 +0000
changeset 366 1c829680503e
parent 283 ef5f62bf5987
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
283
ef5f62bf5987 updated
Christian Urban <urbanc@in.tum.de>
parents: 227
diff changeset
     1
// Preliminary Part about Code Similarity
ef5f62bf5987 updated
Christian Urban <urbanc@in.tum.de>
parents: 227
diff changeset
     2
//========================================
227
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
283
ef5f62bf5987 updated
Christian Urban <urbanc@in.tum.de>
parents: 227
diff changeset
     5
object CW7a { 
227
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
//(1) Complete the clean function below. It should find
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
//    all words in a string using the regular expression
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
//    \w+  and the library function 
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
//
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
//         some_regex.findAllIn(some_string)
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
//
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
//    The words should be Returned as a list of strings.
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
def clean(s: String) : List[String] = 
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  ("""\w+""".r).findAllIn(s).toList
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
//(2) The function occurrences calculates the number of times  
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
//    strings occur in a list of strings. These occurrences should 
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
//    be calculated as a Map from strings to integers.
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
def occurrences(xs: List[String]): Map[String, Int] =
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
  (for (x <- xs.distinct) yield (x, xs.count(_ == x))).toMap
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
//(3) This functions calculates the dot-product of two documents
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
//    (list of strings). For this it calculates the occurrence
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
//    maps from (2) and then multiplies the corresponding occurrences. 
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
//    If a string does not occur in a document, the product is zero.
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
//    The function finally sums up all products. 
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
def prod(lst1: List[String], lst2: List[String]) : Int = {
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
    val words = (lst1 ::: lst2).distinct
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
    val occs1 = occurrences(lst1)
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
    val occs2 = occurrences(lst2)
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
    words.map{ w => occs1.getOrElse(w, 0) * occs2.getOrElse(w, 0) }.sum
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
}
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
//(4) Complete the functions overlap and similarity. The overlap of
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
//    two documents is calculated by the formula given in the assignment
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
//    description. The similarity of two strings is given by the overlap
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
//    of the cleaned (see (1)) strings.  
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
def overlap(lst1: List[String], lst2: List[String]) : Double = {
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
    val m1 = prod(lst1, lst1)
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
    val m2 = prod(lst2, lst2) 
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
    prod(lst1, lst2).toDouble / (List(m1, m2).max)
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
}
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
def similarity(s1: String, s2: String) : Double =
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
  overlap(clean(s1), clean(s2))
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
/*
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
val list1 = List("a", "b", "b", "c", "d") 
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
val list2 = List("d", "b", "d", "b", "d")
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
occurrences(List("a", "b", "b", "c", "d"))   // Map(a -> 1, b -> 2, c -> 1, d -> 1)
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
occurrences(List("d", "b", "d", "b", "d"))   // Map(d -> 3, b -> 2)
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
prod(list1,list2) // 7 
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
overlap(list1, list2)   // 0.5384615384615384
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
overlap(list2, list1)   // 0.5384615384615384
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
overlap(list1, list1)   // 1.0
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
overlap(list2, list2)   // 1.0
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
// Plagiarism examples from 
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
// https://desales.libguides.com/avoidingplagiarism/examples
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
val orig1 = """There is a strong market demand for eco-tourism in
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
Australia. Its rich and diverse natural heritage ensures Australia's
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
capacity to attract international ecotourists and gives Australia a
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
comparative advantage in the highly competitive tourism industry."""
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
val plag1 = """There is a high market demand for eco-tourism in
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
Australia. Australia has a comparative advantage in the highly
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
competitive tourism industry due to its rich and varied natural
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
heritage which ensures Australia's capacity to attract international
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
ecotourists."""
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
similarity(orig1, plag1)
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
// Plagiarism examples from 
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
// https://www.utc.edu/library/help/tutorials/plagiarism/examples-of-plagiarism.php
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
val orig2 = """No oil spill is entirely benign. Depending on timing and
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
location, even a relatively minor spill can cause significant harm to
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
individual organisms and entire populations. Oil spills can cause
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
impacts over a range of time scales, from days to years, or even
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
decades for certain spills. Impacts are typically divided into acute
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
(short-term) and chronic (long-term) effects. Both types are part of a
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
complicated and often controversial equation that is addressed after
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
an oil spill: ecosystem recovery."""
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
val plag2 = """There is no such thing as a "good" oil spill. If the
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
time and place are just right, even a small oil spill can cause damage
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
to sensitive ecosystems. Further, spills can cause harm days, months,
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
years, or even decades after they occur. Because of this, spills are
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
usually broken into short-term (acute) and long-term (chronic)
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
effects. Both of these types of harm must be addressed in ecosystem
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
recovery: a controversial tactic that is often implemented immediately
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
following an oil spill."""
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
overlap(clean(orig2), clean(plag2))
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
similarity(orig2, plag2)
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
// The punchline: everything above 0.6 looks suspicious and 
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
// should be looked at by staff.
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
*/
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
b5f3e814a710 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
283
ef5f62bf5987 updated
Christian Urban <urbanc@in.tum.de>
parents: 227
diff changeset
   117
}