Module: TextUtils::TitleHelper

Included in:
TextUtils
Defined in:
lib/textutils/helper/title_helper.rb

Instance Method Summary collapse

Instance Method Details

#strip_part_markers(title) ⇒ Object

  • todo: use new additional sub module ???

    e.g. TextUtils::Reader::TagHelper
    

    lets us use “classic” web helpers a la rails find a good name for sub module - Reader? Fixtures? Values? Parser?



13
14
15
16
17
18
19
# File 'lib/textutils/helper/title_helper.rb', line 13

def strip_part_markers( title )   # use different name e.g. strip_name_markers/strip_name_enclosure etc.??
   # remove optional part markers
   # e.g. Bock ‹Damm› becomes =>  Bock Damm
   #      ‹Estrella› ‹Damm› Inedit becomes =>  Estrella Damm Inedit

   title.gsub( /[<>‹›]/, '' )
end

#strip_special_chars(title) ⇒ Object



51
52
53
54
# File 'lib/textutils/helper/title_helper.rb', line 51

def strip_special_chars( title )
    # remove special chars (e.g. %°&)
    title.gsub( /[%&°]/, '' )
end

#strip_subtitles(title) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/textutils/helper/title_helper.rb', line 28

def strip_subtitles( title )
    # remove optional longer title part in ()
    # e.g. Las Palmas (de Gran Canaria) => Las Palmas
    #      Palma (de Mallorca) => Palma

    title.gsub( /\([^\)]+\)/, '' )
end

#strip_tags(title) ⇒ Object

todo: use an alias or rename for better name ??



36
37
38
39
40
41
42
43
44
# File 'lib/textutils/helper/title_helper.rb', line 36

def strip_tags( title )   # todo: use an alias or rename for better name ??
    # remove optional longer title part in {}
    #  e.g. Ottakringer {Bio}   => Ottakringer
    #       Ottakringer {Alkoholfrei} => Ottakringer
    #
    # todo: use for autotags? e.g. {Bio} => bio 
    
    title.gsub( /\{[^\}]+\}/, '' )
end

#strip_translations(title) ⇒ Object



21
22
23
24
25
26
# File 'lib/textutils/helper/title_helper.rb', line 21

def strip_translations( title )
    # remove optional english translation in square brackets ([])
    # e.g. Wien [Vienna]  =>  Wien

    title.gsub( /\[[^\]]+\]/, '' )
end

#strip_whitespaces(title) ⇒ Object



46
47
48
49
# File 'lib/textutils/helper/title_helper.rb', line 46

