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