Spelled out number to NSNumber and overcoming NSNumberFormatter limitation

121 Views Asked by At

I'm using OpenEars to detect numbers spoken by the user and transcribed into text. My goal is to transform the spelled out number into an NSNumber instance.

I have followed the advice of Rob in here to do that. So i'm using NSNumberFormatter and setting the numberStyle to NSNumberFormatterSpellOutStyle before I call numberFromString.

Here's what my NSNumberFormatter initialization code looks like.

    _numberFormatter = [[NSNumberFormatter alloc] init];
    [_numberFormatter setLocale:[NSLocale currentLocale]];
    [_numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
    [_numberFormatter setMaximumFractionDigits:2];
    [_numberFormatter setMinimumFractionDigits:2];

and then I'm NSLogging the result of numberFromString for a series of examples (just for testing)

First:

NSLog(@"%@", [formatter numberFromString:@"eleven point three"]);
NSLog(@"%@", [formatter numberFromString:@"three point eleven"]);
NSLog(@"%@", [formatter numberFromString:@"three point one one"]);

and the results:

11.3
(null)
3.11

Second:

NSLog(@"%@", [formatter numberFromString:@"thirty-four"]);
NSLog(@"%@", [formatter numberFromString:@"thirty four"]);

and the results:

34
3004

Third:

NSLog(@"%@", [formatter numberFromString:@"three point one one"]);
NSLog(@"%@", [formatter numberFromString:@"one one point three"]);

and the results:

3.11
101.3

NSNumberFormatter seems to dislike anything other than single digits as decimals. It also seems to have a very strict set of rules when it comes to double digit numbers having to be separated by hyphens. And for the 3rd example it's just a mess...

Are these known limitations of NSNumberFormatter and would you have any suggestions as to overcoming this?

Are there any other limitations that I haven't encountered yet?

Many thanks for all input.

0

There are 0 best solutions below