Skip to content

Instantly share code, notes, and snippets.

@ryankohl
Created April 29, 2011 03:04
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/947772 to your computer and use it in GitHub Desktop.
Save ryankohl/947772 to your computer and use it in GitHub Desktop.
NHL Play-by-Play Analysis
(ns nhl.analysis
(:use [seabass core] )
(:use [incanter core stats charts] ))
(defn get-data[ n m] (map #(str "data/file-" % ".nt") (range n (+ 1 m))))
(defn get-model [n m] (apply build (get-data n m)))
(def ont "files/ontology.ttl")
(def rules "files/nhl.rules")
(defn penaltyDef [pen match]
(str "prefix nhl: <http://www.nhl.com/> construct { ?e a nhl:" pen "} "
"where { ?e a nhl:Penalty . ?e nhl:desc ?d . FILTER regex(?d, '.*("
match ").*')}"))
(def boarding (penaltyDef "Boarding" "BOARDING"))
(def charging (penaltyDef "Charging" "CHARGING"))
(def cross-checking (penaltyDef "CrossChecking" "CROSS CHECKING"))
(def delay-of-game (penaltyDef "DelayOfGame" "DELAYING GAME-PUCK OVER GLASS"))
(def elbowing (penaltyDef "Elbowing" "ELBOWING"))
(def fighting-maj (penaltyDef "FightingMaj" "FIGHTING (MAJ)"))
(def highsticking (penaltyDef "HighSticking" "HI-STICKING"))
(def highsticking-dm (penaltyDef "HighSticking-DoubleMinor" "HI-STICK - DOUBLE MINOR"))
(def holding (penaltyDef "Holding" "HOLDING"))
(def hooking (penaltyDef "Hooking" "HOOKING"))
(def interference (penaltyDef "Interference" "INTERFERENCE"))
(def misconduct (penaltyDef "Misconduct" "MISCONDUCT (10 MIN)"))
(def roughing (penaltyDef "Roughing" "ROUGHING"))
(def slashing (penaltyDef "Slashing" "SLASHING"))
(def tripping (penaltyDef "Tripping" "TRIPPING"))
(def unsportsmanlike (penaltyDef "Unsportsmanlike" "UNSPORTSMANLIKE CONDUCT"))
(def totalPenaltyMinutes "
prefix nhl: <http://www.nhl.com/>
construct { ?g nhl:totalPenaltyMinutes ?value }
where {
select ?g (sum(?minutes) as ?value)
where { ?g nhl:play ?p . ?p a nhl:Penalty . ?p a ?c . ?c nhl:penaltyMinutes ?minutes }
group by ?g }
")
(def totalHits "
prefix nhl: <http://www.nhl.com/>
construct { ?g nhl:totalHits ?value }
where {
select ?g (count(?e) as ?value)
where { ?g nhl:play ?e . ?e a nhl:Hit . }
group by ?g }
")
(def totalEnforcerActions "
prefix nhl: <http://www.nhl.com/>
construct { ?g nhl:totalEnforcerActions ?value }
where {
select ?g (count(?e) as ?value)
where { ?g nhl:play ?e . ?e a nhl:EnforcerAction .}
group by ?g }
")
(def enforcer1 "
prefix nhl: <http://www.nhl.com/>
construct { _:x a nhl:Enforcement . _:x nhl:game ?g .
_:x nhl:actor ?p1 . _:x nhl:value ?value }
where {
select ?g ?p1 (sum(?minutes) as ?value)
where {
?g nhl:play ?e . ?g nhl:play ?e2 .
?e a nhl:EnforcerPenalty . ?e nhl:agent1 ?p1 . ?e nhl:agent2 ?p2 .
?e a ?class . ?class nhl:penaltyMinutes ?minutes .
?e2 nhl:agent1 ?p2 . ?e2 a nhl:ViolentPenalty . }
group by ?g ?p1}
")
(def enforcer2 "
prefix nhl: <http://www.nhl.com/>
construct { _:x a nhl:Enforcement . _:x nhl:game ?g .
_:x nhl:actor ?p1 . _:x nhl:value ?value }
where {
select ?g ?p1 (count(?e) as ?value)
where {
?g nhl:play ?e . ?g nhl:play ?e2 .
?e a nhl:Hit. ?e nhl:agent1 ?p1 . ?e nhl:agent2 ?p2 .
?e2 nhl:agent1 ?p2 . ?e2 a nhl:ViolentPenalty . }
group by ?g ?p1 }
")
(def enforcer3 "
prefix nhl: <http://www.nhl.com/>
construct { _:x a nhl:Enforcement . _:x nhl:game ?g .
_:x nhl:actor ?p1 . _:x nhl:value ?value }
where {
select ?g ?p1 (count(?e) as ?value)
where {
?g nhl:play ?e . ?g nhl:play ?e2 .
?e nhl:agent1 ?p1 . ?e nhl:agent2 ?p2 .
?e2 nhl:agent1 ?p2 . ?e2 a nhl:ViolentPenalty .
?e a nhl:EnforcerAction . ?e nhl:influences ?e2 }
group by ?g ?p1 }
")
(let [ n1 (get-model 1 10)
n2 (build n1
(pull cross-checking n1) (pull charging n1)
(pull fighting-maj n1) (pull highsticking n1)
(pull roughing n1) (pull slashing n1)
(pull unsportsmanlike n1)
ont rules) ]
(def m1 (build n2 (pull totalPenaltyMinutes n2) (pull enforcer1 n2) rules))
(def m2 (build n2 (pull totalHits n2) (pull enforcer2 n2) rules))
(def m3 (build n2 (pull totalEnforcerActions n2) (pull enforcer3 n2) rules)) )
(def enforcement1 "
prefix nhl: <http://www.nhl.com/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?name (sum(?enfMinutes / ?totMinutes) as ?value)
where { ?x a nhl:Enforcement . ?x nhl:actor ?player . ?x nhl:value ?enfMinutes .
?x nhl:game ?game . ?game nhl:totalPenaltyMinutes ?totMinutes .
?player rdfs:label ?name}
group by ?name
order by desc(?value)
")
(def enforcement2 "
prefix nhl: <http://www.nhl.com/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?name (sum(?enfHits/ ?totHits) as ?value)
where { ?x a nhl:Enforcement . ?x nhl:actor ?player . ?x nhl:value ?enfHits .
?x nhl:game ?game . ?game nhl:totalHits ?totHits .
?player rdfs:label ?name}
group by ?name
order by desc(?value)
")
(def enforcement3 "
prefix nhl: <http://www.nhl.com/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?name (sum(?enfActs/ ?totActs) as ?value)
where { ?x a nhl:Enforcement . ?x nhl:actor ?player . ?x nhl:value ?enfActs .
?x nhl:game ?game . ?game nhl:totalEnforcerActions ?totActs .
?player rdfs:label ?name}
group by ?name
order by desc(?value)
")
(def detailed "
select ?cs ?p (count(?s) as ?cnt)
where { ?s a ?cs . ?s ?p ?o }
group by ?cs ?p
order by ?cs ?cnt
")
(def penalties "
prefix nhl: <http://www.nhl.com/>
select distinct ?d
where { ?p a nhl:Penalty . ?p nhl:desc ?d }
")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment