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