// Preliminary Part about Code Similarity//========================================object CW7a {//(1) Complete the clean function below. It should find// all words in a string using the regular expression// \w+ and the library function//// some_regex.findAllIn(some_string)//// The words should be Returned as a list of strings.//def clean(s: String) : List[String] = ...def clean(s: String) : List[String] = "\\w+".r.findAllIn(s).toList//(2) The function occurrences calculates the number of times// strings occur in a list of strings. These occurrences should// be calculated as a Map from strings to integers.//def occurrences(xs: List[String]): Map[String, Int] = ..def occurrences(xs: List[String]) : Map[String, Int] = xs.groupBy(identity).view.mapValues(_.size).toMap//(3) This functions calculates the dot-product of two documents// (list of strings). For this it calculates the occurrence// maps from (2) and then multiplies the corresponding occurrences.// If a string does not occur in a document, the product is zero.// The function finally sums up all products.//def prod(lst1: List[String], lst2: List[String]) : Int = ..def prod(lst1: List[String], lst2: List[String]) : Int = occurrences(lst1).map(x => occurrences(lst2).getOrElse(x._1, 0) * x._2).reduce(_ + _)//(4) Complete the functions overlap and similarity. The overlap of// two documents is calculated by the formula given in the assignment// description. The similarity of two strings is given by the overlap// of the cleaned strings (see (1)).//def overlap(lst1: List[String], lst2: List[String]) : Double = ...def overlap(lst1: List[String], lst2: List[String]) : Double = prod(lst1, lst2).toDouble/Math.max(prod(lst1, lst1).toDouble, prod(lst2, lst2).toDouble)//def similarity(s1: String, s2: String) : Double = ...def similarity(s1: String, s2: String) : Double = overlap(clean(s1), clean(s2))/* Test casesimport CW7a._val list1 = List("a", "b", "b", "c", "d")val list2 = List("d", "b", "d", "b", "d")occurrences(List("a", "b", "b", "c", "d"))occurrences(List("d", "b", "d", "b", "d"))prod(list1,list2) // 7overlap(list1, list2) // 0.5384615384615384overlap(list2, list1) // 0.5384615384615384overlap(list1, list1) // 1.0overlap(list2, list2) // 1.0// Plagiarism examples from// https://desales.libguides.com/avoidingplagiarism/examplesval orig1 = """There is a strong market demand for eco-tourism inAustralia. Its rich and diverse natural heritage ensures Australia'scapacity to attract international ecotourists and gives Australia acomparative advantage in the highly competitive tourism industry."""val plag1 = """There is a high market demand for eco-tourism inAustralia. Australia has a comparative advantage in the highlycompetitive tourism industry due to its rich and varied naturalheritage which ensures Australia's capacity to attract internationalecotourists."""similarity(orig1, plag1) // 0.8679245283018868// Plagiarism examples from// https://www.utc.edu/library/help/tutorials/plagiarism/examples-of-plagiarism.phpval orig2 = """No oil spill is entirely benign. Depending on timing andlocation, even a relatively minor spill can cause significant harm toindividual organisms and entire populations. Oil spills can causeimpacts over a range of time scales, from days to years, or evendecades for certain spills. Impacts are typically divided into acute(short-term) and chronic (long-term) effects. Both types are part of acomplicated and often controversial equation that is addressed afteran oil spill: ecosystem recovery."""val plag2 = """There is no such thing as a "good" oil spill. If thetime and place are just right, even a small oil spill can cause damageto sensitive ecosystems. Further, spills can cause harm days, months,years, or even decades after they occur. Because of this, spills areusually broken into short-term (acute) and long-term (chronic)effects. Both of these types of harm must be addressed in ecosystemrecovery: a controversial tactic that is often implemented immediatelyfollowing an oil spill."""overlap(clean(orig2), clean(plag2)) // 0.728similarity(orig2, plag2) // 0.728// The punchline: everything above 0.6 looks suspicious and// should be investigated by staff.*/}