Skip to content

Instantly share code, notes, and snippets.

@ryankohl
Created April 28, 2011 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryankohl/945621 to your computer and use it in GitHub Desktop.
Save ryankohl/945621 to your computer and use it in GitHub Desktop.
Transform of NHL Json data into RDF N-TRIPLES
(ns nhl.parse
(:require [clojure.string]
[clojure.contrib [json :as json]] ))
(defn nope? [x] (or (= "" x) (= nil x)))
(defn blank [id] (str "_:b" @id) )
(defn node [namespace id] (if-not (or (nope? namespace) (nope? id)) (str "<" namespace id ">") ))
(defn str-lit [val] (if-not (nope? val) (str \" val \") ))
(defn int-lit [val] (if-not (nope? val) (str \" val \" "^^<http://www.w3.org/2001/XMLSchema#integer>") ))
(defn date-lit [val] (if-not (nope? val) (str \" val \" "^^<http://www.w3.org/2001/XMLSchema#date>") ))
(defn time-lit [val] (if-not (nope? val) (str \" val \" "^^<http://www.w3.org/2001/XMLSchema#time>") ))
(defn datetime-lit [val] (if-not (nope? val) (str \" val \" "^^<http://www.w3.org/2001/XMLSchema#dateTime>") ))
(defn duration-lit [val] (if-not (nope? val) (str \" val \" "^^<http://www.w3.org/2001/XMLSchema#duration>") ))
(defn decimal-lit [val] (if-not (nope? val) (str \" val \" "^^<http://www.w3.org/2001/XMLSchema#decimal>") ))
(defn fact [s p o] (if-not (or (nope? s) (nope? p) (nope? o)) (println (clojure.string/join " " [s p o]) " .")))
(def rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
(def rdfs "http://www.w3.org/2000/01/rdf-schema#")
(def nhl "http://www.nhl.com/")
(def *gid* (atom 0))
(defn fixLocalTime [time]
(let [ t (clojure.string/split time #":")
h (str (+ (Integer/parseInt (first t)) 12))
m (clojure.string/replace (str (first (rest t))) " PM" "")
s "00" ]
(clojure.string/join ":" [h m s]) ))
(defn print-play [play]
(let [ pid1 (if-not (nope? (:pid1 play)) (str "pid-" (:pid1 play)))
pid2 (if-not (nope? (:pid2 play)) (str "pid-" (:pid2 play)))
pid3 (if-not (nope? (:pid3 play)) (str "pid-" (:pid3 play)))
tid (if-not (nope? (:teamid play)) (str "team-" (:teamid play)))
eid (if-not (nope? (:formalEventId play))
(str "eid-" (clojure.string/replace (:formalEventId play) "\\" "")))
etyp (if-not (nope? (:type play)) (clojure.string/replace (:type play) "\\" "")) ]
(fact (blank *gid*) (node nhl "play") (node nhl eid))
(fact (node nhl eid) (node rdf "type") (node nhl etyp))
(fact (node nhl eid) (node nhl "desc") (str-lit (:desc play)))
(fact (node nhl eid) (node nhl "agent1") (node nhl pid1))
(fact (node nhl eid) (node nhl "agent2") (node nhl pid2))
(fact (node nhl eid) (node nhl "agent3") (node nhl pid3))
(fact (node nhl pid1) (node rdfs "label") (str-lit (:p1name play)))
(fact (node nhl pid2) (node rdfs "label") (str-lit (:p2name play)))
(fact (node nhl pid3) (node rdfs "label") (str-lit (:p3name play)))
(fact (node nhl eid) (node nhl "team") (node nhl tid))
(fact (node nhl eid) (node nhl "lat") (int-lit (:xcoord play)))
(fact (node nhl eid) (node nhl "lon") (int-lit (:ycoord play)))
(fact (node nhl eid) (node nhl "localtime") (time-lit (fixLocalTime (:localtime play))))
(fact (node nhl eid) (node nhl "time") (time-lit (str "00:" (:time play))))
(fact (node nhl eid) (node nhl "period") (int-lit (:period play))) ))
(defn print-game [game]
(let [ home (if-not (nope? (:hometeamid game)) (str "team-" (:hometeamid game)))
away (if-not (nope? (:awayteamid game)) (str "team-" (:awayteamid game))) ]
(swap! *gid* inc)
(fact (blank *gid*) (node nhl "hometeam") (node nhl home))
(fact (blank *gid*) (node nhl "awayteam") (node nhl away))
(fact (node nhl home) (node rdfs "label") (str-lit (:hometeamname game)))
(fact (node nhl away) (node rdfs "label") (str-lit (:awayteamname game)))
(doseq [p (:play (:plays game))] (print-play p)) ))
(defn parse [n m]
(doseq [ i (range n (+ m 1))]
(spit (str "output/file-" i ".nt")
(with-out-str (print-game (:game (:data (json/read-json (slurp (str "data/file-" i ".json"))))))))) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment