views:

852

answers:

2

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.

+13  A: 

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

mjy
A: 

This does not seem like a proper occasion for invoking regular expressions--you should simply have a list of acceptable answers, plus some filtering to remove nonessential words like "a", "the", and their language-specific equivalents.

Whatever you do, it seems obvious to me that it must be character-encoding-aware and language-aware. Regular expressions are typically neither.

Rob Williams