cws/cw02.tex
author Christian Urban <urbanc@in.tum.de>
Tue, 04 Feb 2020 14:15:42 +0000
changeset 331 e3878cdd38bc
parent 329 8a34b2ebc8cc
child 333 24bc76d97db2
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
     1
% !TEX program = xelatex
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
\documentclass{article}
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
     3
\usepackage{../style}
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
     4
\usepackage{disclaimer}
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
     5
\usepackage{../langs}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
\begin{document}
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    10
%% should ask to lower case the words.
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
    11
306
Christian Urban <urbanc@in.tum.de>
parents: 301
diff changeset
    12
\section*{Part 7 (Scala)}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
264
ecd989eee8bd updated
Christian Urban <urbanc@in.tum.de>
parents: 259
diff changeset
    14
\mbox{}\hfill\textit{``What one programmer can do in one month,}\\
ecd989eee8bd updated
Christian Urban <urbanc@in.tum.de>
parents: 259
diff changeset
    15
\mbox{}\hfill\textit{two programmers can do in two months.''}\smallskip\\
276
52faee6d0be2 updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
    16
\mbox{}\hfill\textit{ --- Frederick P.~Brooks (author of The Mythical Man-Month)}\bigskip\medskip
264
ecd989eee8bd updated
Christian Urban <urbanc@in.tum.de>
parents: 259
diff changeset
    17
ecd989eee8bd updated
Christian Urban <urbanc@in.tum.de>
parents: 259
diff changeset
    18
\noindent
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 276
diff changeset
    19
You are asked to implement Scala programs for measuring similarity in
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 276
diff changeset
    20
texts, and for recommending movies according to a ratings list. The
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 276
diff changeset
    21
preliminary part~(4\%) is due on \cwSEVEN{} at 4pm; the core part is due
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 276
diff changeset
    22
on \cwSEVENa{} at 4pm.   Note the core part might include material you
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 276
diff changeset
    23
have not yet seen in the first two lectures. \bigskip
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 48
diff changeset
    24
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
    25
\IMPORTANT{}
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    26
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    27
\noindent
144
716042628398 updated
Christian Urban <urbanc@in.tum.de>
parents: 110
diff changeset
    28
Also note that the running time of each part will be restricted to a
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    29
maximum of 30 seconds on my laptop.
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    30
166
780c40aaad27 updated
Christian Urban <urbanc@in.tum.de>
parents: 163
diff changeset
    31
\DISCLAIMER{}
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    32
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    33
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    34
\subsection*{Reference Implementation}
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    35
306
Christian Urban <urbanc@in.tum.de>
parents: 301
diff changeset
    36
Like the C++ part, the Scala part works like this: you
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    37
push your files to GitHub and receive (after sometimes a long delay) some
306
Christian Urban <urbanc@in.tum.de>
parents: 301
diff changeset
    38
automated feedback. In the end we will take a snapshot of the submitted files and
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
    39
apply an automated marking script to them.\medskip
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    40
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
    41
\noindent
306
Christian Urban <urbanc@in.tum.de>
parents: 301
diff changeset
    42
In addition, the Scala part comes with reference
Christian Urban <urbanc@in.tum.de>
parents: 301
diff changeset
    43
implementations in form of \texttt{jar}-files. This allows you to run
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    44
any test cases on your own computer. For example you can call Scala on
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    45
the command line with the option \texttt{-cp docdiff.jar} and then
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    46
query any function from the template file. Say you want to find out
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    47
what the function \texttt{occurrences} produces: for this you just need
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    48
to prefix it with the object name \texttt{CW7a} (and \texttt{CW7b}
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    49
respectively for \texttt{danube.jar}).  If you want to find out what
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    50
these functions produce for the list \texttt{List("a", "b", "b")},
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    51
you would type something like:
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    53
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    54
$ scala -cp docdiff.jar
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    55
  
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    56
scala> CW7a.occurrences(List("a", "b", "b"))
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    57
...
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    58
\end{lstlisting}%$
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    59
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    60
\subsection*{Hints}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    62
\noindent
306
Christian Urban <urbanc@in.tum.de>
parents: 301
diff changeset
    63
