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