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