solutions2/docdiff.scala
changeset 203 eb188f9ac038
parent 202 f7bcb27d1940
child 283 ef5f62bf5987
--- a/solutions2/docdiff.scala	Thu Nov 15 03:35:38 2018 +0000
+++ b/solutions2/docdiff.scala	Thu Nov 15 14:23:55 2018 +0000
@@ -10,29 +10,29 @@
 //
 //         some_regex.findAllIn(some_string)
 //
-//    The words should be Returned as a lsit of strings.
+//    The words should be Returned as a list of strings.
 
 def clean(s: String) : List[String] = 
   ("""\w+""".r).findAllIn(s).toList
 
 
-//(2) The function occurences calculates the number of times  
-//    strings occur in a list of strings. These occurences should 
+//(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 occurences(xs: List[String]): Map[String, Int] =
+def occurrences(xs: List[String]): Map[String, Int] =
   (for (x <- xs.distinct) yield (x, xs.count(_ == x))).toMap
 
 //(3) This functions calculates the dot-product of two documents
-//    (list of strings). For this it calcualtes the occurence
-//    maps from (2) and then multiplies the corresponding occurences. 
+//    (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 = {
     val words = (lst1 ::: lst2).distinct
-    val occs1 = occurences(lst1)
-    val occs2 = occurences(lst2)
+    val occs1 = occurrences(lst1)
+    val occs2 = occurrences(lst2)
     words.map{ w => occs1.getOrElse(w, 0) * occs2.getOrElse(w, 0) }.sum
 }
 
@@ -57,8 +57,8 @@
 val list1 = List("a", "b", "b", "c", "d") 
 val list2 = List("d", "b", "d", "b", "d")
 
-occurences(List("a", "b", "b", "c", "d"))   // Map(a -> 1, b -> 2, c -> 1, d -> 1)
-occurences(List("d", "b", "d", "b", "d"))   // Map(d -> 3, b -> 2)
+occurrences(List("a", "b", "b", "c", "d"))   // Map(a -> 1, b -> 2, c -> 1, d -> 1)
+occurrences(List("d", "b", "d", "b", "d"))   // Map(d -> 3, b -> 2)
 
 prod(list1,list2) // 7