středa 23. ledna 2008

Removing diacritic (windows 1250) in Ruby

We received data in windows 1250 (cp1250) encoding. It's problem in Rails that works with utf8.
When you save these data in YAML, you will get accented signs as \xe1 for á (in cp1250), but strange sign in UTF8. So we need remove this encoding and replace it by ASCII signs.

You can use following code for removing diacritic:

TABLE1250 = {"e1" => "a", "e4" => "a", "e8" => "c", "ef" => "d", "e9" => "e", "ec" => "e", "ed" => "i", "be" => "l", "e5" => "l", "f2" => "n", "f3" => "o", "f6" => "o", "f5" => "o", "f4" => "o", "f8" => "r", "e0" => "r", "9a" => "s", "9d" => "t", "fa" => "u", "f9" => "u", "fc" => "u", "fb" => "u", "fd" => "y", "9e" => "z", "c1" => "A", "c4" => "A", "c8" => "C", "cf" => "D", "c9" => "E", "cc" => "E", "cd" => "I", "bc" => "L", "c5" => "L", "d2" => "N", "d3" => "O", "d6" => "O", "d5" => "O", "d4" => "O", "d8" => "R", "c0" => "R", "8a" => "S", "8d" => "T", "da" => "U", "d9" => "U", "dc" => "U", "db" => "U", "dd" => "Y", "8e" => "Z"}

def remove_diacritic str
while !str.index("\\x").nil?
idx = str.index("\\x")
str[idx, 4] = "#{TABLE1250[str[idx+2, 2].downcase]}"

4 komentáře:

veny [Václav Sýkora] řekl(a)...

Na todle sem videl prostredek v knihovne Iconv. Neco jako'ASCII//TRANSLIT', 'UTF-8') ...

Anonymní řekl(a)...

Hello. Often the Internet can see links like [url=]Buy cialis without prescription[/url] or [url=]Buy cialis without prescription[/url]. Is it safe to buy in pharmacies such goods?

Anonymní řekl(a)...

Nice Information.. Thx for sharing this


Anonymní řekl(a)...

If regular coffee no longer provides you adequate caffeine, consider brewing espresso. This does mean you will need a diverse piece of gear, as your normal coffee maker most likely can not do it. Espresso machines are typically high priced even though, plus the intensity of the caffeine levels is too much for some. Try espresso a few occasions someplace else just before generating this commitment for the dwelling. It is a terrible thought to let coffee sit on a hot burner immediately after brewing. Maintaining the heat on will bring about your coffee to "cook," and can ruin its freshly-brewed flavor. Rather, unplug your coffee-maker or other appliance cheap coffee makers , or transfer the coffee to an insulated pot to help keep it hot.

Invest within a simple coffee grinder. Grinding your beans just ahead of brewing leaves aromatic, flavorful oils intact and makes coffee taste fresher. Most models let you adjust the coarseness of the grind to accommodate unique brewing designs. When you favor not to possess a separate appliance coffee makers wholesale , look for any coffee maker with an built-in grinder. Generating an incredible cup of coffee definitely depends on your coffee maker. It doesn't need to be probably the most highly-priced model to generate a great cup of coffee, however it does need to brew the coffee appropriately. Appear for a coffee maker who will extract all of the finest elements out of just about every single bean.