Assignment 6 (work in progress).
[Scala.git] / Assignment_06 / src / main / scala / Anagrams.scala
1 package anagrams
2
3 /**
4 * Sentence anagrams generation
5 * MSE course, T-AdvPrPa course
6 */
7 object Anagrams {
8 /**
9 * Types definitions
10 */
11 type Word = String
12 type Occurrences = List[(Char, Int)]
13 type Sentence = List[Word]
14
15 /**
16 * The dictionary is simply a sequence of words.
17 * It is predefined and obtained as a sequence using the utility method `loadDictionary`.
18 */
19 val dictionary: List[Word] = loadDictionary()
20
21 /**
22 * Question 1 : converts the word into its character occurrence list.
23 *
24 * For instance,
25 * wordOccurences("abcd") gives List(('a', 1), ('b', 1), ('c', 1), ('d', 1))
26 * wordOccurences("aabcddd") gives List(('a', 2), ('b', 1), ('c', 1), ('d', 3))
27 *
28 * Note: the upper case and lower case version of the character are treated as the
29 * same character, and are represented as a lower case character in the occurrence list.
30 * The list is sorted alphabeticaly.
31 */
32 def wordOccurrences(w: Word): Occurrences =
33 w.toLowerCase().groupBy(identity).toList.map{ case (_, letters) => (letters(0), letters.length) }.sortWith((a, b) => a._1 < b._1)
34
35 /**
36 * Question 2: Valid words
37 *
38 * The `dictionaryByOccurrences` is a `Map` from different occurrences to a sequence of all
39 * 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.
40 */
41 lazy val dictionaryByOccurrences: Map[Occurrences, List[Word]] =
42 dictionary groupBy (w => wordOccurrences(w))
43
44 /**
45 * Question 3: Returns all the anagrams of a given word
46 */
47 def wordAnagrams(word: Word): List[Word] =
48 dictionaryByOccurrences get wordOccurrences(word) match {
49 case Some(words) => words
50 case None => List()
51 }
52
53 /**
54 * Question 4: Returns all the subsets of an occurrence list
55 */
56
57 /**
58 * Generates all the subsets of a set
59 */
60 def combinations(occurrences: Occurrences): List[Occurrences] = ???
61
62 /**
63 * Question 5: remove occurrences from x that are in y
64 */
65 def subtract(x: Occurrences, y: Occurrences): Occurrences = ???
66
67 /**
68 * Question 6 - Generate sentence anagrams
69 */
70
71 /** Converts a sentence into its character occurrence list. */
72 def sentenceOccurrences(s: Sentence): Occurrences = ???
73
74 def sentenceAnagrams(sentence: Sentence): List[Sentence] = ???
75
76 }