Module: SportDb::FixtureHelpers

Included in:
Reader
Defined in:
lib/sportdb/utils.rb

Instance Method Summary collapse

Instance Method Details

#cut_off_end_of_line_comment!(line) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/sportdb/utils.rb', line 78

def cut_off_end_of_line_comment!( line )
  #  cut off (that is, remove) optional end of line comment starting w/ #
  
  line.sub!( /#.*$/ ) do |_|
    logger.debug "   cutting off end of line comment - >>#{$&}<<"
    ''
  end
  
  # NB: line = line.sub  will NOT work - thus, lets use line.sub!
end

#find_date!(line) ⇒ Object

method find_round_pos!



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/sportdb/utils.rb', line 153

def find_date!( line )
  # extract date from line
  # and return it
  # NB: side effect - removes date from line string
  
  # e.g. 2012-09-14 20:30   => YYYY-MM-DD HH:MM
  #  nb: allow 2012-9-3 7:30 e.g. no leading zero required
  regex_db = /\b(\d{4})-(\d{1,2})-(\d{1,2})\s+(\d{1,2}):(\d{2})\b/
  
  # e.g. 2012-09-14  w/ implied hours (set to 12:00)
  #  nb: allow 2012-9-3 e.g. no leading zero required
  regex_db2 = /\b(\d{4})-(\d{1,2})-(\d{1,2})\b/

  # e.g. 14.09. 20:30  => DD.MM. HH:MM
  #  nb: allow 2.3.2012 e.g. no leading zero required
  #  nb: allow hour as 20.30  or 3.30 instead of 03.30
  regex_de = /\b(\d{1,2})\.(\d{1,2})\.\s+(\d{1,2})[:.](\d{2})\b/

  # e.g. 14.09.2012 20:30   => DD.MM.YYYY HH:MM
  #  nb: allow 2.3.2012 e.g. no leading zero required
  #  nb: allow hour as 20.30
  regex_de2 = /\b(\d{1,2})\.(\d{1,2})\.(\d{4})\s+(\d{1,2})[:.](\d{2})\b/

  # e.g. 14.09.2012  => DD.MM.YYYY w/ implied hours (set to 12:00)
  regex_de3 = /\b(\d{1,2})\.(\d{1,2})\.(\d{4})\b/


  month_abbrev_en = "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec"

  # e.g. 12 May 2013 14:00  => D|DD.MMM.YYYY H|HH:MM
  regex_en = /\b(\d{1,2})\s(#{month_abbrev_en})\s(\d{4})\s+(\d{1,2}):(\d{2})\b/


  if line =~ regex_db
    value = '%d-%02d-%02d %02d:%02d' % [$1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i]
    logger.debug "   date: >#{value}<"

    ## todo: lets you configure year
    ##  and time zone (e.g. cet, eet, utc, etc.)
    
    line.sub!( regex_db, '[DATE.DB]' )

    return DateTime.strptime( value, '%Y-%m-%d %H:%M' )
  elsif line =~ regex_db2
    value = '%d-%02d-%02d 12:00' % [$1.to_i, $2.to_i, $3.to_i]
    logger.debug "   date: >#{value}<"

    line.sub!( regex_db2, '[DATE.DB2]' )

    return DateTime.strptime( value, '%Y-%m-%d %H:%M' )
  elsif line =~ regex_de2
    value = '%d-%02d-%02d %02d:%02d' % [$3.to_i, $2.to_i, $1.to_i, $4.to_i, $5.to_i]
    logger.debug "   date: >#{value}<"

    ## todo: lets you configure year
    ##  and time zone (e.g. cet, eet, utc, etc.)
    
    line.sub!( regex_de2, '[DATE.DE2]' )

    return DateTime.strptime( value, '%Y-%m-%d %H:%M' )
  elsif line =~ regex_de
    
    #### fix/todo:
    #  get year from event start date!!!!
    #  do NOT hard code!!!!

    value = '2012-%02d-%02d %02d:%02d' % [$2.to_i, $1.to_i, $3.to_i, $4.to_i]
    logger.debug "   date: >#{value}<"

    ## todo: lets you configure year
    ##  and time zone (e.g. cet, eet, utc, etc.)
    
    line.sub!( regex_de, '[DATE.DE]' )

    return DateTime.strptime( value, '%Y-%m-%d %H:%M' )
  elsif line =~ regex_de3
    value = '%d-%02d-%02d 12:00' % [$3.to_i, $2.to_i, $1.to_i]
    logger.debug "   date: >#{value}<"

    ## todo: lets you configure year
    ##  and time zone (e.g. cet, eet, utc, etc.)
    
    line.sub!( regex_de3, '[DATE.DE3]' )

    return DateTime.strptime( value, '%Y-%m-%d %H:%M' )
  elsif line =~ regex_en
    value = '%d-%s-%02d %02d:%02d' % [$3.to_i, $2, $1.to_i, $4.to_i, $5.to_i]
    logger.debug "   date: >#{value}<"

    line.sub!( regex_en, '[DATE.EN]' )

    return DateTime.strptime( value, '%Y-%b-%d %H:%M' )   ## %b - abbreviated month name (e.g. Jan,Feb, etc.)
  else
    return nil
  end
end

#find_game_pos!(line) ⇒ Object



351
352
353
354
# File 'lib/sportdb/utils.rb', line 351

def find_game_pos!( line )
  ## fix: add depreciation warning - remove - use find_leading_pos!
  find_leading_pos!( line )
end

#find_group_title_and_pos!(line) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sportdb/utils.rb', line 41

def find_group_title_and_pos!( line )
  ## group pos - for now support single digit e.g 1,2,3 or letter e.g. A,B,C or HEX
  ## nb:  (?:)  = is for non-capturing group(ing)
  regex = /(?:Group|Gruppe|Grupo)\s+((?:\d{1}|[A-Z]{1,3}))\b/
  
  match = regex.match( line )
  
  return [nil,nil] if match.nil?

  pos = case match[1]
        when 'A' then 1
        when 'B' then 2
        when 'C' then 3
        when 'D' then 4
        when 'E' then 5
        when 'F' then 6
        when 'G' then 7
        when 'H' then 8
        when 'I' then 9
        when 'J' then 10
        when 'K' then 11
        when 'L' then 12
        when 'HEX' then 666    # HEX for Hexagonal - todo/check: map to something else ??
        else  match[1].to_i
        end

  title = match[0]

  logger.debug "   title: >#{title}<"
  logger.debug "   pos: >#{pos}<"
    
  line.sub!( regex, '[GROUP|TITLE+POS]' )

  return [title,pos]
end

#find_leading_pos!(line) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/sportdb/utils.rb', line 334

def find_leading_pos!( line )
  # extract optional game pos from line
  # and return it
  # NB: side effect - removes pos from line string

  # e.g.  (1)   - must start line 
  regex = /^[ \t]*\((\d{1,3})\)[ \t]+/
  if line =~ regex
    logger.debug "   pos: >#{$1}<"

    line.sub!( regex, '[POS] ' ) # NB: add trailing space
    return $1.to_i
  else
    return nil
  end
end

#find_person!(line) ⇒ Object



446
447
448
# File 'lib/sportdb/utils.rb', line 446

def find_person!( line )
  TextUtils.find_key_for!( 'person', line )
end

#find_record_comment!(line) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/sportdb/utils.rb', line 251

def find_record_comment!( line )
  # assume everything left after the last record marker,that is, ] is a record comment

  regex = /]([^\]]+?)$/   # NB: use non-greedy +?

  if line =~ regex
    value = $1.strip
    return nil if value.blank?   # skip whitespaces only

    logger.debug "   comment: >#{value}<"

    line.sub!( value, '[REC.COMMENT] ' )
    return value
  else
    return nil
  end
