4

I want to convert words into number like Ninety Nine to 99 Second to 2nd and Two to 2 etc

Does anybody knows how to do that?

Ayaz Alavi
  • 4,825
  • 8
  • 50
  • 68
  • This question has already been asked here: http://stackoverflow.com/questions/3299619/algorithm-that-converts-numeric-amount-into-english-words – bibo bode Dec 18 '12 at 20:28
  • 1
    @FawazTahir, It is not the same. That is a different question and has nothing to do with objective c. Here the question is to convert from words to numbers. Not the reverse. – iDev Dec 18 '12 at 20:34
  • My apologies. I misread that. – bibo bode Dec 18 '12 at 20:38
  • yup correct ACB, I searched alot before asking the question. – Ayaz Alavi Dec 18 '12 at 20:38
  • @iArezki, You are making the same mistake as I did earlier. It is not from number to word. – iDev Dec 18 '12 at 20:43
  • Questions: Is this for, eg, "compressing" written text, or for providing a "natural language" interface? Will the writers be fairly formal, or "casual" in their writing. Does the translation need to be 100% accurate, 98%, 90%? Can the tool "throw up its hands" on some sequences (and perhaps ask for assistance), or must it translate everything? – Hot Licks Dec 18 '12 at 20:49
  • (And will you be dealing with dollars and cents in some cases? Distinguishing "fifteen-ninetynine == 1599 from "fifteen-ninetynine" = $15.99 is a definite challenge.) – Hot Licks Dec 18 '12 at 20:51
  • (And consider that "five hundred forty two" = 542, but "ten twenty" = $10.20 or 10:20.) – Hot Licks Dec 18 '12 at 20:54
  • (I think it's doable, to a "non banker" level of precision. But it's more than a couple hours of work.) – Hot Licks Dec 18 '12 at 20:55
  • This seems like a terrible issue to have to address! Mainly because of all the various combinations, misspellings, etc as @Hot Licks eludes to...! Is this existing data or parsing from data input? If existing data... well, good luck!... else, can't you just limit the input character set on a field, instead? (say, to number characters, only)? – JRG-Developer Dec 18 '12 at 21:06

2 Answers2

6

There is no API from Apple for this kind of purpose. Lexicographical analysis is hard even if it is just for a single language. Expressing a number as words might be relatively simple, but getting from words to numbers would require dealing with many special cases.

The brute force approach would be to have a database of all the various possible combinations. A smarter approach would probably analyse the individual words and reason what number that could mean.

Cocoanetics
  • 8,171
  • 2
  • 30
  • 57
3

You are looking for this, NSNumberFormatterStyle. It has an option as NSNumberFormatterSpellOutStyle.

As per the apple documentation,

NSNumberFormatterSpellOutStyle: Specifies a spell-out format; for example, “23” becomes “twenty-three”.

Looks like you want to convert from Word to Number and not Number to Word. I misread your question.

You might have to manually do that as there are no APIs available from Apple for that. You can check NSNumberFormatter and see if you can find something helpful for this.

As jlehr mentioned, NSLog(@"%@", [formatter numberFromString:@"one hundred forty-three"]); will give the number 143. But it may not be a perfect solution which works with all possible combinations.

iDev
  • 23,310
  • 7
  • 60
  • 85
  • 4
    `NSNumberFormatter` will at least get you part of the way there. For example `NSLog(@"%@", [formatter numberFromString:@"one hundred forty-three"]);` correctly yields the number **143**, given a formatter instance set to use the `NSNumberFormatterSpellOutStyle`. – jlehr Dec 18 '12 at 21:15
  • @jlehr, Thanks for pointing that out. I wasn't sure that will work with all possible combinations. – iDev Dec 18 '12 at 22:04
  • yup really nice comment jlehr – Ayaz Alavi Dec 18 '12 at 22:33