sportdb-config 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,65 +1,65 @@
1
- # encoding: utf-8
2
-
3
-
4
- module SportDb
5
- module Import
6
-
7
- class LeagueReader
8
-
9
- def self.read( path )
10
- txt = File.open( path, 'r:utf-8' ).read
11
- parse( txt )
12
- end
13
-
14
-
15
-
16
- SEASON_REGEX = /\[
17
- (?<season>
18
- \d{4}
19
- (-\d{2,4})?
20
- )
21
- \]/x
22
-
23
- def self.parse( txt )
24
- hash = {}
25
- season = '*' ## use '*' for default/latest season
26
-
27
- txt.each_line do |line|
28
- line = line.strip
29
-
30
- next if line.empty?
31
- next if line.start_with?( '#' ) ## skip comments too
32
-
33
- ## strip inline comments too
34
- line = line.sub( /#.*/, '' ).strip
35
-
36
- pp line
37
-
38
-
39
- if (m=line.match( SEASON_REGEX ))
40
- season = m[:season]
41
- else
42
- key_line, values_line = line.split( '=>' )
43
- values = values_line.split( ',' )
44
-
45
- ## remove leading and trailing spaces
46
- key_line = key_line.strip
47
- values = values.map { |value| value.strip }
48
- pp values
49
-
50
- league_key = key_line
51
- league_basename = values[0]
52
-
53
- hash[season] ||= {}
54
- hash[season][league_key] = league_basename
55
- end
56
- end
57
- hash
58
- end # method read
59
-
60
- end ## class LeagueReader
61
-
62
-
63
-
64
- end ## module Import
65
- end ## module SportDb
1
+ # encoding: utf-8
2
+
3
+
4
+ module SportDb
5
+ module Import
6
+
7
+ class LeagueReader
8
+
9
+ def self.read( path )
10
+ txt = File.open( path, 'r:utf-8' ).read
11
+ parse( txt )
12
+ end
13
+
14
+
15
+
16
+ SEASON_REGEX = /\[
17
+ (?<season>
18
+ \d{4}
19
+ (-\d{2,4})?
20
+ )
21
+ \]/x
22
+
23
+ def self.parse( txt )
24
+ hash = {}
25
+ season = '*' ## use '*' for default/latest season
26
+
27
+ txt.each_line do |line|
28
+ line = line.strip
29
+
30
+ next if line.empty?
31
+ next if line.start_with?( '#' ) ## skip comments too
32
+
33
+ ## strip inline comments too
34
+ line = line.sub( /#.*/, '' ).strip
35
+
36
+ pp line
37
+
38
+
39
+ if (m=line.match( SEASON_REGEX ))
40
+ season = m[:season]
41
+ else
42
+ key_line, values_line = line.split( '=>' )
43
+ values = values_line.split( ',' )
44
+
45
+ ## remove leading and trailing spaces
46
+ key_line = key_line.strip
47
+ values = values.map { |value| value.strip }
48
+ pp values
49
+
50
+ league_key = key_line
51
+ league_basename = values[0]
52
+
53
+ hash[season] ||= {}
54
+ hash[season][league_key] = league_basename
55
+ end
56
+ end
57
+ hash
58
+ end # method read
59
+
60
+ end ## class LeagueReader
61
+
62
+
63
+
64
+ end ## module Import
65
+ end ## module SportDb
@@ -1,24 +1,24 @@
1
- # encoding: utf-8
2
-
3
-
4
- module LeagueHelper
5
- def basename( league, country:, season: nil )
6
- ## e.g. eng-england, 2011-12, 1 returns 1-premierleague
7
- ##
8
- ## allow country code or (repo) package name
9
- ## e.g. eng-england or eng
10
- ## de-deutschland or de etc.
11
-
12
- leagues = SportDb::Import.config.leagues
13
-
14
- result = leagues.basename( league, country: country, season: season )
15
-
16
- ##
17
- # note: if no mapping / nothing found return league e.g. 1, 2, 3, 3a, 3b, cup(?), etc.
18
- result ? result : league
19
- end
20
- end
21
-
22
- module LeagueUtils
23
- extend LeagueHelper
24
- end
1
+ # encoding: utf-8
2
+
3
+
4
+ module LeagueHelper
5
+ def basename( league, country:, season: nil )
6
+ ## e.g. eng-england, 2011-12, 1 returns 1-premierleague
7
+ ##
8
+ ## allow country code or (repo) package name
9
+ ## e.g. eng-england or eng
10
+ ## de-deutschland or de etc.
11
+
12
+ leagues = SportDb::Import.config.leagues
13
+
14
+ result = leagues.basename( league, country: country, season: season )
15
+
16
+ ##
17
+ # note: if no mapping / nothing found return league e.g. 1, 2, 3, 3a, 3b, cup(?), etc.
18
+ result ? result : league
19
+ end
20
+ end
21
+
22
+ module LeagueUtils
23
+ extend LeagueHelper
24
+ end
@@ -1,81 +1,91 @@
1
- # encoding: utf-8
2
-
3
- module SportDb
4
- module Import
5
-
6
-
7
-
8
- class Variant ## (spelling) variant finder / builder for names
9
-
10
-
11
- def self.frequency_table( name ) ## todo/check: use/rename to char_frequency_table
12
- ## calculate the frequency table of letters, digits, etc.
13
- freq = Hash.new(0)
14
- name.each_char do |ch|
15
- freq[ch] += 1
16
- end
17
- freq
18
- end
19
-
20
- ALPHA_SPECIALS = %w[
21
- Ä Ö Ü
22
- ä ö ü ß
23
- ]
24
-
25
- ## "simple" translation
26
- SUB_ALPHA_SPECIALS = {
27
- 'Ä'=>'A', 'ä'=>'a',
28
- 'Ö'=>'O', 'ö'=>'o',
29
- 'Ü'=>'U', 'ü'=>'u',
30
- 'ß'=>'ss',
31
- }
32
-
33
- ## de,at,ch translation for umlauts
34
- SUB_ALPHA_SPECIALS_DE = {
35
- 'Ä'=>'Ae', 'ä'=>'ae',
36
- 'Ö'=>'Oe', 'ö'=>'oe',
37
- 'Ü'=>'Ue', 'ü'=>'ue',
38
- 'ß'=>'ss',
39
- }
40
-
41
-
42
- def self.alpha_specials_count( freq )
43
- ALPHA_SPECIALS.reduce(0) do |count,ch|
44
- count += freq[ch]
45
- count
46
- end
47
- end
48
-
49
- def self.tr( name, mapping )
50
- buf = String.new
51
- name.each_char do |ch|
52
- buf << if mapping[ch]
53
- mapping[ch]
54
- else
55
- ch
56
- end
57
- end
58
- buf
59
- end
60
-
61
-
62
-
63
- def self.find( name )
64
- alt_names = []
65
-
66
- freq = frequency_table( name )
67
-
68
- if alpha_specials_count( freq ) > 0 # check if includes äöü etc.
69
- alt_names << tr( name, SUB_ALPHA_SPECIALS )
70
- alt_names << tr( name, SUB_ALPHA_SPECIALS_DE )
71
- end
72
-
73
- ## todo - make uniq e.g. Preußen is Preussen, Preussen 2x
74
- alt_names = alt_names.uniq
75
- alt_names
76
- end
77
- end # Variant
78
-
79
-
80
- end ## module Import
81
- end ## module SportDb
1
+ # encoding: utf-8
2
+
3
+ module SportDb
4
+ module Import
5
+
6
+
7
+
8
+ class Variant ## (spelling) variant finder / builder for names
9
+
10
+
11
+ def self.frequency_table( name ) ## todo/check: use/rename to char_frequency_table
12
+ ## calculate the frequency table of letters, digits, etc.
13
+ freq = Hash.new(0)
14
+ name.each_char do |ch|
15
+ freq[ch] += 1
16
+ end
17
+ freq
18
+ end
19
+
20
+
21
+ ## "simple" translation
22
+ ALPHA_SPECIALS = {
23
+ 'Ä'=>'A', 'ä'=>'a',
24
+ 'á'=>'a',
25
+ 'à'=>'a',
26
+ 'É'=>'E', 'é'=>'e',
27
+ 'í'=>'i',
28
+ 'ñ'=>'n',
29
+ 'Ö'=>'O', 'ö'=>'o',
30
+ 'ó'=>'o',
31
+ 'Ü'=>'U', 'ü'=>'u',
32
+ 'ú'=>'u',
33
+ 'ß'=>'ss',
34
+ }
35
+
36
+ ## de,at,ch translation for umlauts
37
+ ALPHA_SPECIALS_DE = {
38
+ 'Ä'=>'Ae', 'ä'=>'ae',
39
+ 'Ö'=>'Oe', 'ö'=>'oe',
40
+ 'Ü'=>'Ue', 'ü'=>'ue',
41
+ 'ß'=>'ss',
42
+ }
43
+
44
+ ## add ALPHA_SPECIALS_ES - why? why not? is Espanyol catalan spelling or spanish (castillian)?
45
+ # 'ñ'=>'ny', ## e.g. Español => Espanyol
46
+
47
+
48
+
49
+ def self.alpha_specials_count( freq, mapping )
50
+ mapping.keys.reduce(0) do |count,ch|
51
+ count += freq[ch]
52
+ count
53
+ end
54
+ end
55
+
56
+ def self.tr( name, mapping )
57
+ buf = String.new
58
+ name.each_char do |ch|
59
+ buf << if mapping[ch]
60
+ mapping[ch]
61
+ else
62
+ ch
63
+ end
64
+ end
65
+ buf
66
+ end
67
+
68
+
69
+
70
+ def self.find( name )
71
+ alt_names = []
72
+
73
+ freq = frequency_table( name )
74
+
75
+ if alpha_specials_count( freq, ALPHA_SPECIALS ) > 0 # check if includes äöü etc.
76
+ alt_names << tr( name, ALPHA_SPECIALS )
77
+ end
78
+
79
+ if alpha_specials_count( freq, ALPHA_SPECIALS_DE ) > 0 ## todo/fix: add / pass-in language/country code and check - why? why not?
80
+ alt_names << tr( name, ALPHA_SPECIALS_DE )
81
+ end
82
+
83
+ ## todo - make uniq e.g. Preußen is Preussen, Preussen 2x
84
+ alt_names = alt_names.uniq
85
+ alt_names
86
+ end
87
+ end # Variant
88
+
89
+
90
+ end ## module Import
91
+ end ## module SportDb
@@ -8,7 +8,7 @@ module Boot ## note: use a different module than Config to avoid confusion
8
8
 
9
9
  MAJOR = 0 ## todo: namespace inside version or something - why? why not??
10
10
  MINOR = 4
11
- PATCH = 0
11
+ PATCH = 1
12
12
  VERSION = [MAJOR,MINOR,PATCH].join('.')
13
13
 
14
14
  def self.version
@@ -1,150 +1,150 @@
1
- # encoding: utf-8
2
-
3
- ###
4
- # to run use
5
- # ruby -I ./lib -I ./test test/test_club_reader.rb
6
-
7
-
8
- require 'helper'
9
-
10
- class TestClubReader < MiniTest::Test
11
-
12
- def test_parse_at
13
- recs = SportDb::Import::ClubReader.parse( <<TXT )
14
- ==================================
15
- = Austria (at)
16
-
17
- FK Austria Wien, Wien
18
- | Austria Vienna | Austria Wien
19
- SK Rapid Wien, Wien
20
- | Rapid Vienna | Rapid Wien
21
- Wiener Sport-Club, Wien
22
- TXT
23
-
24
- pp recs
25
-
26
- assert_equal 3, recs.size
27
- assert_equal 'FK Austria Wien', recs[0].name
28
- assert_equal 'Wien', recs[0].city
29
- end
30
-
31
- def test_parse_us
32
- recs = SportDb::Import::ClubReader.parse( <<TXT )
33
- ==================================================
34
- = United States (us)
35
-
36
- #######################################
37
- # Major League Soccer (MLS) teams
38
-
39
- Atlanta United FC, 2017, Atlanta › Georgia
40
- | Atlanta United
41
- Chicago Fire, 1998, Bridgeview › Illinois
42
- FC Dallas, 1996, Frisco › Texas ## note: FC Dallas named >Dallas Burn< from 1996-2004
43
-
44
- ##################################
45
- # Defunct / Historic
46
- Miami Fusion (1998-2001), Fort Lauderdale › Florida
47
- CD Chivas USA (2005-2014), Carson › California
48
- | Chivas USA
49
- TXT
50
-
51
- pp recs
52
-
53
- assert_equal 5, recs.size
54
- assert_equal 'Atlanta United FC', recs[0].name
55
- assert_equal 2017, recs[0].year
56
- assert_equal 'Atlanta', recs[0].city
57
- assert_equal ['Georgia'], recs[0].geos
58
- end
59
-
60
-
61
- def test_parse_years
62
- recs = SportDb::Import::ClubReader.parse( <<TXT )
63
- = United States (us)
64
- FC Dallas (1996-), Frisco › Texas
65
- Miami Fusion (1998-2001), Fort Lauderdale › Florida
66
- CD Chivas USA (-2014), Carson › California
67
- TXT
68
-
69
- pp recs
70
-
71
- assert_equal 3, recs.size
72
- assert_equal 1996, recs[0].year
73
- assert_equal false, recs[0].historic?
74
- assert_equal false, recs[0].past?
75
-
76
- assert_equal 1998, recs[1].year
77
- assert_equal 2001, recs[1].year_end
78
- assert_equal true, recs[1].historic?
79
- assert_equal true, recs[1].past?
80
-
81
- assert_equal 2014, recs[2].year_end
82
- assert_equal true, recs[2].historic?
83
- assert_equal true, recs[2].past?
84
- end
85
-
86
- def test_parse_geos
87
- recs = SportDb::Import::ClubReader.parse( <<TXT )
88
- = England (eng)
89
- == Greater London
90
-
91
- Fulham FC, 1879, @ Craven Cottage, London (Fulham) › Greater London
92
- | Fulham | FC Fulham
93
- Charlton Athletic FC, @ The Valley, London (Charlton) › Greater London
94
- | Charlton | Charlton Athletic
95
-
96
- = Deutschland (de)
97
- == Hamburg
98
-
99
- St. Pauli, Hamburg (St. Pauli)
100
- TXT
101
-
102
- pp recs
103
-
104
- assert_equal 3, recs.size
105
- assert_equal 'London', recs[0].city
106
- assert_equal 'Fulham', recs[0].district
107
- assert_equal ['Greater London'], recs[0].geos
108
- assert_equal 'England', recs[0].country.name
109
- assert_equal 'eng', recs[0].country.key
110
-
111
- assert_equal 'London', recs[1].city
112
- assert_equal 'Charlton', recs[1].district
113
- assert_equal ['Greater London'], recs[1].geos
114
- assert_equal 'England', recs[1].country.name
115
- assert_equal 'eng', recs[1].country.key
116
-
117
- assert_equal 'Hamburg', recs[2].city
118
- assert_equal 'St. Pauli', recs[2].district
119
- assert_equal ['Hamburg'], recs[2].geos
120
- assert_equal 'Germany', recs[2].country.name
121
- assert_equal 'de', recs[2].country.key
122
- end
123
-
124
-
125
- def test_parse_headings
126
- recs = SportDb::Import::ClubReader.parse( <<TXT )
127
- ==============
128
- ====
129
- ===========
130
- = Heading 1 - Austria (at)
131
- = Heading 1 - Austria (at) ==================
132
- == Heading 2
133
- == Heading 2 =========
134
- === Heading 3
135
- === Heading 3 ===============
136
- === Heading 3 # with end-of-line comment
137
- === Heading 3 ## with end-of-line comment
138
- === Heading 3 ========= # with end-of-line comment
139
- == Heading 2
140
- ==== Heading 4
141
- = ?????
142
- == ???
143
- TXT
144
-
145
- pp recs
146
-
147
- assert_equal 0, recs.size
148
- end
149
-
150
- end # class TestClubReader
1
+ # encoding: utf-8
2
+
3
+ ###
4
+ # to run use
5
+ # ruby -I ./lib -I ./test test/test_club_reader.rb
6
+
7
+
8
+ require 'helper'
9
+
10
+ class TestClubReader < MiniTest::Test
11
+
12
+ def test_parse_at
13
+ recs = SportDb::Import::ClubReader.parse( <<TXT )
14
+ ==================================
15
+ = Austria (at)
16
+
17
+ FK Austria Wien, Wien
18
+ | Austria Vienna | Austria Wien
19
+ SK Rapid Wien, Wien
20
+ | Rapid Vienna | Rapid Wien
21
+ Wiener Sport-Club, Wien
22
+ TXT
23
+
24
+ pp recs
25
+
26
+ assert_equal 3, recs.size
27
+ assert_equal 'FK Austria Wien', recs[0].name
28
+ assert_equal 'Wien', recs[0].city
29
+ end
30
+
31
+ def test_parse_us
32
+ recs = SportDb::Import::ClubReader.parse( <<TXT )
33
+ ==================================================
34
+ = United States (us)
35
+
36
+ #######################################
37
+ # Major League Soccer (MLS) teams
38
+
39
+ Atlanta United FC, 2017, Atlanta › Georgia
40
+ | Atlanta United
41
+ Chicago Fire, 1998, Bridgeview › Illinois
42
+ FC Dallas, 1996, Frisco › Texas ## note: FC Dallas named >Dallas Burn< from 1996-2004
43
+
44
+ ##################################
45
+ # Defunct / Historic
46
+ Miami Fusion (1998-2001), Fort Lauderdale › Florida
47
+ CD Chivas USA (2005-2014), Carson › California
48
+ | Chivas USA
49
+ TXT
50
+
51
+ pp recs
52
+
53
+ assert_equal 5, recs.size
54
+ assert_equal 'Atlanta United FC', recs[0].name
55
+ assert_equal 2017, recs[0].year
56
+ assert_equal 'Atlanta', recs[0].city
57
+ assert_equal ['Georgia'], recs[0].geos
58
+ end
59
+
60
+
61
+ def test_parse_years
62
+ recs = SportDb::Import::ClubReader.parse( <<TXT )
63
+ = United States (us)
64
+ FC Dallas (1996-), Frisco › Texas
65
+ Miami Fusion (1998-2001), Fort Lauderdale › Florida
66
+ CD Chivas USA (-2014), Carson › California
67
+ TXT
68
+
69
+ pp recs
70
+
71
+ assert_equal 3, recs.size
72
+ assert_equal 1996, recs[0].year
73
+ assert_equal false, recs[0].historic?
74
+ assert_equal false, recs[0].past?
75
+
76
+ assert_equal 1998, recs[1].year
77
+ assert_equal 2001, recs[1].year_end
78
+ assert_equal true, recs[1].historic?
79
+ assert_equal true, recs[1].past?
80
+
81
+ assert_equal 2014, recs[2].year_end
82
+ assert_equal true, recs[2].historic?
83
+ assert_equal true, recs[2].past?
84
+ end
85
+
86
+ def test_parse_geos
87
+ recs = SportDb::Import::ClubReader.parse( <<TXT )
88
+ = England (eng)
89
+ == Greater London
90
+
91
+ Fulham FC, 1879, @ Craven Cottage, London (Fulham) › Greater London
92
+ | Fulham | FC Fulham
93
+ Charlton Athletic FC, @ The Valley, London (Charlton) › Greater London
94
+ | Charlton | Charlton Athletic
95
+
96
+ = Deutschland (de)
97
+ == Hamburg
98
+
99
+ St. Pauli, Hamburg (St. Pauli)
100
+ TXT
101
+
102
+ pp recs
103
+
104
+ assert_equal 3, recs.size
105
+ assert_equal 'London', recs[0].city
106
+ assert_equal 'Fulham', recs[0].district
107
+ assert_equal ['Greater London'], recs[0].geos
108
+ assert_equal 'England', recs[0].country.name
109
+ assert_equal 'eng', recs[0].country.key
110
+
111
+ assert_equal 'London', recs[1].city
112
+ assert_equal 'Charlton', recs[1].district
113
+ assert_equal ['Greater London'], recs[1].geos
114
+ assert_equal 'England', recs[1].country.name
115
+ assert_equal 'eng', recs[1].country.key
116
+
117
+ assert_equal 'Hamburg', recs[2].city
118
+ assert_equal 'St. Pauli', recs[2].district
119
+ assert_equal ['Hamburg'], recs[2].geos
120
+ assert_equal 'Germany', recs[2].country.name
121
+ assert_equal 'de', recs[2].country.key
122
+ end
123
+
124
+
125
+ def test_parse_headings
126
+ recs = SportDb::Import::ClubReader.parse( <<TXT )
127
+ ==============
128
+ ====
129
+ ===========
130
+ = Heading 1 - Austria (at)
131
+ = Heading 1 - Austria (at) ==================
132
+ == Heading 2
133
+ == Heading 2 =========
134
+ === Heading 3
135
+ === Heading 3 ===============
136
+ === Heading 3 # with end-of-line comment
137
+ === Heading 3 ## with end-of-line comment
138
+ === Heading 3 ========= # with end-of-line comment
139
+ == Heading 2
140
+ ==== Heading 4
141
+ = ?????
142
+ == ???
143
+ TXT
144
+
145
+ pp recs
146
+
147
+ assert_equal 0, recs.size
148
+ end
149
+
150
+ end # class TestClubReader