end

#find_record_laps!(line) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/sportdb/utils.rb', line 306

def find_record_laps!( line )
  # e.g.  first free-standing number w/ one or two digits e.g. 7 or 28 etc.
  regex = /\b(\d{1,2})\b/
  if line =~ regex
    logger.debug "   laps: >#{$1}<"
    
    line.sub!( regex, '[REC.LAPS] ' ) # NB: add trailing space
    return $1.to_i
  else
    return nil
  end
end

#find_record_leading_state!(line) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/sportdb/utils.rb', line 319

def find_record_leading_state!( line )
  # e.g.  1|2|3|etc or Ret  - must start line 
  regex = /^[ \t]*(\d{1,3}|Ret)[ \t]+/
  if line =~ regex
    value = $1.dup
    logger.debug "   state: >#{value}<"

    line.sub!( regex, '[REC.STATE] ' ) # NB: add trailing space
    return value
  else
    return nil
  end
end

#find_record_timeline!(line) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/sportdb/utils.rb', line 270

def find_record_timeline!( line )

  #  +1 lap or +n laps
  regex_laps = /\s+\+\d{1,2}\s(lap|laps)\b/

  #  2:17:15.123
  regex_time = /\b\d{1,2}:\d{2}:\d{2}\.\d{1,3}\b/

  #  +40.1 secs
  regex_secs = /\s+\+\d{1,3}\.\d{1,3}\s(secs)\b/   # NB: before \+ - boundry (\b) will not work 

  # NB: $& contains the complete matched text

  if line =~ regex_laps
    value = $&.strip
    logger.debug "   timeline.laps: >#{value}<"

    line.sub!( value, '[REC.TIMELINE.LAPS] ' ) # NB: add trailing space
    return value
  elsif line =~ regex_time
    value = $&.strip
    logger.debug "   timeline.time: >#{value}<"

    line.sub!( value, '[REC.TIMELINE.TIME] ' ) # NB: add trailing space
    return value
  elsif line =~ regex_secs
    value = $&.strip
    logger.debug "   timeline.secs: >#{value}<"

    line.sub!( value, '[REC.TIMELINE.SECS] ' ) # NB: add trailing space
    return value
  else
    return nil
  end
