我正在 Ruby on Rails 上开发一个应用程序,但我被卡住了。我有一个类将所有奇怪的字符映射为普通字符(即 é, è -> e )。我需要两个模型中的这个类,因为我不想重复代码,我想加载它以便模型可以使用它。知道该怎么做吗?
编辑*我上传课程*
类 UtfAnalyzer < Ferret::Analysis::Analyzer 包括 Ferret::Analysis
CHARACTER_MAPPINGS = {
['à','á','â','ã','ä','å','ā','ă'] => 'a',
['æ'] => 'ae',
['ď','đ'] => 'd',
['ć','č','ĉ','ċ'] => 'c',
['è','é','ê','ë','ē','ę','ě','ĕ','ė',] => 'e',
['ƒ'] => 'f',
['ĝ','ğ','ġ','ģ'] => 'g',
['ĥ','ħ'] => 'h',
['ì','ì','í','î','ï','ī','ĩ','ĭ'] => 'i',
['į','ı','ij','ĵ'] => 'j',
['ķ','ĸ'] => 'k',
['ł','ľ','ĺ','ļ','ŀ'] => 'l',
['ñ','ń','ň','ņ','ʼn','ŋ'] => 'n',
['ò','ó','ô','õ','ö','ø','ō','ő','ŏ','ŏ'] => 'o',
['œ'] => 'oek',
['ą'] => 'q',
['ŕ','ř','ŗ'] => 'r',
['ś','š','ş','ŝ','ș'] => 's',
['ť','ţ','ŧ','ț'] => 't',
['ù','ú','û','ü','ū','ů','ű','ŭ','ũ','ų'] => 'u',
['ŵ'] => 'w',
['ý','ÿ','ŷ'] => 'y',
['ž','ż','ź'] => 'z'
}
HTML_MAPPINGS = {
['à', 'á', 'À', 'Á', 'ä', 'Ä'] => 'a',
['è', 'é', 'È', 'É', 'ë', 'Ë'] => 'e',
['ì', 'í', 'Ì', 'Í', 'ï', 'Ï'] => 'i',
['ò', 'ó', 'Ò', 'Ó', 'ö', 'Ö'] => 'o',
['ù', 'ú', 'Ù', 'Ú', 'ü', 'Ü'] => 'u',
['ç', 'Ç'] => 'ç'
}
def self.token_stream(field, str)
ts = MappingFilter.new(RegExpTokenizer.new(str, /([a-zA-Z0-9ÑñàáäèéëìíïòóöùúüÀÁÈÉËÌÍÏÒÓÖÙÚÜçÇ]|(&([aeiouAEIOU](acute|grave|uml)|ccedil|Ccedil);))+/), HTML_MAPPINGS)
ts = MappingFilter.new(StandardTokenizer.new(str), CHARACTER_MAPPINGS)
ts = MappingFilter.new(ts, CHARACTER_MAPPINGS)
ts = LowerCaseFilter.new(ts)
end
结尾