class Moneyball::Parser

Attributes

node[R]

Public Class Methods

new(node) click to toggle source
# File lib/moneyball/parser.rb, line 3
def initialize(node)
  @node = node
end

Public Instance Methods

stats() click to toggle source
# File lib/moneyball/parser.rb, line 7
def stats
  {
    pa: pa,
    ab: ab,
    h: h,
    h_1b: h_1b,
    h_2b: h_2b,
    h_3b: h_3b,
    hr: hr,
    bb: bb,
    ibb: ibb,
    k: k,
    roe: roe,
    gidp: gidp,
    dp: dp,
    tp: tp,
    hbp: hbp,
    sf: sf,
    sh: sh,
    fc: fc,
    bi: bi,
    ci: ci,
    fi: fi,
    r: r,
    rbi: rbi
  }
end

Private Instance Methods

ab() click to toggle source
# File lib/moneyball/parser.rb, line 73
def ab
  @ab ||= any_outcomes_occurred([
    h,
    k,
    batted_out,
    double_play,
    tp,
    roe,
    force_out,
    fc,
    bi,
    fi
  ])
end
any_outcomes_occurred(outcomes) click to toggle source
# File lib/moneyball/parser.rb, line 65
def any_outcomes_occurred(outcomes)
  if outcomes.any? { |outcome| outcome == 1 }
    1
  else
    0
  end
end
batted_out() click to toggle source
# File lib/moneyball/parser.rb, line 166
def batted_out
  @batter_out ||= event_matches(/(Fly|Ground|Line|Pop)( )?(O|o)ut|Grounded Into DP/)
end
batter_id() click to toggle source
# File lib/moneyball/parser.rb, line 41
def batter_id
  @batter_id ||= node.attribute("batter").value
end
bb() click to toggle source
# File lib/moneyball/parser.rb, line 92
def bb
  @bb ||= event_matches(/Walk/)
end
bi() click to toggle source
# File lib/moneyball/parser.rb, line 88
def bi
  @bi ||= event_matches(/Batter Interference/)
end
ci() click to toggle source
# File lib/moneyball/parser.rb, line 100
def ci
  @ci ||= event_matches(/Catcher Interference/)
end
double_play() click to toggle source
# File lib/moneyball/parser.rb, line 120
def double_play
  @double_play ||= event_matches(/((D|d)ouble (P|p)lay| - DP)/)
end
dp() click to toggle source
# File lib/moneyball/parser.rb, line 108
def dp
  @dp ||= any_outcomes_occurred([gidp, double_play, sacrifice_double_play])
end
event_matches(regex) click to toggle source
# File lib/moneyball/parser.rb, line 57
def event_matches(regex)
  if event_value.match(regex)
    1
  else
    0
  end
end
event_value() click to toggle source
# File lib/moneyball/parser.rb, line 53
def event_value
  @event_value ||= node.attribute("event").value
end
fc() click to toggle source
# File lib/moneyball/parser.rb, line 124
def fc
  @fc ||= event_matches(/Fielders Choice( Out)?/)
end
fi() click to toggle source
# File lib/moneyball/parser.rb, line 128
def fi
  @fi ||= event_matches(/Fan (I|i)nterference/)

  if @fi
    parse_hit_from_description
  end

  @fi
end
force_out() click to toggle source
# File lib/moneyball/parser.rb, line 170
def force_out
  @force_out ||= event_matches(/Forceout/)
end
gidp() click to toggle source
# File lib/moneyball/parser.rb, line 104
def gidp
  @gidp ||= event_matches(/((G|g)rounded (I|i)nto|Sacrifice Bunt) DP/)
end
h() click to toggle source
# File lib/moneyball/parser.rb, line 138
def h
  @h ||= any_outcomes_occurred([h_1b, h_2b, h_3b, hr])
end
h_1b() click to toggle source
# File lib/moneyball/parser.rb, line 146
def h_1b
  @h_1b ||= event_matches(/(S|s)ingle/)
end
h_2b() click to toggle source
# File lib/moneyball/parser.rb, line 150
def h_2b
  @h_2b ||= event_matches(/(D|d)ouble\Z/)
end
h_3b() click to toggle source
# File lib/moneyball/parser.rb, line 154
def h_3b
  @h_3b ||= event_matches(/(T|t)riple?\Z/)
end
hbp() click to toggle source
# File lib/moneyball/parser.rb, line 142
def hbp
  @hbp ||= event_matches(/Hit By Pitch/)
end
hr() click to toggle source
# File lib/moneyball/parser.rb, line 158
def hr
  @hr ||= event_matches(/(H|h)ome (R|r)un/)
end
ibb() click to toggle source
# File lib/moneyball/parser.rb, line 96
def ibb
  @ibb ||= event_matches(/(I|i)ntent(ional)? (W|w)alk/)
end
k() click to toggle source
# File lib/moneyball/parser.rb, line 162
def k
  @k ||= event_matches(/(S|s)trikeout/)
end
pa() click to toggle source
# File lib/moneyball/parser.rb, line 174
def pa
  @pa ||= if !event_value.match(/Runner Out/)
    1
  else
    0
  end
end
parse_hit_from_description() click to toggle source
# File lib/moneyball/parser.rb, line 194
def parse_hit_from_description
  description = node.attribute("des").value

  if description.match(/singles/)
    @h = 1
    @h_1b = 1
  elsif description.match(/doubles/)
    @h = 1
    @h_2b = 1
  elsif description.match(/ground-rule double/)
    @h = 1
    @h_2b = 1
  elsif description.match(/triples/)
    @h = 1
    @h_3b = 1
  elsif description.match(/hits a sacrifice bunt/) && description.match(/error/)
    @roe = 1
  end
end
r() click to toggle source
# File lib/moneyball/parser.rb, line 45
def r
  @r = node.search("runner[id='#{batter_id}'][score='T']").any? ? 1 : 0
end
rbi() click to toggle source
# File lib/moneyball/parser.rb, line 49
def rbi
  @r = node.search("runner[rbi='T']").count
end
roe() click to toggle source
# File lib/moneyball/parser.rb, line 182
def roe
  @reached_on_error ||= event_matches(/Field Error/)
end
sacrifice_double_play() click to toggle source
# File lib/moneyball/parser.rb, line 116
def sacrifice_double_play
  @sacrifice_double_play ||= event_matches(/Sac (Bunt|Fly) DP/)
end
sf() click to toggle source
# File lib/moneyball/parser.rb, line 186
def sf
  @sf ||= event_matches(/Sac Fly/)
end
sh() click to toggle source
# File lib/moneyball/parser.rb, line 190
def sh
  @sh ||= event_matches(/Sac(rifice)? Bunt/)
end
tp() click to toggle source
# File lib/moneyball/parser.rb, line 112
def tp
  @tp ||= event_matches(/(T|t)riple (P|p)lay/)
end