\textbf{For Preliminary Part:} useful operations involving regular
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    64
expressions:
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    65
\[\texttt{reg.findAllIn(s).toList}\]
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    66
\noindent finds all
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    67
substrings in \texttt{s} according to a regular regular expression
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    68
\texttt{reg}; useful list operations: \texttt{.distinct}
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    69
removing duplicates from a list, \texttt{.count} counts the number of
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    70
elements in a list that satisfy some condition, \texttt{.toMap}
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    71
transfers a list of pairs into a Map, \texttt{.sum} adds up a list of
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    72
integers, \texttt{.max} calculates the maximum of a list.\bigskip
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    73
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    74
\noindent
306
Christian Urban <urbanc@in.tum.de>
parents: 301
diff changeset
    75
\textbf{For Core Part:} use \texttt{.split(",").toList} for splitting
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    76
strings according to commas (similarly $\backslash$\texttt{n}),
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 284
diff changeset
    77
\texttt{.getOrElse(..,..)} allows to query a Map, but also gives a
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    78
default value if the Map is not defined, a Map can be `updated' by
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    79
using \texttt{+}, \texttt{.contains} and \texttt{.filter} can test whether
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    80
an element is included in a list, and respectively filter out elements in a list,
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    81
\texttt{.sortBy(\_.\_2)} sorts a list of pairs according to the second
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    82
elements in the pairs---the sorting is done from smallest to highest,
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    83
\texttt{.take(n)} for taking some elements in a list (takes fewer if the list
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    84
contains less than \texttt{n} elements).
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    85
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 38
diff changeset
    86
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
    87
\newpage
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 276
diff changeset
    88
\subsection*{Preliminary Part (4 Marks, file docdiff.scala)}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    90
It seems source code plagiarism---stealing and submitting someone
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    91
else's code---is a serious problem at other
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    92
universities.\footnote{Surely, King's students, after all their
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    93
  instructions and warnings, would never commit such an offence. Yes?}
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    94
Detecting such plagiarism is time-consuming and disheartening for
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    95
lecturers at those universities. To aid these poor souls, let's
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    96
implement in this part a program that determines the similarity
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    97
between two documents (be they source code or texts in English). A
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
    98
document will be represented as a list of strings.
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   101
\subsection*{Tasks}
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   102
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   103
\begin{itemize}
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   104
\item[(1)] Implement a function that `cleans' a string by finding all
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   105
  (proper) words in this string. For this use the regular expression
276
52faee6d0be2 updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   106
  \texttt{\textbackslash{}w+} for recognising words and the library function
52faee6d0be2 updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   107
  \texttt{findAllIn}. The function should return a document (a list of
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   108
  strings).\\
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   109
  \mbox{}\hfill [1 Mark]
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   110
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   111
\item[(2)] In order to compute the overlap between two documents, we
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   112
  associate each document with a \texttt{Map}. This Map represents the
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   113
  strings in a document and how many times these strings occur in the
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   114
  document. A simple (though slightly inefficient) method for counting
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   115
  the number of string-occurrences in a document is as follows: remove
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   116
  all duplicates from the document; for each of these (unique)
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   117
  strings, count how many times they occur in the original document.
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   118
  Return a Map associating strings with occurrences. For example
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   120
  \begin{center}
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   121
  \pcode{occurrences(List("a", "b", "b", "c", "d"))}
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   122
  \end{center}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   123
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   124
  produces \pcode{Map(a -> 1, b -> 2, c -> 1, d -> 1)} and
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   125
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   126
  \begin{center}
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   127
  \pcode{occurrences(List("d", "b", "d", "b", "d"))}
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   128
  \end{center}
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   129
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   130
  produces \pcode{Map(d -> 3, b -> 2)}.\hfill[1 Mark]
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   132
\item[(3)] You can think of the Maps calculated under (2) as memory-efficient
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   133
  representations of sparse ``vectors''. In this subtask you need to
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   134
  implement the \emph{product} of two such vectors, sometimes also called
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   135
  \emph{dot product} of two vectors.\footnote{\url{https://en.wikipedia.org/wiki/Dot_product}}
148
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   136
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   137
  For this dot product, implement a function that takes two documents
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   138
  (\texttt{List[String]}) as arguments. The function first calculates
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   139
  the (unique) strings in both. For each string, it multiplies the
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   140
  corresponding occurrences in each document. If a string does not
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   141
  occur in one of the documents, then the product for this string is zero. At the end
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   142
  you need to add up all products. For the two documents in (2) the dot
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   143
  product is 7, because
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   144
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   145
  \[
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   146
    \underbrace{1 * 0}_{"a"} \;\;+\;\;
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   147
    \underbrace{2 * 2}_{"b"} \;\;+\;\;
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   148
    \underbrace{1 * 0}_{"c"} \;\;+\;\;
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   149
    \underbrace{1 * 3}_{"d"} \qquad = 7
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   150
  \]  
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   151
  
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   152
  \hfill\mbox{[1 Mark]}
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   153
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   154
\item[(4)] Implement first a function that calculates the overlap
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   155
  between two documents, say $d_1$ and $d_2$, according to the formula
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   156
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   157
  \[
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   158
  \texttt{overlap}(d_1, d_2) = \frac{d_1 \cdot d_2}{max(d_1^2, d_2^2)}  
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   159
  \]
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   160
316
8b57dd326a91 updated
Christian Urban <urbanc@in.tum.de>
parents: 306
diff changeset
   161
  where $d_1^2$ means $d_1 \cdot d_1$ and so on.
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   162
  You can expect this function to return a \texttt{Double} between 0 and 1. The
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   163
  overlap between the lists in (2) is $0.5384615384615384$.
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 166
diff changeset
   164
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   165
  Second, implement a function that calculates the similarity of
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   166
  two strings, by first extracting the substrings using the clean
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   167
  function from (1)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   168
  and then calculating the overlap of the resulting documents.\\
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   169
  \mbox{}\hfill\mbox{[1 Mark]}
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   170
\end{itemize}\bigskip
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   171
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   172
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 276
diff changeset
   173
\subsection*{Core Part (6 Marks, file danube.scala)}
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   174
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   175
You are creating Danube.co.uk which you hope will be the next big thing
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   176
in online movie renting. You know that you can save money by
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   177
anticipating what movies people will rent; you will pass these savings
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   178
on to your users by offering a discount if they rent movies that
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   179
Danube.co.uk recommends.  
48
7a6a75ea9738 updated
Christian Urban <urbanc@in.tum.de>
parents: 46
diff changeset
   180
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   181
Your task is to generate \emph{two} movie recommendations for every
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   182
movie a user rents. To do this, you calculate what other
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   183
renters, who also watched this movie, suggest by giving positive ratings.
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   184
Of course, some suggestions are more popular than others. You need to find
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   185
the two most-frequently suggested movies. Return fewer recommendations,
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   186
if there are fewer movies suggested.
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   187
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   188
The calculations will be based on the small datasets which the research lab
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   189
GroupLens provides for education and development purposes.
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   190
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   191
\begin{center}
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   192
\url{https://grouplens.org/datasets/movielens/}
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   193
\end{center}
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   194
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   195
\noindent
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   196
The slightly adapted CSV-files should be downloaded in your Scala
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   197
file from the URLs:
148
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   198
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   199
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   200
\begin{center}
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   201
\begin{tabular}{ll}  
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   202
  \url{https://nms.kcl.ac.uk/christian.urban/ratings.csv} & (940 KByte)\\
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   203
  \url{https://nms.kcl.ac.uk/christian.urban/movies.csv}  & (280 KByte)\\
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   204
\end{tabular}
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   205
\end{center}
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   206
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   207
\noindent
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   208
The ratings.csv file is organised as userID, 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   209
movieID, and rating (which is between 0 and 5, with \emph{positive} ratings
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   210
being 4 and 5). The file movie.csv is organised as
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   211
movieID and full movie name. Both files still contain the usual
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   212
CSV-file header (first line). In this part you are asked
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   213
to implement functions that process these files. If bandwidth
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   214
is an issue for you, download the files locally, but in the submitted
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   215
version use \texttt{Source.fromURL} instead of \texttt{Source.fromFile}.
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   216
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   217
\subsection*{Tasks}
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   218
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   219
\begin{itemize}
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   220
\item[(1)] Implement the function \pcode{get_csv_url} which takes an
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   221
  URL-string as argument and requests the corresponding file. The two
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   222
  URLs of interest are \pcode{ratings_url} and \pcode{movies_url},
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   223
  which correspond to CSV-files mentioned above.  The function should
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   224
  return the CSV-file appropriately broken up into lines, and the
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   225
  first line should be dropped (that is omit the header of the CSV-file).
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   226
  The result is a list of strings (the lines in the file). In case
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   227
  the url does not produce a file, return the empty list.\\
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   228
  \mbox{}\hfill [1 Mark]
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   229
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   230
\item[(2)] Implement two functions that process the (broken up)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   231
  CSV-files from (1). The \pcode{process_ratings} function filters out all
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   232
  ratings below 4 and returns a list of (userID, movieID) pairs. The
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   233
  \pcode{process_movies} function returns a list of (movieID, title) pairs.\\
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   234
  \mbox{}\hfill [1 Mark]
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   235
%\end{itemize}  
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   236
%  
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   237
%
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   238
%\subsection*{Part 3 (4 Marks, file danube.scala)}
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   239
%
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   240
%\subsection*{Tasks}
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   241
%
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   242
%\begin{itemize}
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   243
\item[(3)] Implement a kind of grouping function that calculates a Map
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   244
  containing the userIDs and all the corresponding recommendations for
259
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents: 251
diff changeset
   245
  this user (list of movieIDs). This should be implemented in a
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents: 251
diff changeset
   246
  tail-recursive fashion using a Map as accumulator. This Map is set to
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   247
  \pcode{Map()} at the beginning of the calculation. For example
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   248
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   249
\begin{lstlisting}[numbers=none]
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   250
val lst = List(("1", "a"), ("1", "b"),
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   251
               ("2", "x"), ("3", "a"),
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   252
               ("2", "y"), ("3", "c"))
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   253
groupById(lst, Map())
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   254
\end{lstlisting}
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   255
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   256
returns the ratings map
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   257
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   258
\begin{center}
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   259
  \pcode{Map(1 -> List(b, a), 2 -> List(y, x), 3 -> List(c, a))}.
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   260
\end{center}
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   261
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   262
\noindent
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   263
In which order the elements of the list are given is unimportant.\\
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   264
\mbox{}\hfill [1 Mark]
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   265
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   266
\item[(4)] Implement a function that takes a ratings map and a movieID
210
63a1376cbebd updated
Christian Urban <urbanc@in.tum.de>
parents: 203
diff changeset
   267
  as arguments.  The function calculates all suggestions containing the
63a1376cbebd updated
Christian Urban <urbanc@in.tum.de>
parents: 203
diff changeset
   268
  given movie in its recommendations. It returns a list of all these
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   269
  recommendations (each of them is a list and needs to have the given
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   270
  movie deleted, otherwise it might happen we recommend the same movie
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   271
  ``back''). For example for the Map from above and the movie
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   272
  \pcode{"y"} we obtain \pcode{List(List("x"))}, and for the movie
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   273
  \pcode{"a"} we get \pcode{List(List("b"), List("c"))}.\\
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   274
  \mbox{}\hfill [1 Mark]
148
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   275
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   276
\item[(5)] Implement a suggestions function which takes a ratings map
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   277
  and a movieID as arguments. It calculates all the recommended movies
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   278
  sorted according to the most frequently suggested movie(s) sorted
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   279
  first. This function returns \emph{all} suggested movieIDs as a list of
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   280
  strings.\\
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   281
  \mbox{}\hfill [1 Mark]
148
ead6089209ba updated
Christian Urban <urbanc@in.tum.de>
parents: 147
diff changeset
   282
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   283
\item[(6)]  
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   284
  Implement then a recommendation function which generates a maximum
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   285
  of two most-suggested movies (as calculated above). But it returns
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   286
  the actual movie name, not the movieID. If fewer movies are recommended,
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   287
  then return fewer than two movie names.\\
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   288
  \mbox{}\hfill [1 Mark]
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   289
\end{itemize}
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   290
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   291
\end{document} 
6
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   292
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   293
%%% Local Variables: 
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   294
%%% mode: latex
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   295
%%% TeX-master: t
aae256985251 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   296
%%% End: