sundbp
8/11/2017 - 12:04 PM

leaderboard

;;; leaderboard

(defn parse-goal-csv
  [filename]
  (->> (drop 1 (csv/read-csv (slurp filename)))
       (map (fn [r]
              {:season         (nth r 0)
               :league_id      (to-int (nth r 1))
               :date           (nth r 2)
               :match_id       (to-int (nth r 3))
               :home_team_id   (to-int (nth r 4))
               :home_team_name (nth r 5)
               :away_team_id   (to-int (nth r 6))
               :away_team_name (nth r 7)
               :goal_scorer    (to-int (nth r 8))
               :scoring_team   (to-int (nth r 9))
               :goal_time      (to-int (nth r 10))}))))

(defn goals-by-player
  [goals]
  (reduce (fn [ret g]
            (let [existing (get ret (:goal_scorer g) 0)]
              (assoc ret (:goal_scorer g) (inc existing))))
          {}
          goals))

(defn get-player-lookup
  [filename]
  (->> (csv/read-csv (slurp filename))
       (drop 1)
       (reduce (fn [ret r]
                 (assoc ret
                        (to-int (nth r 1))
                        (nth r 2)))
               {})))

(defn leaderboard
  []
  (let [players   (get-player-lookup "/Users/sundbp/Downloads/soccer/players.csv")
        by-player (->> (parse-goal-csv "/Users/sundbp/Downloads/soccer/my-goals.csv")
                       goals-by-player
                       (sort-by second >)
                       (map (fn [entry]
                              {:player (get players (first entry))
                               :goals  (second entry)})))]
    (clojure.pprint/print-table [:player :goals] (take 10 by-player))))