nothingmuch has asked for the wisdom of the Perl Monks concerning the following question:

Hola,

There are many modules on the CPAN which know how to parse dates that look like "tomorrow", "in one hour", "24/12", etc.

I'm looking for a module that does the same for numbers... "one half", "1/2", "0.5" and "½" should all be parsed into a float, or maybe a bigrat.

I can't find anything of the sort.

Update: I should note that Math::Expr is the next best thing I found, and I'll probably use it given nothing else, because it at least gives me proper support of fractions.

-nuffin
zz zZ Z Z #!perl

Replies are listed 'Best First'.
Re: Parsing human notations of numbers
by davido (Cardinal) on Jan 16, 2005 at 07:22 UTC

    For general numbers there is Lingua::EN::FindNumber. Here's an example from its POD:

    use Lingua::EN::FindNumber; my $text = "Fourscore and seven years ago, our four fathers..."; numify($text); # "87 years ago, our 4 fathers..." @numbers = extract_numbers($text); # "Fourscore and seven", "four"

    It's pretty easy to use, and surprisingly thorough, though I haven't had a chance to test if it handles fractions.


    Dave

      It's actually based on Lingua::EN::Words2Nums, which is sort of what I really want. It doesn't support fractions and and decimal-point notations yet.

      I might be tempted to patch it given some spare time.

      Thanks!

      -nuffin
      zz zZ Z Z #!perl

        I just checked Lingua::EN::Numericalize, and found that it doesn't properly support fractions either. "Three fourths" parses as 12... which is actually 3*4. That seems kinda wierd, but thems the breaks.


        Dave

Re: Parsing human notations of numbers
by ysth (Canon) on Jan 16, 2005 at 07:48 UTC
    If you end up publishing something, don't forget that besides the latin1 fractions ¼ ½ ¾ there are additional unicode fractions: ⅓ ⅔ ⅕ ⅖ ⅗ ⅘ ⅙ ⅚ ⅛ ⅜ ⅝ ⅞