Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I ignore accents when comparing strings in Perl?

I have this quiz application where I match what people type with the right answer. For now, what I do is basically that :

if ($input =~ /$answer/i) {
     print "you won";
}

It's nice, as if the answer is "fish" the user can type "a fish" and be counted a good answer.

The problem I'm facing is that, well, my users as I are french, and I'd like to be able to accept, say, a user typing "taton", and the answer being "tâton".

So, what I could do, is :

use POSIX qw(locale_h);
use locale;
setlocale(LC_TYPE, "fr_FR.ISO8859-15");
setlocale(LC_COLLATE, "fr_FR.ISO8859-15");

And in my check routine, do a :

$input = lc($input);
$input =~ tr/àáâãäåçèéêëìíîïñòóôõöùúûüýÿ/aaaaaaceeeeiiiinooooouuuuyy/;

and something likewise with the answer.

I don't like it, because I have to hard code things, and the day I decide I'm leaving the ISO-8859-15 world for the UTF-8 world, I'm doomed.

So, I'm looking for a way to compare strings, that will make "tâton" eq "taton", "maçon" eq "macon" or "macon" =~ /maçon/ be true.

like image 725
mat Avatar asked Dec 22 '08 15:12

mat


1 Answers

Try the Text::Unaccent module from CPAN (or Text::Unaccent::PurePerl).

like image 196
mjy Avatar answered Nov 13 '22 22:11

mjy