Assignment 6 (work in progress).
[Scala.git] / Assignment_06 / src / main / scala / Anagrams.scala
diff --git a/Assignment_06/src/main/scala/Anagrams.scala b/Assignment_06/src/main/scala/Anagrams.scala
new file mode 100644 (file)
index 0000000..e529124
--- /dev/null
@@ -0,0 +1,76 @@
+package anagrams
+
+/**
+ * Sentence anagrams generation
+ * MSE course, T-AdvPrPa course
+ */
+object Anagrams {
+       /**
+        * Types definitions
+        */
+       type Word = String
+       type Occurrences = List[(Char, Int)]
+       type Sentence = List[Word]
+
+       /**
+        * The dictionary is simply a sequence of words.
+        * It is predefined and obtained as a sequence using the utility method `loadDictionary`.
+        */
+       val dictionary: List[Word] = loadDictionary()
+
+       /**
+        *  Question 1 : converts the word into its character occurrence list.
+        *
+        *  For instance,
+        *  wordOccurences("abcd") gives List(('a', 1), ('b', 1), ('c', 1), ('d', 1))
+        *  wordOccurences("aabcddd") gives List(('a', 2), ('b', 1), ('c', 1), ('d', 3))
+        *
+        *  Note: the upper case and lower case version of the character are treated as the
+        *  same character, and are represented as a lower case character in the occurrence list.
+        *  The list is sorted alphabeticaly.
+        */
+       def wordOccurrences(w: Word): Occurrences =
+               w.toLowerCase().groupBy(identity).toList.map{ case (_, letters) => (letters(0), letters.length) }.sortWith((a, b) => a._1 < b._1)
+
+       /**
+        * Question 2: Valid words
+        *
+        * The `dictionaryByOccurrences` is a `Map` from different occurrences to a sequence of all
+        * the words that have that occurrence count. This map serves as an easy way to obtain all the anagrams of a word given its occurrence list.
+        */
+       lazy val dictionaryByOccurrences: Map[Occurrences, List[Word]] =
+               dictionary groupBy (w => wordOccurrences(w))
+
+       /**
+        * Question 3: Returns all the anagrams of a given word
+        */     
+       def wordAnagrams(word: Word): List[Word] =
+               dictionaryByOccurrences get wordOccurrences(word) match {
+                       case Some(words) => words
+                       case None => List()
+               }
+       
+       /**
+        * Question 4: Returns all the subsets of an occurrence list
+        */
+
+       /**
+        * Generates all the subsets of a set
+        */
+       def combinations(occurrences: Occurrences): List[Occurrences] = ???
+       
+       /**
+        * Question 5: remove occurrences from x that are in y
+        */
+       def subtract(x: Occurrences, y: Occurrences): Occurrences = ???
+       
+       /**
+        * Question 6 - Generate sentence anagrams
+        */
+       
+       /** Converts a sentence into its character occurrence list. */
+       def sentenceOccurrences(s: Sentence): Occurrences = ???
+       
+       def sentenceAnagrams(sentence: Sentence): List[Sentence] = ???
+
+}