sportdb-config 0.4.1 → 0.5.0

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,91 +1,106 @@
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
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
+ 'ã'=>'a',
27
+ 'â'=>'a',
28
+
29
+ 'ç'=>'c',
30
+
31
+ 'É'=>'E', 'é'=>'e',
32
+ 'è'=>'e',
33
+ 'ê'=>'e',
34
+
35
+ 'í'=>'i',
36
+ 'î'=>'i',
37
+
38
+ 'ñ'=>'n',
39
+
40
+ 'Ö'=>'O', 'ö'=>'o',
41
+ 'ó'=>'o',
42
+ 'õ'=>'o',
43
+ 'ô'=>'o',
44
+
45
+ 'Ü'=>'U', 'ü'=>'u',
46
+ 'ú'=>'u',
47
+
48
+ 'ß'=>'ss',
49
+ }
50
+
51
+ ## de,at,ch translation for umlauts
52
+ ALPHA_SPECIALS_DE = {
53
+ 'Ä'=>'Ae', 'ä'=>'ae',
54
+ 'Ö'=>'Oe', 'ö'=>'oe',
55
+ 'Ü'=>'Ue', 'ü'=>'ue',
56
+ 'ß'=>'ss',
57
+ }
58
+
59
+ ## add ALPHA_SPECIALS_ES - why? why not? is Espanyol catalan spelling or spanish (castillian)?
60
+ # 'ñ'=>'ny', ## e.g. Español => Espanyol
61
+
62
+
63
+
64
+ def self.alpha_specials_count( freq, mapping )
65
+ mapping.keys.reduce(0) do |count,ch|
66
+ count += freq[ch]
67
+ count
68
+ end
69
+ end
70
+
71
+ def self.tr( name, mapping )
72
+ buf = String.new
73
+ name.each_char do |ch|
74
+ buf << if mapping[ch]
75
+ mapping[ch]
76
+ else
77
+ ch
78
+ end
79
+ end
80
+ buf
81
+ end
82
+
83
+
84
+
85
+ def self.find( name )
86
+ alt_names = []
87
+
88
+ freq = frequency_table( name )
89
+
90
+ if alpha_specials_count( freq, ALPHA_SPECIALS ) > 0 # check if includes äöü etc.
91
+ alt_names << tr( name, ALPHA_SPECIALS )
92
+ end
93
+
94
+ if alpha_specials_count( freq, ALPHA_SPECIALS_DE ) > 0 ## todo/fix: add / pass-in language/country code and check - why? why not?
95
+ alt_names << tr( name, ALPHA_SPECIALS_DE )
96
+ end
97
+
98
+ ## todo - make uniq e.g. Preußen is Preussen, Preussen 2x
99
+ alt_names = alt_names.uniq
100
+ alt_names
101
+ end
102
+ end # Variant
103
+
104
+
105
+ end ## module Import
106
+ end ## module SportDb
@@ -7,8 +7,8 @@ module Boot ## note: use a different module than Config to avoid confusion
7
7
  ## maybe rename later gem itself to sportdb-boot - why? why not?
8
8
 
9
9
  MAJOR = 0 ## todo: namespace inside version or something - why? why not??
10
- MINOR = 4
11
- PATCH = 1
10
+ MINOR = 5
11
+ PATCH = 0
12
12
  VERSION = [MAJOR,MINOR,PATCH].join('.')
13
13
 
14
14
  def self.version
@@ -30,6 +30,12 @@ class TestClubIndex < MiniTest::Test
30
30
  assert_equal 'Austria', m[0].country.name
31
31
  assert_equal 'Wien', m[0].city
32
32
 
33
+ ## note: all spaces and dashes (-) get always removed
34
+ m = SportDb::Import.config.clubs.match( '--- r a p i d w i e n ---' )
35
+ assert_equal 'SK Rapid Wien', m[0].name
36
+ assert_equal 'Austria', m[0].country.name
37
+ assert_equal 'Wien', m[0].city
38
+
33
39
  m = SportDb::Import.config.clubs.match( 'RAPID WIEN' )
34
40
  assert_equal 'SK Rapid Wien', m[0].name
35
41
  assert_equal 'Austria', m[0].country.name