Re: Convert spelled out numbers to digits?
Re: Convert spelled out numbers to digits?
- Subject: Re: Convert spelled out numbers to digits?
- From: Keary Suska <email@hidden>
- Date: Wed, 27 Mar 2013 09:24:45 -0600
On Mar 27, 2013, at 7:49 AM, Eric E. Dolecki wrote:
> I have an application where I cannot control how string data is formatted -
> and I am interested in numbers. Sometimes I get a "five" instead of a "5"
> or an "eleven" instead of an "11" - and so on.
>
> Is there an easy way to convert these to numeral string representations? Or
> should I just make a monster set of stringByReplacingOccurrencesOfString in
> a conversion method to return a modified string?
There isn't anything built in, probably due to localization issues, but it wouldn't be too onerous as long as you can stick to english only and you upper end isn't too high. However, it seems that the real issue is since there doesn't seem to be any data normalization on your input you can't guarantee that you will get sensible values. For instance, spelling errors or other typos.
In any case I would recommend using regular expressions instead, as you will need to distinguish "six" from "sixteen", and maybe "sixty", and it will be much easier with regexes. You may be able to get away with twenty or so expressions that in some combination can account for many numbers: zero through twelve, then alternates for irregular forms: "twen", "thir", "for"; then for factors "teen", "ty" (watch out for "eighty") and maybe "hundred", also ignoring noise such as "and". You could have a dictionary to do lookups for 0-12, then you only need logic for +10 for -teen, *10 for -ty, etc.
The easiest way would be to iterate over the string, from beginning to end, adding up the value and removing number atoms until the string is exhausted or you have uninterpretable text and have to decide what to do about it.
HTH,
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden