Class: SportDb::Import::ClubReader

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/config/club_reader.rb

Class Method Summary collapse

Class Method Details

.parse(txt) ⇒ Object



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
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
82
83
84
85
86
87
88
89
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
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
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/sportdb/config/club_reader.rb', line 17

def self.parse( txt )
  recs = []
  last_rec  = nil
  headings = []   ## headings stack

  txt.each_line do |line|
    line = line.strip

    next if line.empty?
    next if line.start_with?( '#' )   ## skip comments too

    ## strip inline (until end-of-line) comments too
    ##  e.g Eupen        => KAS Eupen,    ## [de]
    ##   => Eupen        => KAS Eupen,
    line = line.sub( /#.*/, '' ).strip
    pp line


    next if line =~ /^={1,}$/          ## skip "decorative" only heading e.g. ========

     ## note: like in wikimedia markup (and markdown) all optional trailing ==== too
     ##  todo/check:  allow ===  Text  =-=-=-=-=-=   too - why? why not?
    if line =~ /^(={1,})       ## leading ======
                 ([^=]+?)      ##  text   (note: for now no "inline" = allowed)
                 =*            ## (optional) trailing ====
                 $/x
       heading_marker = $1
       heading_level  = $1.length   ## count number of = for heading level
       heading        = $2.strip

       puts "heading #{heading_level} >#{heading}<"

       ## 1) first pop headings if present
       while headings.size+1 > heading_level
         headings.pop
       end

       ## 2) add missing (hierarchy) level if
       while headings.size+1 < heading_level
         ##  todo/fix: issue warning about "skipping" hierarchy level
         puts "!!! warn [team reader] - skipping hierarchy level in headings "
         headings.push( nil )
       end

       if heading =~ /^\?+$/    ## note: use ? or ?? or ?? to reset level to nil
         ## keep level empty
       else

         ## quick hack:   if level is 1 assume country for now
         ##                 and extract country code e.g.
         ##                    Austria (at) => at
         ##  todo/fix:  allow code only e.g. at or aut without enclosing () too - why? why not?
         if heading_level == 1
             if heading =~ /\(([a-z]{2,3})\)/i    ## note allow (at) or (AUT) too
               country_code = $1

               ## check country code - MUST exist for now!!!!
               country = SportDb::Import.config.countries[ country_code ]
               if country.nil?
                 puts "!!! error [team reader] - unknown country with code >#{country_code}< - sorry - add country to config to fix"
                 exit 1
               end

               headings.push( country.key )
             else
               puts "!!! error - heading level 1 - missing country code - >#{heading}<"
               exit 1
             end
         else
          ## quick hack:
          ##   remove known fill/dummy words incl:
          ##     Provincia San Juan  =>  San Juan   (see argentina, for example)
          ##
          ##   use geo tree long term with alternative names - why? why not?
           words = ['Provincia']
           words.each { |word| heading = heading.gsub( word, '' ) }
           heading = heading.strip

           headings.push( heading )
         end

         ## assert that hierarchy level is ok
         if headings.size != heading_level
           puts "!!! error - headings hierarchy/stack out of order - #{heading.size}<=>#{heading_level}"
           exit 1
         end
       end

       pp headings

    elsif line.start_with?( '|' )
      ## assume continuation with line of alternative names
      ##  note: skip leading pipe
      values = line[1..-1].split( '|' )   # team names - allow/use pipe(|)
      ## strip and  squish (white)spaces
      #   e.g. New York FC      (2011-)  => New York FC (2011-)
      values = values.map { |value| value.strip.gsub( /[ \t]+/, ' ' ) }
      last_rec.alt_names += values
      last_rec.add_variants( values ) # auto-add (possible) auto-generated variant names

      ## check for duplicates
      if last_rec.duplicates?
        duplicates = last_rec.duplicates
        puts "*** !!! WARN !!! - #{duplicates.size} duplicate alt name mapping(s):"
        pp duplicates
        pp last_rec
        ##
        ##  todo/fix:  make it only an error with exit 1
        ##               if (not normalized) names are the same (not unique/uniq)
        ##                  e.g. don't exit on  A.F.C. == AFC etc.
        ## exit 1
      end
    else
      values = line.split( ',' )

      rec = Club.new
      value = values.shift    ## get first item
      ## strip and  squish (white)spaces
      #   e.g. New York FC      (2011-)  => New York FC (2011-)
      value = value.strip.gsub( /[ \t]+/, ' ' )
      rec.name = value            # canoncial name (global unique "beautiful/long" name)
      rec.add_variants( value )   # auto-add (possible) auto-generated variant names

      ## note:
      ##   check/todo!!!!!!!!!!!!!!!!!-
      ##  strip year if to present e.g. (2011-)
      ##
      ##  do NOT strip for defunct / historic clubs e.g.
      ##    (1899-1910)
      ## or (-1914) or (-2011) etc.

      ###
      ##  todo: move year out of canonical team name - why? why not?

      ## check if canonical name include (2011-) or similar in name
      ##   if yes, remove (2011-) and add to (alt) names
      ##   e.g. New York FC (2011) => New York FC
      if rec.name =~ /\(.+?\)/   ## note: use non-greedy (?) match
        name = rec.name.gsub( /\(.+?\)/, '' ).strip

        if rec.name =~ /\(([0-9]{4})-\)/            ## e.g. (2014-)
          rec.year     = $1.to_i
        elsif  rec.name =~ /\(-([0-9]{4})\)/            ## e.g. (-2014)
          rec.year_end = $1.to_i
        elsif  rec.name =~ /\(([0-9]{4})-([0-9]{4})\)/  ## e.g. (2011-2014)
          rec.year     = $1.to_i
          rec.year_end = $2.to_i
        else
          ## todo/check: warn about unknown year format
        end
      end

      ##  todo/check - check for unknown format values
      ##    e.g. too many values, duplicate years, etc.
      ##         check for overwritting, etc.
      while values.size > 0
        value = values.shift
        ##  strip and squish (white)spaces
        #   e.g. León     › Guanajuato     => León › Guanajuato
        value = value.strip.gsub( /[ \t]+/, ' ' )
        if value =~/^\d{4}$/   # e.g 1904
          ## todo/check: issue warning if year is already set!!!!!!!
          if rec.year
            puts "!!! error - year already set to #{rec.year} - CANNOT overwrite with #{value}:"
            pp rec
            exit 1
          end
          rec.year  = value.to_i
        elsif value.start_with?( '@' )   # e.g. @ Anfield
          ## cut-off leading @ and spaces
          rec.ground  = value[1..-1].strip
        else
          ## assume city / geo tree
          ## split into geo tree
          geos = split_geo( value )
          city = geos[0]
          ## check for "embedded" district e.g. London (Fulham) or Hamburg (St. Pauli) etc.
          if city =~ /\((.+?)\)/   ## note: use non-greedy (?) match
            rec.district  = $1.strip
            city          = city.gsub( /\(.+?\)/, '' ).strip
          end
          rec.city = city

          if geos.size > 1
             ## cut-off city and keep the rest (of geo tree)
             rec.geos = geos[1..-1]
          end
        end
      end  ## while values


      ###############
      ## use headings text for geo tree

      ## 1) add country if present
      if headings.size > 0 && headings[0]
        country = SportDb::Import.config.countries[ headings[0] ]
        rec.country = country
      else
        ## make it an error - why? why not?
        puts "!!! error - country missing in headings hierarchy - sorry - add to quicklist"
        exit 1
      end

      ## 2) check geo tree with headings hierarchy
      if headings.size > 1 && headings[1]
         geos = split_geo( headings[1] )
         if rec.geos
           if rec.geos[0] != geos[0]
             puts "!!! error - geo tree - headings mismatch >#{rec.geos[0]}< <=> >#{geos[0]}<"
             exit 1
           end
           if rec.geos[1] && rec.geos[1] != geos[1]   ## check optional 2nd level too
             puts "!!! error - geo tree - headings mismatch >#{rec.geos[1]}< <=> >#{geos[1]}<"
             exit 1
           end
         else
           ## add missing region (state/province) from headings hierarchy
           rec.geos = geos
         end
      end

      last_rec = rec


    ### todo/fix:
    ##  auto-add alt name with dots stripped - why? why not?
    ##    e.g.  D.C. United    => DC United
    ##    e.g.  Liverpool F.C. => Liverpool FC
    ##    e.g.  St. Albin       => St Albin etc.
    ##    e.g.  1. FC Köln     => 1 FC Köln  -- make special case for 1. - why? why not?

    ##
    ## todo/fix:  unify mapping entries
    ##   always lowercase !!!!  (case insensitive)
    ##   always strip (2011-) !!!
    ##   always strip dots (e.g. St., F.C, etc.)

      recs << rec
    end
  end  # each_line
  recs
end

.read(path) ⇒ Object

use - rename to read_file or from_file etc. - why? why not?



11
12
13
14
# File 'lib/sportdb/config/club_reader.rb', line 11

def self.read( path )   ## use - rename to read_file or from_file etc. - why? why not?
  txt = File.open( path, 'r:utf-8' ).read
  parse( txt )
end

.split_geo(str) ⇒ Object

helpers



262
263
264
265
266
267
268
269
270
271
272
# File 'lib/sportdb/config/club_reader.rb', line 262

def self.split_geo( str )
  ## assume city / geo tree
  ##  strip and squish (white)spaces
  #   e.g. León     › Guanajuato     => León › Guanajuato
  str = str.strip.gsub( /[ \t]+/, ' ' )

  ## split into geo tree
  geos = str.split( /[<>‹›]/ )   ## note: allow > < or › ‹
  geos = geos.map { |geo| geo.strip }   ## remove all whitespaces
  geos
end