end

#find_round_pos!(line) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/sportdb/utils.rb', line 126

def find_round_pos!( line )
  ## fix/todo:
  ##  if no round found assume last_pos+1 ??? why? why not?

  # extract optional round pos from line
  # e.g.  (1)   - must start line 
  regex_pos = /^[ \t]*\((\d{1,3})\)[ \t]+/

  ## find free standing number
  regex_num = /\b(\d{1,3})\b/

  if line =~ regex_pos
    logger.debug "   pos: >#{$1}<"
    
    line.sub!( regex_pos, '[ROUND|POS] ' )  ## NB: add back trailing space that got swallowed w/ regex -> [ \t]+
    return $1.to_i
  elsif line =~ regex_num
    ## assume number in title is pos (e.g. Jornada 3, 3 Runde etc.)
    ## NB: do NOT remove pos from string (will get removed by round title)
    logger.debug "   pos: >#{$1}<"
    return $1.to_i
  else
    ## fix: add logger.warn no round pos found in line
    return nil
  end
end

#find_round_title!(line) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/sportdb/utils.rb', line 104

def find_round_title!( line )
  # assume everything left is the round title
  #  extract all other items first (round title2, round pos, group title n pos, etc.)

  buf = line.dup
  logger.debug "  find_round_title! line-before: >>#{buf}<<"

  buf.gsub!( /\[.+?\]/, '' )   # e.g. remove [ROUND|POS], [ROUND|TITLE2], [GROUP|TITLE+POS] etc.
  buf.sub!( /\s+[\/\-]{1,}\s+$/, '' )    # remove optional trailing / or / chars (left over from group)
  buf.strip!    # remove leading and trailing whitespace

  logger.debug "  find_round_title! line-after: >>#{buf}<<"

  ### bingo - assume what's left is the round title

  logger.debug "   title: >>#{buf}<<"
  line.sub!( buf, '[ROUND|TITLE]' )

  buf
end

#find_round_title2!(line) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sportdb/utils.rb', line 90

def find_round_title2!( line )
  # assume everything after // is title2 - strip off leading n trailing whitespaces
  regex = /\/{2,}\s*(.+)\s*$/
  if line =~ regex
    logger.debug "   title2: >#{$1}<"
    
    line.sub!( regex, '[ROUND|TITLE2]' )
    return $1
  else
    return nil    # no round title2 found (title2 is optional)
  end
end

#find_scores!(line) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/sportdb/utils.rb', line 356

def find_scores!( line )

  ### fix: depending on language allow 1:1 or 1-1
  ##   do NOT allow mix and match
  ##  e.g. default to en is  1-1
  ##    de is 1:1 etc.
  

  # extract score from line
  # and return it
  # NB: side effect - removes date from line string
  
  # e.g. 1:2 or 0:2 or 3:3 // 1-1 or 0-2 or 3-3
  regex = /\b(\d)[:\-](\d)\b/
  
  # e.g. 1:2nV  => overtime
  regex_ot = /\b(\d)[:\-](\d)[ \t]?[nN][vV]\b/
  
  # e.g. 5:4iE  => penalty
  regex_p = /\b(\d)[:\-](\d)[ \t]?[iI][eE]\b/
  
  scores = []
  
  if line =~ regex
    logger.debug "   score: >#{$1}-#{$2}<"
    
    line.sub!( regex, '[SCORE]' )

    scores << $1.to_i
    scores << $2.to_i
    
    if line =~ regex_ot
      logger.debug "   score.ot: >#{$1}-#{$2}<"
    
      line.sub!( regex_ot, '[SCORE.OT]' )

      scores << $1.to_i
      scores << $2.to_i
    
      if line =~ regex_p
        logger.debug "   score.p: >#{$1}-#{$2}<"
    
        line.sub!( regex_p, '[SCORE.P]' )

        scores << $1.to_i
        scores << $2.to_i
      end
    end
  end
  scores
