Module: TextUtils::TitleHelper
- Included in:
- TextUtils
- Defined in:
- lib/textutils/helper/title_helper.rb
Instance Method Summary collapse
- #strip_special_chars(title) ⇒ Object
- #strip_subtitles(title) ⇒ Object
-
#strip_tags(title) ⇒ Object
todo: use an alias or rename for better name ??.
- #strip_translations(title) ⇒ Object
- #strip_whitespaces(title) ⇒ Object
- #title_esc_regex(title_unescaped) ⇒ Object
- #title_to_key(title) ⇒ Object
Instance Method Details
#strip_special_chars(title) ⇒ Object
40 41 42 43 |
# File 'lib/textutils/helper/title_helper.rb', line 40 def strip_special_chars( title ) # remove special chars (e.g. %°&) title.gsub( /[%&°]/, '' ) end |
#strip_subtitles(title) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/textutils/helper/title_helper.rb', line 17 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 ??
25 26 27 28 29 30 31 32 33 |
# File 'lib/textutils/helper/title_helper.rb', line 25 def ( 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
10 11 12 13 14 15 |
# File 'lib/textutils/helper/title_helper.rb', line 10 def strip_translations( title ) # remove optional english translation in square brackets ([]) # e.g. Wien [Vienna] => Wien title.gsub( /\[.+\]/, '' ) end |
#strip_whitespaces(title) ⇒ Object
35 36 37 38 |
# File 'lib/textutils/helper/title_helper.rb', line 35 def strip_whitespaces( title ) # remove all whitespace and punctuation title.gsub( /[ \t_\-\.()\[\]'"\/]/, '' ) end |
#title_esc_regex(title_unescaped) ⇒ Object
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 |
# File 'lib/textutils/helper/title_helper.rb', line 136 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
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 |
# File 'lib/textutils/helper/title_helper.rb', line 45 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_translations( key ) key = strip_subtitles( key ) key = ( 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 |