Convert English to a number

Applescript

A silly, hacky mash-up that might upset some Cupertino/Mountain View folks, but I think its a creative silly, hacky mash-up.

set myNumber to text returned of (display dialog ¬
    "Enter number as text:" buttons {"Continue…"} ¬
    default answer "" default button 1)
tell application "Google Chrome"
    activate
    open location "https://www.google.com"
end tell
delay 5
say "ok google. " & myNumber
delay 2
tell application "System Events"
    tell application process "Google Chrome"
        set fullURL to value of text field 1 of toolbar 1 of window 1
    end tell
end tell
set AppleScript's text item delimiters to "="
display alert item 2 of text items of fullURL

Uses OSX text to speech to speak the text number, and google audio search to listen for it and convert it to an integer.

Requirements

  • OSX
  • Google chrome
  • speech-recognition enabled in your google account
  • volume turned up to a reasonable level

The delay timings may need to be adjusted depending on your chrome load time and google lookup time.

Example input:

enter image description here

Example output:

enter image description here


Bash, 93 64 55 characters*

In the fantastic bsd-games package that's available on most Linux operating systems, there's a small command-line toy called number. It turns numbers into English text, that is, it does the exact opposite of this question. It really is the exact opposite: all the rules in the question are followed by number. It's almost too good to be a coincidence.

$ number 42
forty-two.

Of course, number doesn't answer the question. We want it the other way around. I thought about this for a while, tried string parsing and all that, then realised that I can just call number on all 999.999 numbers and see if something matches the input. If so, the first line where it matches has twice the line number I'm looking for (number prints a line of dots after every number). Simple as that. So, without further ado, here's the complete code for my entry:

seq 0 999999|number -l|awk "/$1/{print (NR-1)/2;exit}"

It even short-circuits, so converting "two" is quite fast, and even higher numbers are usually decoded in under a second on my box. Here's an example run:

wn@box /tmp> bash unnumber.sh "zero"
0
wn@box /tmp> bash unnumber.sh "fifteen"
15
wn@box /tmp> bash unnumber.sh "ninety" 
90
wn@box /tmp> bash unnumber.sh "seven hundred four"
704
wn@box /tmp> bash unnumber.sh "sixty-nine thousand four hundred eleven"
69411
wn@box /tmp> bash unnumber.sh "five hundred twenty thousand two"    
520002

Of course, you'll need to have number installed for this to work.


*: Yes, I know, this is not a code-golf challenge, but shortness is pretty much the only discerning quality of my entry, so... :)


Javascript

(function parse(input) {
  var pat = "ze/on/tw/th.?r/fo/fi/ix/se/ei/ni/ten/ele".split("/");
  var num = "", last = 0, token = input.replace(/-/g, " ").split(" ");
  for(var i in token) {
    var t = token[i];
    for(var p in pat) if(t.match(RegExp(pat[p])) !== null) num += "+" + p;
    if(t.indexOf("een") >= 0) num += "+10";
    if(t.indexOf("lve") >= 0) num += "+10";
    if(t.indexOf("ty") >= 0) num += "*10";
    if(t.indexOf("dr") >= 0) { last = 100; num += "*100"; }
    if(t.indexOf("us") >= 0) {
      if(last < 1000) num = "(" + num + ")"; last = 0;
      num += "*1000";
    }
  }
  alert(eval(num));
})(prompt());

Do you like some eval()?

Run this script on your browser's console.

Edit: Thanks for the feedback. Bugs fixed (again).