end

#find_team!(line) ⇒ Object

NB: returns key (string or nil)



414
415
416
# File 'lib/sportdb/utils.rb', line 414

def find_team!( line )  # NB: returns key (string or nil)
  TextUtils.find_key_for!( 'team', line )
end

#find_team1!(line) ⇒ Object

todo: check if find_team1 gets used? if not remove it!! use find_teams!



419
420
421
# File 'lib/sportdb/utils.rb', line 419

def find_team1!( line )
  TextUtils.find_key_for!( 'team1', line )
end

#find_team2!(line) ⇒ Object



423
424
425
# File 'lib/sportdb/utils.rb', line 423

def find_team2!( line )
  TextUtils.find_key_for!( 'team2', line )
end

#find_teams!(line) ⇒ Object

NB: returns an array - note: plural! (teamsss)



410
411
412
# File 'lib/sportdb/utils.rb', line 410

def find_teams!( line ) # NB: returns an array - note: plural! (teamsss)
  TextUtils.find_keys_for!( 'team', line )
end

#find_track!(line) ⇒ Object



437
438
439
# File 'lib/sportdb/utils.rb', line 437

def find_track!( line )
  TextUtils.find_key_for!( 'track', line )
end

#is_group?(line) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
# File 'lib/sportdb/utils.rb', line 17

def is_group?( line )
  # NB: check after is_round? (round may contain group reference!)
  line =~ SportDb.lang.regex_group
end

#is_knockout_round?(line) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sportdb/utils.rb', line 22

def is_knockout_round?( line )

  ## todo: check for adding ignore case for regex (e.g. 1st leg/1st Leg)

  if line =~ SportDb.lang.regex_leg1
    logger.debug "  two leg knockout; skip knockout flag on first leg"
    false
  elsif line =~ SportDb.lang.regex_knockout_round
    logger.debug "   setting knockout flag to true"
    true
  elsif line =~ /K\.O\.|K\.o\.|Knockout/
      ## NB: add two language independent markers, that is, K.O. and Knockout
    logger.debug "   setting knockout flag to true (lang independent marker)"
    true
  else
    false
  end
end

#is_postponed?(line) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
# File 'lib/sportdb/utils.rb', line 8

def is_postponed?( line )
  # check if line include postponed marker e.g. =>
  line =~ /=>/
end

#is_round?(line) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/sportdb/utils.rb', line 13

def is_round?( line )
  line =~ SportDb.lang.regex_round
end

#map_person!(line) ⇒ Object



450
451
452
# File 'lib/sportdb/utils.rb', line 450

def map_person!( line )
  TextUtils.map_titles_for!( 'person', line, @known_persons)
end

#map_team!(line) ⇒ Object

alias map_teams!



433
434
435
# File 'lib/sportdb/utils.rb', line 433

def map_team!( line )  # alias map_teams!
  map_teams!( line )
end

#map_teams!(line) ⇒ Object

todo/fix: pass in known_teams as a parameter? why? why not?



429
430
431
# File 'lib/sportdb/utils.rb', line 429

def map_teams!( line )
  TextUtils.map_titles_for!( 'team', line, @known_teams )
end

#map_track!(line) ⇒ Object

todo/fix: pass in known_tracks as a parameter? why? why not?



442
443
444
# File 'lib/sportdb/utils.rb', line 442

def map_track!( line )
  TextUtils.map_titles_for!( 'track', line, @known_tracks )
end

#match_teams!(line) ⇒ Object

depreciated methods - use map_



457
458
459
460
# File 'lib/sportdb/utils.rb', line 457

def match_teams!( line )   ## fix: rename to map_teams!! - remove match_teams!
  ## todo: issue depreciated warning
  map_teams!( line )
end

#match_track!(line) ⇒ Object

method match_teams!



462
463
464
465
# File 'lib/sportdb/utils.rb', line 462

def match_track!( line )  ## fix: rename to map_track!!!
  ## todo: issue depreciated warning
  map_track!( line )
end