2008/10/15 Xavier N. [email protected]:
No con todas las normalizaciones posibles.
Visto. Habia tres caracters en los que la normalizacion de la
aplicacion dependia de la normalizacion Unicode del input, eran: ą, į,
ij.
He añadido ligaduras y la letra ß a la transliteracion. He factorizado
tambien la tabla para poder automatizar esto sin repetirla en el test
(es posible que en codigo final cachee las regexps en lugar de
interpolar).
Os paso el modulo y el test abajo, son 120 aserciones.
– fxn
test/unit/normalization_test.rb
require ‘test_helper’
class NormalizationTest < ActiveSupport::TestCase
def test_normalization
MyAppUtils::TRANSLITERATIONS.each do |from, to|
expected = to * from.chars.length
ActiveSupport::Multibyte::NORMALIZATIONS_FORMS.each do |f|
normalized_from = from.chars.normalize(f)
assert_equal expected, MyAppUtils.normalize(normalized_from)
end
end
end
end
lib/my_app_utils.rb
module MyAppUtils
TRANSLITERATIONS = {
“à áâãäåÄăą” => ‘a’,
“ß” => ‘ss’,
“æ” => ‘ae’,
“ÄÄ‘” => ‘d’,
“çćÄĉċ” => ‘c’,
“èéêëēęěĕė” => ‘e’,
“Æ’” => ‘f’,
“ff” => ‘ff’,
“ﬔ => ‘fi’,
“fl” => ‘fl’,
“ffi” => ‘ffi’,
“ffl” => ‘ffl’,
“ſt” => ‘st’,
“Äğġģ” => ‘g’,
“ĥħ” => ‘h’,
“ììÃîïīĩÄį” => ‘i’,
“ij” => ‘ij’,
“ıĵ” => ‘j’,
“ķĸ” => ‘k’,
“łľĺļŀ” => ‘l’,
“ñńňņʼnŋ” => ‘n’,
“òóôõöøÅÅ‘ÅÅ” => ‘o’,
“Å“” => ‘oe’,
“ŕřŗ” => ‘r’,
“śšşÅÈ™” => ‘s’,
“ťţŧț” => ‘t’,
“ùúûüūůűÅũų” => ‘u’,
“ŵ” => ‘w’,
“ýÿŷ” => ‘y’,
“žżź” => ‘z’,
}
def self.normalize(str)
return ‘’ if str.nil?
n = str.chars.downcase.strip.to_s
TRANSLITERATIONS.each do |from, to|
n.gsub!(/[#{from}]/, to)
end
n.gsub!(/\s+/, ’ ‘)
n.delete!(’^ a-z0-9_/\-.')
n
end
end