solutions-resit/resit.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 02 Nov 2023 13:53:37 +0000
changeset 474 b528d1d3d3c3
parent 343 c8fcc0e0a57f
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
336
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Resit Exam about data-mining a chat log
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
//=========================================
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
// This coursework is about mining a log of an online chat between 85
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
// participants. The log is given as a csv-list in the file
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
// log.csv. The log is an unordered list containing information which
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
// message has been sent, by whom, when and in response to which other
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
// message. Each message has also a number and a unique hash code.
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
// !! For further information abiout the tasks, see:     !!
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
// !!                                                    !!
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
// !! https://nms.kcl.ac.uk/christian.urban/cw-resit.pdf !!
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
object Resit {
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
import io.Source
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
import scala.util._
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
//=============
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
// (1) The function get_csv takes file name as argument. It should read
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
// the corresponding file and return its content. The content should
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
// be returned as a list of strings, a string for each line in the
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
// file. Since the file is a csv-file, the first line (the header)
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
// should be dropped. Lines are separated by "\n".
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
def get_csv(name: String) : List[String] = ...
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
// test cases:
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
// get_csv("log.csv")
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
// get_csv("log.csv").length  // should be 680
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
//=============
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
// (2) The function below takes a single line from the csv-file (as
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
// generated by get_csv) and creates a Rec(ord) data structure. The
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
// data from the csv-file should be copied as follows:
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
//     csv-file         Rec data structure
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
//     -----------------------------------
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
//      counter      => num
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
//      id           => msg_id 
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
//      time_date    => date
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
//      name         => author
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
//      country      => country (should be None if no country is given)
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
//      parent_id    => reply_id (should be None if there is no parent)
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
//      msg          => msg
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
//                   => parent is set to None  (will be calculated later)
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
//                   => children is set to Nil (will be calculated later)
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
//     You should use the function line.split(",").toList to separate
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
//     the items in the csv-line. BE CAREFUL that the message text in 
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
//     the last field can contain commas and therefore the split will not
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
//     always result in a list of 7 elements. You need to concatenate
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
//     anything beyond the 7th field into a single string for the field msg.
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
case class Rec(num: Int, 
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
               msg_id: String,
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
               date: String,
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
               msg: String,
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
               author: String,
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
               country: Option[String],
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
               reply_id : Option[String],
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
               parent: Option[Int] = None,
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
               children: List[Int] = Nil)  
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
def process_line(line: String) : Rec = ...
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
// test cases:
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
// process_line("""0,5ebeb459ac278d01301f1497,2020-05-15T15:25:13.413000,participant34,United Kingdom,5ebea6424923321d63155796,this question please?""")
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
// 
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
//      ==>  Rec(0,
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
//               "5ebeb459ac278d01301f1497",
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
//               "2020-05-15T15:25:13.413000",
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
//               "this question please?",
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
//               "participant34",
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
//               Some("United Kingdom"),
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
//               Some("5ebea6424923321d63155796"),
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
//               None,
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
//               List())
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
// process_line("""1,hash,date,p43,,,foo, bar""")
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
//      ==>  Rec(1, "hash", "date", "foo, bar",
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
//               "p43", None, None, None, List())
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
// (Note that in the second test case the message needs to be "foo, bar")
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
//=============
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
// (3) Each record in the log contains a unique hash code
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
//     identifying each message. Some messages also contain a hash
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
//     code identifying the parent message (to which question they reply).
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
//     The function post_process fills in the information about
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
//     potential children and a potential parent message. 
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
//  
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
//     The auxiliary function get_children takes a record e and a
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
//     record list rs as arguments, and returns the list of all direct
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
//     children (which have the hash code of e as reply_id). The list
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
//     of children are returned as a list of nums.
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
//      
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
//     The auxiliary function get_parent returns the number of the
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
//     record corresponding to the reply_id (if there exists one,
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
//     otherwise returns None).
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
def get_children(e: Rec, rs: List[Rec]) : List[Int] = ...
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
def get_parent(e: Rec, rs: List[Rec]) : Option[Int] = ...
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
def post_process(rs: List[Rec]) : List[Rec] = ...
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
// test cases:
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
//val recs = get_csv("log.csv").map(process_line)
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
//post_process(recs)(4).children   // List(12)
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
//post_process(recs)(23).children  // List(16,26)
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
//post_process(recs)(8).parent     // None
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
//post_process(recs)(9).parent     // Some(7)
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
//post_process(recs)(16).parent    // Some(23)
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
//post_process(recs)(26).parent    // Some(23)
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
//=============
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
// (4) The next two functions calculate the countries where
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
//     message authors are coming from and how many authors
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
//     come from each country (returned as a Map from countries
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
//     to numbers). In case an author did not specify a country,
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
//     the empty string is returned.
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
def get_countries(rs: List[Rec]) : Set[String] = ...
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
  
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
def get_countries_numbers(rs: List[Rec]) :  Map[String, Int] = ...
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
// test cases:
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
//val recs = get_csv("log.csv").map(process_line)
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
// get_countries(recs) => 
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
//    Set("", Poland, Lebanon, Trinidad and
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
//        Tobago, Japan, Spain, Nigeria, Peru, India, Lithuania, Hong
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
//        Kong, Italy, Ireland, Uganda, England, Bangladesh, China,
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
//        Romania, Slovakia, United Kingdom, Norway, Pakistan, Indonesia,
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
//        Malaysia, Turkey, Portugal, Hungary)
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
//
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 336
diff changeset
   154
// get_countries_numbers(recs) => 
336
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
//    Map("" -> 4, Poland -> 2, Lebanon -> 1, Trinidad and Tobago -> 2,
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
//        Japan -> 1, Spain -> 1, Nigeria -> 1, Peru -> 1, India -> 6,
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
//        Lithuania -> 1, Hong Kong -> 3, Italy -> 1, Ireland -> 2, Uganda -> 1,
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
//        England -> 4, Bangladesh -> 2, China -> 4, Romania -> 3,
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
//        Slovakia -> 1, United Kingdom -> 34, Norway -> 1, Pakistan -> 2,
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
//        Indonesia -> 2, Malaysia -> 1, Turkey -> 1, Portugal -> 1,
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
//        Hungary -> 2)
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
//============= 
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
// (5) The function ordered_thread_sizes orders the message threads
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
// according to how many answers were given for one message (that is
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
// how many children, grand-children and so on one message has).
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
// The auxiliary function search enumerates all children,
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
// grand-children and so on for a given record r (including the record
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
// itself). Search returns the children and so on as a list of Recs.
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
// The function thread_size generates for a record, say r, a pair
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
// consisting of the number of r and the number of all children as
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
// produced by search. 
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
// The function ordered_thread_sizes orders than the list of pairs
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
// according to which thread in the chat is the longest.
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
def search(r: Rec, rs: List[Rec]) : List[Rec] = ...
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
  
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
def thread_size(r: Rec, rs: List[Rec]) : (Int, Int) = ...
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
 
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
def ordered_thread_sizes(rs: List[Rec]) : List[(Int, Int)] = ...
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
 
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
// test cases: 
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
//val recs_p = post_process(get_csv("log.csv").map(process_line))
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
//search(recs_p(459), recs_p).map(_.num)
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
//    => List(459, 401, 404, 426, 428, 399, 377, 357, 325, 304)
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
//thread_size(recs_p(459), recs_p) 
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
//    => (459,10)
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
//
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
//ordered_thread_sizes(recs_p).take(4)
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
//    => List((402,18), (95,12), (488,11), (459,10))
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
25d9c3b2bc99 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
}