Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add support two digit scores e.g. 10-0

  • Loading branch information...
commit ca52bca0f36210d5123a0c9974c9e9a0cb199fd0 1 parent 7b87bec
@geraldb geraldb authored
View
77 lib/sportdb/utils.rb
@@ -387,8 +387,13 @@ def find_game_pos!( line )
find_leading_pos!( line )
end
+
def find_scores!( line )
+ # note: always call after find_dates !!!
+ # scores match date-like patterns!! e.g. 10-11 or 10:00 etc.
+ # -- note: score might have two digits too
+
### fix: depending on language allow 1:1 or 1-1
## do NOT allow mix and match
## e.g. default to en is 1-1
@@ -399,17 +404,32 @@ def find_scores!( line )
# and return it
# NB: side effect - removes date from line string
+ # note: regex should NOT match regex extra time or penalty
+ # thus, we do NOT any longer allow spaces for now between
+ # score and marker (e.g. nV,iE, etc.)
+
# 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/
-
+ regex = /\b(\d{1,2})[:\-](\d{1,2})\b/
+
+ ## todo: add/allow english markers e.g. aet ??
+
+ ## fix: use case insansitive flag instead e.g. /i
+ # instead of [nN] etc.
+
+ # e.g. 1:2nV => after extra time a.e.t
+ regex_et = /\b(\d{1,2})[:\-](\d{1,2})[nN][vV]\b/
+
+ # e.g. 5:4iE => penalty / after penalty a.p
+ regex_p = /\b(\d{1,2})[:\-](\d{1,2})[iI][eE]\b/
+
scores = []
-
+
+ ## todo: how to handle game w/o extra time
+ # but w/ optional penalty ??? e.g. used in copa liberatores, for example
+ # retrun 0,0 or nil,nil for extra time score ?? or -1, -1 ??
+ # for now use -1,-1 - check if nil,nil will crash upstream/downstream code??
+ # todo/fix: better use nil, nil ??
+
if line =~ regex
logger.debug " score: >#{$1}-#{$2}<"
@@ -417,24 +437,35 @@ def find_scores!( line )
scores << $1.to_i
scores << $2.to_i
-
- if line =~ regex_ot
- logger.debug " score.ot: >#{$1}-#{$2}<"
-
- line.sub!( regex_ot, '[SCORE.OT]' )
+ end
+
+ ## todo:
+ ## reverse matching order ??? allows us to support spaces for nV and iE
+ ## why? why not??
- scores << $1.to_i
- scores << $2.to_i
+ if line =~ regex_et
+ logger.debug " score.et: >#{$1}-#{$2}<"
- if line =~ regex_p
- logger.debug " score.p: >#{$1}-#{$2}<"
+ line.sub!( regex_et, '[SCORE.ET]' )
+
+ ## check scores empty? - fill with -1,-1
+ scores += [-1,-1] if scores.size == 0
+
+ scores << $1.to_i
+ scores << $2.to_i
+ end
+
+ if line =~ regex_p
+ logger.debug " score.p: >#{$1}-#{$2}<"
- line.sub!( regex_p, '[SCORE.P]' )
+ line.sub!( regex_p, '[SCORE.P]' )
- scores << $1.to_i
- scores << $2.to_i
- end
- end
+ ## check scores empty? - fill with -1,-1
+ scores += [-1,-1] if scores.size == 0
+ scores += [-1,-1] if scores.size == 2
+
+ scores << $1.to_i
+ scores << $2.to_i
end
scores
end # methdod find_scores!
View
2  lib/sportdb/version.rb
@@ -1,6 +1,6 @@
module SportDb
- VERSION = '1.7.6'
+ VERSION = '1.7.7'
end
View
35 test/test_round.rb
@@ -18,28 +18,29 @@ def test_round_en
pos, title, title2, group_pos, group_title, ko = parse_round( line )
- assert( pos == 2 )
- assert( title == "2. Round" )
- assert( title2 == nil )
- assert( ko == false )
+ assert_equal 2, pos
+ assert_equal '2. Round', title
+ assert_equal nil, title2
+ assert_equal false, ko
line = "(1) Matchday P.1 / 1st Leg // January 22-24"
pos, title, title2, group_pos, group_title, ko = parse_round( line )
- assert( pos == 1 )
- assert( title == "Matchday P.1 / 1st Leg" )
- assert( title2 == "January 22-24" )
- assert( ko == false )
+ assert_equal 1, pos
+ assert_equal 'Matchday P.1 / 1st Leg', title
+ assert_equal 'January 22-24', title2
+ assert_equal false, ko
line = "(4) Matchday 2 / Group 1 // February 19-21"
pos, title, title2, group_pos, group_title, ko = parse_round( line )
- assert( pos == 4 )
- assert( title == "Matchday 2" )
- assert( title2 == "February 19-21" )
- assert( ko == false )
+ assert_equal 4, pos
+ assert_equal 'Matchday 2', title
+ assert_equal 'February 19-21', title2
+ assert_equal false, ko
+
line = "(13) Round of 16 / 1st Leg // April 25, May 1-3"
@@ -101,7 +102,7 @@ def test_round_es
pos, title, title2, group_pos, group_title, ko = parse_round( line )
- assert( pos = 2 )
+ assert( pos == 2 )
assert( title == 'Jornada 2' )
assert( title2 == '27, 28 y 29 de julio' )
assert( ko == false )
@@ -110,7 +111,7 @@ def test_round_es
pos, title, title2, group_pos, group_title, ko = parse_round( line )
- assert( pos = 18 )
+ assert( pos == 18 )
assert( title == 'Cuartos de Final / Ida' )
assert( title2 == '14/15 de noviembre' )
assert( ko == false )
@@ -119,7 +120,7 @@ def test_round_es
pos, title, title2, group_pos, group_title, ko = parse_round( line )
- assert( pos = 19 )
+ assert( pos == 19 )
assert( title == 'Cuartos de Final / Vuelta' )
assert( title2 == '17/18 de noviembre' )
assert( ko == true )
@@ -133,7 +134,7 @@ def test_round_de
pos, title, title2, group_pos, group_title, ko = parse_round( line )
- assert( pos = 5 )
+ assert( pos == 5 )
assert( title == 'Spieltag 5' )
assert( title2 == 'Di./Mi., 20.+21. Nov 2012' )
assert( ko == false )
@@ -142,7 +143,7 @@ def test_round_de
pos, title, title2, group_pos, group_title, ko = parse_round( line )
- assert( pos = 8 )
+ assert( pos == 8 )
assert( title == 'Achtelfinale Rückspiele' )
assert( title2 == 'Di./Mi., 5.+6./12.+13. Mär 2013' )
assert( ko == true )
View
45 test/test_scores.rb
@@ -0,0 +1,45 @@
+# encoding: utf-8
+
+require 'helper'
+
+class TestScores < MiniTest::Unit::TestCase
+
+
+ def test_scores
+ assert_equal [10,0], parse_scores( '10:0' )
+ assert_equal [1,22], parse_scores( '1:22' )
+ assert_equal [1,22], parse_scores( '1-22' )
+
+ assert_equal [], parse_scores( '1-222' ) # do not support three digits
+ assert_equal [], parse_scores( '111-0' ) # do not support three digits
+ assert_equal [], parse_scores( '1:222' ) # do not support three digits
+ assert_equal [], parse_scores( '111:0' ) # do not support three digits
+
+ ## penality only
+ assert_equal [-1,-1,-1,-1,3,4], parse_scores( '3-4iE' )
+ assert_equal [-1,-1,-1,-1,3,4], parse_scores( '3:4iE' )
+
+ ## extra time only - allow ?? why not ?? only allow penalty w/ missing extra time?
+ ## todo/fix: issue warning or error in parser!!!
+ assert_equal [-1,-1,3,4], parse_scores( '3-4nV' )
+ assert_equal [-1,-1,3,4], parse_scores( '3:4nV' )
+
+ assert_equal [1,1,3,4], parse_scores( '3:4nV 1:1' )
+ assert_equal [1,1,3,4], parse_scores( '1:1 3:4nV' )
+
+ assert_equal [1,1,-1,-1,3,4], parse_scores( '3:4iE 1:1' )
+ assert_equal [1,1,-1,-1,3,4], parse_scores( '1:1 3:4iE' )
+ end
+
+private
+ class Reader
+ include LogUtils::Logging # add logger
+ include SportDb::FixtureHelpers
+ end
+
+ def parse_scores( line )
+ reader = Reader.new
+ reader.find_scores!( line )
+ end
+
+end # class TestScores
Please sign in to comment.
Something went wrong with that request. Please try again.