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