def strip_whitespaces( title )
    # remove all whitespace and punctuation
    title.gsub( /[ \t_\-\.()\[\]'"\/]/, '' )
end

#title_esc_regex(title_unescaped) ⇒ Object



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
# File 'lib/textutils/helper/title_helper.rb', line 149

def title_esc_regex( title_unescaped )
    
    ##  escape regex special chars e.g. . to \. and ( to \( etc.
    # e.g. Benfica Lis.
    # e.g. Club Atlético Colón (Santa Fe)

    ## NB: cannot use Regexp.escape! will escape space '' to '\ '
    ## title = Regexp.escape( title_unescaped )
    title = title_unescaped.gsub( '.', '\.' )
    title = title.gsub( '(', '\(' )
    title = title.gsub( ')', '\)' )

    ##  match accented char with or without accents
    ##  add (ü|ue) etc.
    ## also make - optional change to (-| ) e.g. Blau-Weiss == Blau Weiss

    ## todo: add some more
    ## see http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references  for more
    ##
    ##  reuse for all readers!
    
    alternatives = [
      ['-', '(-| )'],  ## e.g. Blau-Weiß Linz
      ['æ', '(æ|ae)'],  ## e.g. 
      ['á', '(á|a)'],  ## e.g. Bogotá, Sársfield
      ['ã', '(ã|a)'],  ## e.g  São Paulo
      ['ä', '(ä|ae)'],  ## e.g. 
      ['ç', '(ç|c)'],  ## e.g. Fenerbahçe
      ['é', '(é|e)'],  ## e.g. Vélez
      ['ê', '(ê|e)'],  ## e.g. Grêmio
      ['ñ', '(ñ|n)'],  ## e.g. Porteño
      ['ň', '(ň|n)'],  ## e.g. Plzeň
      ['Ö', '(Ö|Oe)'], ## e.g. Österreich
      ['ö', '(ö|oe)'],  ## e.g. Mönchengladbach
      ['ó', '(ó|o)'],   ## e.g. Colón
      ['ș', '(ș|s)'],   ## e.g. Bucarești
      ['ß', '(ß|ss)'],  ## e.g. Blau-Weiß Linz
      ['ü', '(ü|ue)'],  ## e.g. 
      ['ú', '(ú|u)']  ## e.g. Fútbol
    ]
    
    alternatives.each do |alt|
      title = title.gsub( alt[0], alt[1] )
    end

    title
end

#title_to_key(title) ⇒ Object



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
# File 'lib/textutils/helper/title_helper.rb', line 56

def title_to_key( title )

    ## NB: used in/moved from readers/values_reader.rb

    ## NB: downcase does NOT work for accented chars (thus, include in alternatives)
    key = title.downcase

    key = strip_part_markers( key )  # e.g. ‹Estrella› ‹Damm› Inedit becomes =>  Estrella Damm Inedit

    key = strip_translations( key )

    key = strip_subtitles( key )

    key = strip_tags( key )

    key = strip_whitespaces( key )

    key = strip_special_chars( key )

    ##  turn accented char into ascii look alike if possible
    ##
    ## todo: add some more
    ## see http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references  for more
    
    ## todo: add unicode codepoint name
    
    alternatives = [
      ['ß', 'ss'],
      ['æ', 'ae'],
      ['ä', 'ae'],
      ['ā', 'a' ],  # e.g. Liepājas
      ['á', 'a' ],  # e.g. Bogotá, Králové
      ['ã', 'a' ],  # e.g  São Paulo
      ['ă', 'a' ],  # e.g. Chișinău
      ['â', 'a' ],  # e.g  Goiânia
      ['å', 'a' ],  # e.g. Vålerenga
      ['ą', 'a' ],  # e.g. Śląsk
      ['ç', 'c' ],  # e.g. São Gonçalo, Iguaçu, Neftçi
      ['ć', 'c' ],  # e.g. Budućnost
      ['č', 'c' ],  # e.g. Tradiční, Výčepní
      ['é', 'e' ],  # e.g. Vélez, Králové
      ['è', 'e' ],  # e.g. Rivières
      ['ê', 'e' ],  # e.g. Grêmio
      ['ě', 'e' ],  # e.g. Budějovice
      ['ĕ', 'e' ],  # e.g. Svĕtlý
      ['ė', 'e' ],  # e.g. Vėtra
      ['ë', 'e' ],  # e.g. Skënderbeu
      ['ğ', 'g' ],  # e.g. Qarabağ
      ['ì', 'i' ],  # e.g. Potosì
      ['í', 'i' ],  # e.g. Ústí
      ['ł', 'l' ],  # e.g. Wisła, Wrocław
      ['ñ', 'n' ],  # e.g. Porteño
      ['ň', 'n' ],  # e.g. Plzeň, Třeboň
      ['ö', 'oe'],
      ['ő', 'o' ],  # e.g. Győri
      ['ó', 'o' ],  # e.g. Colón, Łódź, Kraków
      ['õ', 'o' ],  # e.g. Nõmme
      ['ø', 'o' ],  # e.g. Fuglafjørdur, København
      ['ř', 'r' ],  # e.g. Třeboň
      ['ș', 's' ],  # e.g. Chișinău, București
      ['ş', 's' ],  # e.g. Beşiktaş
      ['š', 's' ],  # e.g. Košice
      ['ť', 't' ],  # e.g. Měšťan
      ['ü', 'ue'],
      ['ú', 'u' ],  # e.g. Fútbol
      ['ū', 'u' ],  # e.g. Sūduva
      ['ů', 'u' ],  # e.g. Sládkův
      ['ı', 'u' ],  # e.g. Bakı   # use u?? (Baku) why-why not?
      ['ý', 'y' ],  # e.g. Nefitrovaný
      ['ź', 'z' ],  # e.g. Łódź
      ['ž', 'z' ],  # e.g. Domžale, Petržalka

      ['Č', 'c' ],  # e.g. České
      ['İ', 'i' ],  # e.g. İnter
      ['Í', 'i' ],  # e.g. ÍBV
      ['Ł', 'l' ],  # e.g. Łódź
      ['Ö', 'oe' ], # e.g. Örebro
      ['Ř', 'r' ],  # e.g. Řezák
      ['Ś', 's' ],  # e.g. Śląsk
      ['Š', 's' ],  # e.g. MŠK
      ['Ş', 's' ],  # e.g. Şüvälan
      ['Ú', 'u' ],  # e.g. Ústí, Újpest
      ['Ž', 'z' ]   # e.g. Žilina
    ]
    
    alternatives.each do |alt|
      key = key.gsub( alt[0], alt[1] )
    end

    key
end