2

I have a NSString with me. This NSString is obtained from a Voice engine. The voice input is converted to native NSString. See the following string: "Set heat to thirty two degree"

Is there any way to get this converted to "Set heat to 32 degree"

If there are some third party library that does this conversion, it would be really helpful. Otherwise I will have to create a complex logic to get this done it seems.

2
  • What is the range of numbers you are talking about? Commented Aug 1, 2013 at 16:17
  • This is ruby, but it might help if you eventually do write your own: markburns/numbers_in_words Commented Aug 1, 2013 at 16:32

1 Answer 1

3

And that's where a forgotten NSNumberFormatter can show what it's capable of doing.

After you parsed your input string (using regular expression magic or NSString's componentsSeparatedByString:, I'll not be discussing that step) and obtained the spell-out number, you can use NSNumberFormatter's NSNumberFormatterSpellOutStyle to quickly convert your string into a number:

NSString *obtainedDegreesString = @"thirty-two";
NSNumberFormatter *spellOutFormatter = [[NSNumberFormatter alloc] init];
[spellOutFormatter setLocale:[NSLocale currentLocale]]; // or whatever locale you want
[spellOutFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
NSNumber *degreesNumber = [spellOutFormatter numberFromString:obtainedDegreesString];
NSLog(@"%d", degreesNumber.intValue); // logs 32

But warning - you have to convert strings like "thirty two" to "thirty-two" (the correct English numerals) for your formatter to work - passing "thirty two" results in 3002! Your users probably don't want to burn in 3002 degrees :P You can achieve that using another regular expression, I suppose.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.