Class: SportDb::ScoresFinder
- Inherits:
-
Object
- Object
- SportDb::ScoresFinder
- Includes:
- LogUtils::Logging
- Defined in:
- lib/sportdb/finders/scores.rb
Constant Summary collapse
- EN__P_ET_FT_HT__REGEX =
e.g. 3-4 pen. 2-2 a.e.t. (1-1, 1-1)
/\b (?<score1p>\d{1,2}) - (?<score2p>\d{1,2}) \s* # allow optional spaces (?:p|pen\.?|pso) # e.g. pen, pen., PSO, p etc. \s* # allow optional spaces (?<score1et>\d{1,2}) - (?<score2et>\d{1,2}) \s* # allow optional spaces (?:aet|a\.e\.t\.) \s* # allow optional spaces \( (?<score1>\d{1,2}) - (?<score2>\d{1,2}) \s* , \s* (?<score1i>\d{1,2}) - (?<score2i>\d{1,2}) \) (?=[\s\]]|$)/xi
- EN__ET_FT_HT__REGEX =
e.g. 2-1 a.e.t. (1-1, 0-0)
/\b (?<score1et>\d{1,2}) - (?<score2et>\d{1,2}) \s* # allow optional spaces (?:aet|a\.e\.t\.) \s* # allow optional spaces \( (?<score1>\d{1,2}) - (?<score2>\d{1,2}) \s* , \s* (?<score1i>\d{1,2}) - (?<score2i>\d{1,2}) \) (?=[\s\]]|$)/xi
- EN__FT_HT__REGEX =
e.g. 2-1 (1-1)
/\b (?<score1>\d{1,2}) - (?<score2>\d{1,2}) \s* \( (?<score1i>\d{1,2}) - (?<score2i>\d{1,2}) \) (?=[\s\]]|$)/x
- FT_REGEX =
e.g. 1:2 or 0:2 or 3:3 or
1-1 or 0-2 or 3-3
/\b (?<score1>\d{1,2}) [:\-] (?<score2>\d{1,2}) \b/x
- ET_REGEX =
note: possible ending w/ . -> thus cannot use /b will not work w/ .; use zero look-ahead
/\b (?<score1>\d{1,2}) [:\-] (?<score2>\d{1,2}) \s? # allow optional space (?:nv|n\.v\.|aet|a\.e\.t\.) # allow optional . e.g. nV or n.V. (?=[\s\)\]]|$)/xi
- P_REGEX =
note: possible ending w/ . -> thus cannot use /b will not work w/ .; use zero look-ahead
/\b (?<score1>\d{1,2}) [:\-] (?<score2>\d{1,2}) \s? # allow optional space (?:iE|i\.E\.|p|pen|PSO) # allow optional . e.g. iE or i.E. (?=[\s\)\]]|$)/xi
Instance Method Summary collapse
- #find!(line, opts = {}) ⇒ Object
-
#initialize ⇒ ScoresFinder
constructor
todo: allow all-in-one “literal form a la kicker” e.g.
Constructor Details
#initialize ⇒ ScoresFinder
todo: allow all-in-one “literal form a la kicker” e.g.
2:2 (1:1, 1:0) n.V. 5:1 i.E.
119 120 121 |
# File 'lib/sportdb/finders/scores.rb', line 119 def initialize # nothing here for now end |
Instance Method Details
#find!(line, opts = {}) ⇒ Object
123 124 125 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 152 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 |
# File 'lib/sportdb/finders/scores.rb', line 123 def find!( line, opts={} ) ### fix: add and match all-in-one literal first, followed by # 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 ## de is 1:1 etc. # extract score from line # and return it # note: side effect - removes date from line string score1i = nil # half time (ht) scores score2i = nil score1 = nil # full time (ft) scores score2 = nil score1et = nil # extra time (et) scores score2et = nil score1p = nil # penalty (p) scores score2p = nil if (md = EN__P_ET_FT_HT__REGEX.match( line )) score1i = md[:score1i].to_i score2i = md[:score2i].to_i score1 = md[:score1].to_i score2 = md[:score2].to_i score1et = md[:score1et].to_i score2et = md[:score2et].to_i score1p = md[:score1p].to_i score2p = md[:score2p].to_i logger.debug " score.en__p_et_ft_ht: >#{score1p}-#{score2p} pen. #{score1et}-#{score2et} a.e.t. (#{score1}-#{score2}, #{score1i}-#{score2i})<" line.sub!( md[0], '[SCORES.EN__P_ET_FT_HT]' ) elsif (md = EN__ET_FT_HT__REGEX.match( line )) score1i = md[:score1i].to_i score2i = md[:score2i].to_i score1 = md[:score1].to_i score2 = md[:score2].to_i score1et = md[:score1et].to_i score2et = md[:score2et].to_i logger.debug " score.en__et_ft_ht: >#{score1et}-#{score2et} a.e.t. (#{score1}-#{score2}, #{score1i}-#{score2i})<" line.sub!( md[0], '[SCORES.EN__ET_FT_HT]' ) elsif (md = EN__FT_HT__REGEX.match( line )) score1i = md[:score1i].to_i score2i = md[:score2i].to_i score1 = md[:score1].to_i score2 = md[:score2].to_i logger.debug " score.en__ft_ht: >#{score1}-#{score2} (#{score1i}-#{score2i})<" line.sub!( md[0], '[SCORES.EN__FT_HT]' ) else ####################################################### ## try "standard" generic patterns for fallbacks if (md = ET_REGEX.match( line )) score1et = md[:score1].to_i score2et = md[:score2].to_i logger.debug " score.et: >#{score1et}-#{score2et}<" line.sub!( md[0], '[SCORE.ET]' ) end if (md = P_REGEX.match( line )) score1p = md[:score1].to_i score2p = md[:score2].to_i logger.debug " score.p: >#{score1p}-#{score2p}<" line.sub!( md[0], '[SCORE.P]' ) end ## let full time (ft) standard regex go last - has no marker if (md = FT_REGEX.match( line )) score1 = md[:score1].to_i score2 = md[:score2].to_i logger.debug " score: >#{score1}-#{score2}<" line.sub!( md[0], '[SCORE]' ) end end ## 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 nil,nil scores = [] scores += [score1i, score2i] if score1p || score2p || score1et || score2et || score1 || score2 || score1i || score2i scores += [score1, score2] if score1p || score2p || score1et || score2et || score1 || score2 scores += [score1et, score2et] if score1p || score2p || score1et || score2et scores += [score1p, score2p] if score1p || score2p scores end |