0

i have some problem with removing diacritics with iconv in ruby on rails

here is my code:

class Diacritics
  def removeDiacritics(text)
    dRemover = Iconv.new("ASCII//TRANSLIT", "UTF-8")
    text = dRemover.iconv(text).gsub(/[^a-zA-Z0-9 ]/, '')
  end
end

this is output:

1.9.3-p392 :001 > require "diacritics"
/usr/local/rvm/gems/ruby-1.9.3-p392@persoc/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `block in require': iconv will be deprecated in the future, use String#encode instead.
 => true 
1.9.3-p392 :002 > remover = Diacritics.new
 => #<Diacritics:0x00000004237068> 
1.9.3-p392 :003 > text = "Dánský prezídent"
 => "Dánský prezídent" 
1.9.3-p392 :004 > remover.removeDiacritics(text)
 => "Dnsk prezdent"

i expect "Dansky prezident"

server apache on fedora (httpd), using rvm and ruby 1.9.3-p392

Can anybody help me?

1 Answers1

0

You can use the ActiveSupport::Inflector.transliterate method.

ActiveSupport::Inflector.transliterate("Dánsky prezídent") # => "Dansky prezident"

If you need this for a url slug, it's even easier.

"Dánsky prezídent".parameterize # => "dansky-prezident"
johno
  • 852
  • 1
  • 7
  • 9
  • thank you but i have another problem: ActionView::Template::Error (undefined method `unpack' for nil:NilClass): in parsed_text function: parsedText = ActiveSupport::Inflector.transliterate(parsedText) #on this line parsedText = lemmatizer.lemmatize(parsedText).body – Scooby Šalmík Apr 20 '13 at 15:17
  • Also http://stackoverflow.com/questions/522715/removing-accents-diacritics-from-string-while-preserving-other-special-chars-tr?rq=1 – johno Apr 22 '13 at 10:22