Class: Moneyball::BattedBallTypeExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/moneyball/batted_ball_type_extractor.rb

Instance Method Summary collapse

Constructor Details

#initialize(event_description) ⇒ BattedBallTypeExtractor

Returns a new instance of BattedBallTypeExtractor.



3
4
5
# File 'lib/moneyball/batted_ball_type_extractor.rb', line 3

def initialize(event_description)
  @event_description = event_description
end

Instance Method Details

#classificationObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/moneyball/batted_ball_type_extractor.rb', line 7

def classification
  case
  when event_description.match(/line(s)? (drive|into|out)/)
    "Line drive"
  when event_description.match(/fl(ies|y) (ball|out)/)
    "Fly ball"
  when event_description.match(/sacrifice fly/)
    "Fly ball"
  when event_description.match(/flies into a/)
    "Fly ball"
  when event_description.match(/ground(s)?( (ball|into|out)|, fielded)/)
    "Ground ball"
  when event_description.match(/fielder's choice/)
    "Ground ball"
  when event_description.match(/((hits|out on) a sacrifice|ground) bunt/)
    "Ground ball"
  when event_description.match(/reaches on a(n)? ((fielding |missed catch |throwing )?error|force attempt)/)
    "Ground ball"
  when event_description.match(/pop(s)? (into a(n)? (unassisted )?double play|(into a force )?out|up)/)
    "Pop up"
  when event_description.match(/(grand slam|home run)/)
    "Fly ball"
  when event_description.match(/(\AThrowing error|catcher interference|caught stealing|fan interference|hit by pitch|strikes out|out on strikes|walks)/)
    nil
  when event_description.match(/(passed ball|wild pitch).+out at/) then nil
  when event_description.match(/picks off/) then nil
  when event_description.match(/\A(\w+ challenged ([A-Za-z]|\s|\(|\)|\-|\,)+\:)?([A-Za-z]|\s)+(, Jr.)? out at /)
    nil
  when event_description.match(/ triples\.\s+\Z/) then nil
  when event_description.match(/ doubles\.\s+\Z/) then nil
  when event_description.match(/ singles\.\s+\Z/) then nil
  else
    raise "No match for batted ball type on '#{event_description}'"
    nil
  end
end