(* Based on the code by Ilmari Heikkinen. Dead code removed, and minor * modifications with no consequences on speed. *) let add_to_key hash key value = Hashtbl.replace hash key ((try (Hashtbl.find hash key) with Not_found -> 0) + value) let process_line hash str re = try ignore (Str.search_forward re str 0); add_to_key hash (Str.matched_group 1 str) 1 with Not_found -> () let sort_results hash = let list = Hashtbl.fold (fun url hits lst -> (url,hits) :: lst) hash [] in List.sort (fun (_,a_hits) (_,b_hits) -> b_hits - a_hits) list let rec print_top_n n = function [] -> () | ((url, hits) :: tl) -> if n > 0 then begin print_endline ((string_of_int hits) ^ ": " ^ url); print_top_n (n-1) tl end let line_finder filename = let ic = open_in filename in let re = (Str.regexp "GET /ongoing/When/[0-9][0-9][0-9]x/\\([0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]/[^ .]+\\) ") in let hash = Hashtbl.create 2000 in try while true do process_line hash (input_line ic) re done with End_of_file -> print_top_n 10 (sort_results hash) let () = line_finder (Array.get Sys.argv 1)