Class: SportDb::AutoConfParser
- Inherits:
-
Object
- Object
- SportDb::AutoConfParser
show all
- Includes:
- Logging, ParserHelper
- Defined in:
- lib/sportdb/formats/match/match_parser_auto_conf.rb
Overview
todo/check: rename/change to MatchAutoConfParser - why? why not?
Class Method Summary
collapse
Instance Method Summary
collapse
#is_goals?, #is_group?, #is_group_def?, #is_knockout_round?, #is_round?, #is_round_def?, #read_lines
Constructor Details
Returns a new instance of AutoConfParser.
20
21
22
23
24
25
26
|
# File 'lib/sportdb/formats/match/match_parser_auto_conf.rb', line 20
def initialize( lines, start )
@lines = lines.is_a?( String ) ? read_lines( lines ) : lines
@start = start
end
|
Class Method Details
.parse(lines, start:) ⇒ Object
8
9
10
11
12
13
|
# File 'lib/sportdb/formats/match/match_parser_auto_conf.rb', line 8
def self.parse( lines, start: )
parser = new( lines, start )
parser.parse
end
|
Instance Method Details
#find_date!(line, start:) ⇒ Object
192
193
194
195
196
197
198
199
200
|
# File 'lib/sportdb/formats/match/match_parser_auto_conf.rb', line 192
def find_date!( line, start: )
DateFormats.find!( line, start: start )
end
|
#find_score!(line) ⇒ Object
185
186
187
188
189
190
|
# File 'lib/sportdb/formats/match/match_parser_auto_conf.rb', line 185
def find_score!( line )
ScoreFormats.find!( line )
end
|
#parse ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
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
76
77
78
79
80
81
|
# File 'lib/sportdb/formats/match/match_parser_auto_conf.rb', line 28
def parse
@last_round = nil
@last_group = nil
@round_defs = Hash.new(0)
@group_defs = Hash.new(0)
@rounds = {} @groups = {} @teams = Hash.new(0)
@warns = []
@lines.each do |line|
if is_goals?( line )
logger.debug "skipping matched goals line: >#{line}<"
elsif is_round_def?( line )
logger.debug "skipping matched round def line: >#{line}<"
@round_defs[ line ] += 1
elsif is_round?( line )
logger.debug "skipping matched round line: >#{line}<"
round = @rounds[ line ] ||= {count: 0, match_count: 0} round[:count] +=1
@last_round = round
elsif is_group_def?( line ) logger.debug "skipping matched group def line: >#{line}<"
@group_defs[ line ] += 1
elsif is_group?( line )
logger.debug "skipping matched group line: >#{line}<"
group = @groups[ line ] ||= {count: 0, match_count: 0}
group[:count] +=1
@last_group = group
elsif try_parse_game( line )
else
logger.warn "skipping line (no match found): >#{line}<"
@warns << line
end
end
[@teams, @rounds, @groups, @round_defs, @group_defs, @warns]
end
|
#parse_game(line) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
|
# File 'lib/sportdb/formats/match/match_parser_auto_conf.rb', line 90
def parse_game( line )
logger.debug "parsing game (fixture) line: >#{line}<"
line = line.gsub( /\[
[^\]]+?
\]/x, '' ).strip
return true if line.empty?
values = line.split( '@' )
line = values[0]
date = find_date!( line, start: @start )
if date line = line.sub( /\[
[^\]]+?
\]/x, '' ).strip
else
line = line.sub( %r{^ ## MUST be anchored to beginning of line
[012]?[0-9]
[.:hH]
[0-9][0-9]
(?=[ ]) ## must be followed by space for now (add end of line too - why? why not?)
}x, '' ).strip
end
return true if line.empty?
score = find_score!( line )
logger.debug " line: >#{line}<"
line = line.sub( /\[
[^\]]+?
\]/x, '$$' ) line = line.gsub( /\[
[^\]]+?
\]/x, '' )
line = line.gsub( /\(
[^)]*?
\)/x, '' )
line = line.sub( / \s+
( -
| v
| vs\.? # note: allow optional dot eg. vs.
)
\s+
/ix, '$$' )
values = line.split( '$$' )
values = values.map { |value| value.strip } values = values.select { |value| !value.empty? }
return true if values.size == 0
if values.size == 1
puts "(auto config) try matching teams separated by spaces (2+):"
pp values
values = values[0].split( /[ ]{2,}/ )
pp values
end
return false if values.size != 2
puts "(auto config) try matching teams:"
pp values
@teams[ values[0] ] += 1 @teams[ values[1] ] += 1
@last_round[ :match_count ] += 1 if @last_round
@last_group[ :match_count ] += 1 if @last_group
true
end
|
#try_parse_game(line) ⇒ Object
84
85
86
87
88
|
# File 'lib/sportdb/formats/match/match_parser_auto_conf.rb', line 84
def try_parse_game( line )
parse_game( line.dup )
end
|