<p>As we all know numbers can be written either in numerics, or called by their names. While there are a lot of examples to be found that convert 123 into one hundred twenty three, I could not find good examples of how to convert it the other way around.</p>

<p>Some of the caveats:</p>

<ol>
<li>cardinal/nominal or ordinal: "one" and "first"</li>
<li>common spelling mistakes: "forty"/"fourty"</li>
<li>hundreds/thousands: 2100 -> "twenty one hundred" and also "two thousand and one hundred"</li>
<li>separators: "eleven hundred fifty two", but also "elevenhundred fiftytwo" or "eleven-hundred fifty-two" and whatnot</li>
<li>colloquialisms: "thirty-something"</li>
<li>fractions: 'one third', 'two fifths'</li>
<li>common names: 'a dozen', 'half'</li>
</ol>

<p>And there are probably more caveats possible that are not yet listed.
Suppose the algorithm needs to be very robust, and even understand spelling mistakes.</p>

<p>What fields/papers/studies/algorithms should I read to learn how to write all this?
Where is the information?</p>

<blockquote>
  <p>PS: My final parser should actually understand 3 different languages, English, Russian and Hebrew. And maybe at a later stage more languages will be added. Hebrew also has male/female numbers, like "one man" and "one woman" have a different "one" — "ehad" and "ahat". Russian also has some of its own complexities.</p>
</blockquote>

<p>Google does a great job at this. For example:</p>

<p><a href="http://www.google.com/search?q=two+thousand+and+one+hundred+plus+five+dozen+and+four+fifths+in+decimal" rel="noreferrer">http://www.google.com/search?q=two+thousand+and+one+hundred+plus+five+dozen+and+four+fifths+in+decimal</a></p>

<p>(the reverse is also possible <a href="http://www.google.com/search?q=999999999999+in+english" rel="noreferrer">http://www.google.com/search?q=999999999999+in+english</a>)</p>

[{'answer_id': 70198, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 4, 'selected': False, 'text': '<p>I have some code I wrote a while ago: <a href="https://github.com/ghewgill/text2num" rel="nofollow noreferrer">text2num</a>. This does some of what you want, except it does not handle ordinal numbers. I haven\'t actually used this code for anything, so it\'s largely untested!</p>\n'}, {'answer_id': 88903, 'author': 'Aleksandar Dimitrov', 'author_id': 11797, 'author_profile': 'https://Stackoverflow.com/users/11797', 'pm_score': 4, 'selected': False, 'text': '<p>It\'s not an easy issue, and I know of no library to do it. I might sit down and try to write something like this sometime. I\'d do it in either Prolog, Java or Haskell, though. As far as I can see, there are several issues:</p>\n\n<ul>\n<li>Tokenization: sometimes, numbers are written eleven hundred fifty two, but I\'ve seen elevenhundred fiftytwo or eleven-hundred-fifty-two and whatnot. One would have to conduct a survey on what forms are actually in use. This might be especially tricky for Hebrew.</li>\n<li>Spelling mistakes: that\'s not so hard. You have a limited amount of words, and a bit of Levenshtein-distance magic should do the trick.</li>\n<li>Alternate forms, like you already mentioned, exist. This includes ordinal/cardinal numbers, as well as forty/fourty and...</li>\n<li>... common names or commonly used phrases and NEs (named entities). Would you want to extract 30 from the Thirty Years War or 2 from World War II?</li>\n<li>Roman numerals, too?</li>\n<li>Colloqialisms, such as "thirty-something" and "three Euro and shrapnel", which I wouldn\'t know how to treat.</li>\n</ul>\n\n<p>If you are interested in this, I could give it a shot this weekend. My idea is probably using UIMA and tokenizing with it, then going on to further tokenize/disambiguate and finally translate. There might be more issues, let\'s see if I can come up with some more interesting things.</p>\n\n<p>Sorry, this is not a real answer yet, just an extension to your question. I\'ll let you know if I find/write something.</p>\n\n<p>By the way, if you are interested in the semantics of numerals, I just found an <a href="http://semanticsarchive.net/Archive/WVlYTRkO/" rel="noreferrer">interesting paper</a> by Friederike Moltmann, discussing some issues regarding the logic interpretation of numerals.</p>\n'}, {'answer_id': 645106, 'author': 'Fraser', 'author_id': 74861, 'author_profile': 'https://Stackoverflow.com/users/74861', 'pm_score': 2, 'selected': False, 'text': '<p>Ordinal numbers are not applicable because they cant be joined in meaningful ways with other numbers in language (...at least in English)</p>\n\n<p>e.g. one hundred and first, eleven second, etc...</p>\n\n<p>However, there is another English/American caveat with the word \'and\'</p>\n\n<p>i.e.</p>\n\n<p>one hundred and one (English)\none hundred one (American)</p>\n\n<p>Also, the use of \'a\' to mean one in English</p>\n\n<p>a thousand = one thousand </p>\n\n<p>...On a side note Google\'s calculator does an amazing job of this.</p>\n\n<p><a href="http://www.google.co.uk/search?hl=en&amp;rlz=1C1GGLS_en-GBGB316GB316&amp;ei=jAC7SbzoCYzFjAfNsPClCA&amp;sa=X&amp;oi=spell&amp;resnum=0&amp;ct=result&amp;cd=1&amp;q=one%20hundred%20and%20three%20thousand%20times%20the%20speed%20of%20light&amp;spell=1" rel="nofollow noreferrer">one hundred and three thousand times the speed of light</a></p>\n\n<p>And even...</p>\n\n<p><a href="http://www.google.co.uk/search?hl=en&amp;rlz=1C1GGLS_en-GBGB316GB316&amp;ei=OAG7SaGmONzFjAeHjpCVCA&amp;sa=X&amp;oi=spell&amp;resnum=0&amp;ct=result&amp;cd=1&amp;q=two%20thousand%20and%20one%20hundred%20plus%20a%20dozen&amp;spell=1" rel="nofollow noreferrer">two thousand and one hundred plus a dozen</a></p>\n\n<p>...wtf?!? <a href="http://www.google.co.uk/search?hl=en&amp;rlz=1C1GGLS_en-GBGB316GB316&amp;q=a%20score%20plus%20a%20dozen%20in%20roman%20numerals&amp;btnG=Search&amp;meta=" rel="nofollow noreferrer">a score plus a dozen in roman numerals</a></p>\n'}, {'answer_id': 653098, 'author': 'MarkusQ', 'author_id': 62970, 'author_profile': 'https://Stackoverflow.com/users/62970', 'pm_score': 7, 'selected': True, 'text': '<p>I was playing around with a PEG parser to do what you wanted (and may post that as a separate answer later) when I noticed that there\'s a very simple algorithm that does a remarkably good job with common forms of numbers in English, Spanish, and German, at the very least.</p>\n\n<p>Working with English for example, you need a dictionary that maps words to values in the obvious way:</p>\n\n<pre><code>"one" -&gt; 1, "two" -&gt; 2, ... "twenty" -&gt; 20,\n"dozen" -&gt; 12, "score" -&gt; 20, ...\n"hundred" -&gt; 100, "thousand" -&gt; 1000, "million" -&gt; 1000000\n</code></pre>\n\n<p>...and so forth</p>\n\n<p>The algorithm is just:</p>\n\n<pre><code>total = 0\nprior = null\nfor each word w\n    v &lt;- value(w) or next if no value defined\n    prior &lt;- case\n        when prior is null:       v\n        when prior &gt; v:     prior+v\n        else                prior*v\n        else\n    if w in {thousand,million,billion,trillion...}\n        total &lt;- total + prior\n        prior &lt;- null\ntotal = total + prior unless prior is null\n</code></pre>\n\n<p>For example, this progresses as follows:</p>\n\n<pre><code>total    prior      v     unconsumed string\n    0      _              four score and seven \n                    4     score and seven \n    0      4              \n                   20     and seven \n    0     80      \n                    _     seven \n    0     80      \n                    7 \n    0     87      \n   87\n\ntotal    prior      v     unconsumed string\n    0        _            two million four hundred twelve thousand eight hundred seven\n                    2     million four hundred twelve thousand eight hundred seven\n    0        2\n                  1000000 four hundred twelve thousand eight hundred seven\n2000000      _\n                    4     hundred twelve thousand eight hundred seven\n2000000      4\n                    100   twelve thousand eight hundred seven\n2000000    400\n                    12    thousand eight hundred seven\n2000000    412\n                    1000  eight hundred seven\n2000000  412000\n                    1000  eight hundred seven\n2412000     _\n                      8   hundred seven\n2412000     8\n                     100  seven\n2412000   800\n                     7\n2412000   807\n2412807\n</code></pre>\n\n<p>And so on.  I\'m not saying it\'s perfect, but for a quick and dirty it does quite well.</p>\n\n<hr>\n\n<p>Addressing your specific list on edit:</p>\n\n<ol>\n<li>cardinal/nominal or ordinal: "one" and "first" -- <strong>just put them in the dictionary</strong></li>\n<li>english/british: "fourty"/"forty" -- <strong>ditto</strong></li>\n<li>hundreds/thousands: \n  2100 -> "twenty one hundred" and also "two thousand and one hundred" -- <strong>works as is</strong></li>\n<li>separators: "eleven hundred fifty two", but also "elevenhundred fiftytwo" or "eleven-hundred fifty-two" and whatnot -- <strong>just define "next word" to be the longest prefix that matches a defined word, or up to the next non-word if none do, for a start</strong></li>\n<li>colloqialisms: "thirty-something" -- <strong>works</strong></li>\n<li>fragments: \'one third\', \'two fifths\' -- <strong>uh, not yet...</strong></li>\n<li>common names: \'a dozen\', \'half\' -- <strong>works; you can even do things like "a half dozen"</strong></li>\n</ol>\n\n<p>Number 6 is the only one I don\'t have a ready answer for, and that\'s because of the ambiguity between ordinals and fractions (in English at least) added to the fact that my last cup of coffee was <em>many</em> hours ago.</p>\n'}, {'answer_id': 653113, 'author': 'chaos', 'author_id': 47529, 'author_profile': 'https://Stackoverflow.com/users/47529', 'pm_score': 2, 'selected': False, 'text': '<p>My LPC implementation of some of your requirements (American English only):</p>\n\n<pre><code>internal mapping inordinal = ([]);\ninternal mapping number = ([]);\n\n#define Numbers ([\\\n    "zero"        : 0, \\\n    "one"         : 1, \\\n    "two"         : 2, \\\n    "three"       : 3, \\\n    "four"        : 4, \\\n    "five"        : 5, \\\n    "six"         : 6, \\\n    "seven"       : 7, \\\n    "eight"       : 8, \\\n    "nine"        : 9, \\\n    "ten"         : 10, \\\n    "eleven"      : 11, \\\n    "twelve"      : 12, \\\n    "thirteen"    : 13, \\\n    "fourteen"    : 14, \\\n    "fifteen"     : 15, \\\n    "sixteen"     : 16, \\\n    "seventeen"   : 17, \\\n    "eighteen"    : 18, \\\n    "nineteen"    : 19, \\\n    "twenty"      : 20, \\\n    "thirty"      : 30, \\\n    "forty"       : 40, \\\n    "fifty"       : 50, \\\n    "sixty"       : 60, \\\n    "seventy"     : 70, \\\n    "eighty"      : 80, \\\n    "ninety"      : 90, \\\n    "hundred"     : 100, \\\n    "thousand"    : 1000, \\\n    "million"     : 1000000, \\\n    "billion"     : 1000000000, \\\n])\n\n#define Ordinals ([\\\n    "zeroth"        : 0, \\\n    "first"         : 1, \\\n    "second"        : 2, \\\n    "third"         : 3, \\\n    "fourth"        : 4, \\\n    "fifth"         : 5, \\\n    "sixth"         : 6, \\\n    "seventh"       : 7, \\\n    "eighth"        : 8, \\\n    "ninth"         : 9, \\\n    "tenth"         : 10, \\\n    "eleventh"      : 11, \\\n    "twelfth"       : 12, \\\n    "thirteenth"    : 13, \\\n    "fourteenth"    : 14, \\\n    "fifteenth"     : 15, \\\n    "sixteenth"     : 16, \\\n    "seventeenth"   : 17, \\\n    "eighteenth"    : 18, \\\n    "nineteenth"    : 19, \\\n    "twentieth"     : 20, \\\n    "thirtieth"     : 30, \\\n    "fortieth"      : 40, \\\n    "fiftieth"      : 50, \\\n    "sixtieth"      : 60, \\\n    "seventieth"    : 70, \\\n    "eightieth"     : 80, \\\n    "ninetieth"     : 90, \\\n    "hundredth"     : 100, \\\n    "thousandth"    : 1000, \\\n    "millionth"     : 1000000, \\\n    "billionth"     : 1000000000, \\\n])\n\nvarargs int denumerical(string num, status ordinal) {\n    if(ordinal) {\n        if(member(inordinal, num))\n            return inordinal[num];\n    } else {\n        if(member(number, num))\n            return number[num];\n    }\n    int sign = 1;\n    int total = 0;\n    int sub = 0;\n    int value;\n    string array parts = regexplode(num, " |-");\n    if(sizeof(parts) &gt;= 2 &amp;&amp; parts[0] == "" &amp;&amp; parts[1] == "-")\n        sign = -1;\n    for(int ix = 0, int iix = sizeof(parts); ix &lt; iix; ix++) {\n        string part = parts[ix];\n        switch(part) {\n        case "negative" :\n        case "minus"    :\n            sign = -1;\n            continue;\n        case ""         :\n            continue;\n        }\n        if(ordinal &amp;&amp; ix == iix - 1) {\n            if(part[0] &gt;= \'0\' &amp;&amp; part[0] &lt;= \'9\' &amp;&amp; ends_with(part, "th"))\n                value = to_int(part[..&lt;3]);\n            else if(member(Ordinals, part))\n                value = Ordinals[part];\n            else\n                continue;\n        } else {\n            if(part[0] &gt;= \'0\' &amp;&amp; part[0] &lt;= \'9\')\n                value = to_int(part);\n            else if(member(Numbers, part))\n                value = Numbers[part];\n            else\n                continue;\n        }\n        if(value &lt; 0) {\n            sign = -1;\n            value = - value;\n        }\n        if(value &lt; 10) {\n            if(sub &gt;= 1000) {\n                total += sub;\n                sub = value;\n            } else {\n                sub += value;\n            }\n        } else if(value &lt; 100) {\n            if(sub &lt; 10) {\n                sub = 100 * sub + value;\n            } else if(sub &gt;= 1000) {\n                total += sub;\n                sub = value;\n            } else {\n                sub *= value;\n            }\n        } else if(value &lt; sub) {\n            total += sub;\n            sub = value;\n        } else if(sub == 0) {\n            sub = value;\n        } else {\n            sub *= value;\n        }\n    }\n    total += sub;\n    return sign * total;\n}\n</code></pre>\n'}, {'answer_id': 654386, 'author': 'ʞɔıu', 'author_id': 41613, 'author_profile': 'https://Stackoverflow.com/users/41613', 'pm_score': -1, 'selected': False, 'text': '<p>One place to start looking is the <a href="http://www.gnu.org/software/tar/manual/html_node/Date-input-formats.html" rel="nofollow noreferrer">gnu get_date lib</a>, which can parse <a href="http://php-date.com/#strtotime" rel="nofollow noreferrer">just about any</a> English textual date into a timestamp. While not exactly what you\'re looking for, their solution to a similar problem could provide a lot of useful clues.</p>\n'}, {'answer_id': 654409, 'author': 'fmsf', 'author_id': 26004, 'author_profile': 'https://Stackoverflow.com/users/26004', 'pm_score': 3, 'selected': False, 'text': '<p>You should keep in mind that Europe and America count differently.</p>\n<h3>European standard:</h3>\n<pre><code>One Thousand\nOne Million\nOne Thousand Millions (British also use Milliard)\nOne Billion\nOne Thousand Billions\nOne Trillion\nOne Thousand Trillions\n</code></pre>\n<p><a href="http://www.jimloy.com/math/billion.htm" rel="nofollow noreferrer">Here</a> is a small reference on it.</p>\n<hr />\n<p>A simple way to see the difference is the following:</p>\n<pre><code>(American counting Trillion) == (European counting Billion)\n</code></pre>\n'}, {'answer_id': 663741, 'author': 'DrFloyd5', 'author_id': 1736623, 'author_profile': 'https://Stackoverflow.com/users/1736623', 'pm_score': -1, 'selected': False, 'text': '<p>Try</p>\n\n<ol>\n<li><p>Open an HTTP Request to "<a href="http://www.google.com/search?q=" rel="nofollow noreferrer">http://www.google.com/search?q=</a>" + number + "+in+decimal".</p></li>\n<li><p>Parse the result for your number.</p></li>\n<li><p>Cache the number / result pairs to lesson the requests over time.</p></li>\n</ol>\n'}, {'answer_id': 668360, 'author': 'LarryF', 'author_id': 18518, 'author_profile': 'https://Stackoverflow.com/users/18518', 'pm_score': 2, 'selected': False, 'text': '<p>Well, I was too late on the answer for this question, but I was working a little test scenario that seems to have worked very well for me.  I used a (simple, but ugly, and large) regular expression to locate all the words for me.  The expression is as follows:</p>\n\n<pre><code>(?&lt;Value&gt;(?:zero)|(?:one|first)|(?:two|second)|(?:three|third)|(?:four|fourth)|\n(?:five|fifth)|(?:six|sixth)|(?:seven|seventh)|(?:eight|eighth)|(?:nine|ninth)|\n(?:ten|tenth)|(?:eleven|eleventh)|(?:twelve|twelfth)|(?:thirteen|thirteenth)|\n(?:fourteen|fourteenth)|(?:fifteen|fifteenth)|(?:sixteen|sixteenth)|\n(?:seventeen|seventeenth)|(?:eighteen|eighteenth)|(?:nineteen|nineteenth)|\n(?:twenty|twentieth)|(?:thirty|thirtieth)|(?:forty|fortieth)|(?:fifty|fiftieth)|\n(?:sixty|sixtieth)|(?:seventy|seventieth)|(?:eighty|eightieth)|(?:ninety|ninetieth)|\n(?&lt;Magnitude&gt;(?:hundred|hundredth)|(?:thousand|thousandth)|(?:million|millionth)|\n(?:billion|billionth)))\n</code></pre>\n\n<p>Shown here with line breaks for formatting purposes..</p>\n\n<p>Anyways, my method was to execute this RegEx with a library like PCRE, and then read back the named matches.  And it worked on all of the different examples listed in this question, minus the "One Half", types, as I didn\'t add them in, but as you can see, it wouldn\'t be hard to do so.  This addresses a lot of issues.  For example, it addresses the following items in the original question and other answers:</p>\n\n<ol>\n<li>cardinal/nominal or ordinal: "one" and "first"</li>\n<li>common spelling mistakes: "forty"/"fourty" (Note that it does not EXPLICITLY address this, that would be something you\'d want to do before you passed the string to this    parser.  This parser sees this example as "FOUR"...)</li>\n<li>hundreds/thousands: 2100 -> "twenty one hundred" and also "two thousand and one hundred"</li>\n<li>separators: "eleven hundred fifty two", but also "elevenhundred fiftytwo" or "eleven-hundred fifty-two" and whatnot</li>\n<li>colloqialisms: "thirty-something" (This also is not TOTALLY addressed, as what IS "something"?  Well, this code finds this number as simply "30").**</li>\n</ol>\n\n<p>Now, rather than store this monster of a regular expression in your source, I was considering building this RegEx at runtime, using something like the following:</p>\n\n<pre><code>char *ones[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve",\n  "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};\nchar *tens[] = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};\nchar *ordinalones[] = { "", "first", "second", "third", "fourth", "fifth", "", "", "", "", "", "", "twelfth" };\nchar *ordinaltens[] = { "", "", "twentieth", "thirtieth", "fortieth", "fiftieth", "sixtieth", "seventieth", "eightieth", "ninetieth" };\nand so on...\n</code></pre>\n\n<p>The easy part here is we are only storing the words that matter.  In the case of SIXTH, you\'ll notice that there isn\'t an entry for it, because it\'s just it\'s normal number with TH tacked on...  But ones like TWELVE need different attention.</p>\n\n<p>Ok, so now we have the code to build our (ugly) RegEx, now we just execute it on our number strings.</p>\n\n<p>One thing I would recommend, is to filter, or eat the word "AND".  It\'s not necessary, and only leads to other issues.</p>\n\n<p>So, what you are going to want to do is setup a function that passes the named matches for "Magnitude" into a function that looks at all the possible magnitude values, and multiplies your current result by that value of magnitude.  Then, you create a function that looks at the "Value" named matches, and returns an int (or whatever you are using), based on the value discovered there.</p>\n\n<p>All VALUE matches are ADDED to your result, while magnitutde matches multiply the result by the mag value.  So, Two Hundred Fifty Thousand becomes "2", then "2 * 100", then "200 + 50", then "250 * 1000", ending up with 250000...</p>\n\n<p>Just for fun, I wrote a vbScript version of this and it worked great with all the examples provided.  Now, it doesn\'t support named matches, so I had to work a little harder getting the correct result, but I got it.  Bottom line is, if it\'s a "VALUE" match, add it your accumulator.  If it\'s a magnitude match, multiply your accumulator by 100, 1000, 1000000, 1000000000, etc...  This will provide you with some pretty amazing results, and all you have to do to adjust for things like "one half" is add them to your RegEx, put in a code marker for them, and handle them.</p>\n\n<p>Well, I hope this post helps SOMEONE out there.  If anyone want, I can post by vbScript pseudo code that I used to test this with, however, it\'s not pretty code, and NOT production code.</p>\n\n<p>If I may..  What is the final language this will be written in?  C++, or something like a scripted language?  Greg Hewgill\'s source will go a long way in helping understand how all of this comes together.</p>\n\n<p>Let me know if I can be of any other help.  Sorry, I only know English/American, so I can\'t help you with the other languages.</p>\n'}, {'answer_id': 6925837, 'author': 'Alex Brooks', 'author_id': 177674, 'author_profile': 'https://Stackoverflow.com/users/177674', 'pm_score': 3, 'selected': False, 'text': '<p>Use the Python <a href="http://www.clips.ua.ac.be/pages/pattern-en" rel="nofollow">pattern-en</a> library: </p>\n\n<pre><code>&gt;&gt;&gt; from pattern.en import number\n&gt;&gt;&gt; number(\'two thousand fifty and a half\') =&gt; 2050.5\n</code></pre>\n'}, {'answer_id': 8112142, 'author': 'Mike Mattie', 'author_id': 1044238, 'author_profile': 'https://Stackoverflow.com/users/1044238', 'pm_score': 2, 'selected': False, 'text': '<p>Here is an extremely robust solution in Clojure.</p>\n\n<p>AFAIK it is a unique implementation approach.</p>\n\n<pre><code>;----------------------------------------------------------------------\n; numbers.clj\n; written by: Mike Mattie codermattie@gmail.com\n;----------------------------------------------------------------------\n(ns operator.numbers\n  (:use compojure.core)\n\n  (:require\n    [clojure.string     :as string] ))\n\n(def number-word-table {\n  "zero"          0\n  "one"           1\n  "two"           2\n  "three"         3\n  "four"          4\n  "five"          5\n  "six"           6\n  "seven"         7\n  "eight"         8\n  "nine"          9\n  "ten"           10\n  "eleven"        11\n  "twelve"        12\n  "thirteen"      13\n  "fourteen"      14\n  "fifteen"       15\n  "sixteen"       16\n  "seventeen"     17\n  "eighteen"      18\n  "nineteen"      19\n  "twenty"        20\n  "thirty"        30\n  "fourty"        40\n  "fifty"         50\n  "sixty"         60\n  "seventy"       70\n  "eighty"        80\n  "ninety"        90\n})\n\n(def multiplier-word-table {\n  "hundred"       100\n  "thousand"      1000\n})\n\n(defn sum-words-to-number [ words ]\n  (apply + (map (fn [ word ] (number-word-table word)) words)) )\n\n; are you down with the sickness ?\n(defn words-to-number [ words ]\n  (let\n    [ n           (count words)\n\n      multipliers (filter (fn [x] (not (false? x))) (map-indexed\n                                                      (fn [ i word ]\n                                                        (if (contains? multiplier-word-table word)\n                                                          (vector i (multiplier-word-table word))\n                                                          false))\n                                                      words) )\n\n      x           (ref 0) ]\n\n    (loop [ indices (reverse (conj (reverse multipliers) (vector n 1)))\n            left    0\n            combine + ]\n      (let\n        [ right (first indices) ]\n\n        (dosync (alter x combine (* (if (&gt; (- (first right) left) 0)\n                                      (sum-words-to-number (subvec words left (first right)))\n                                      1)\n                                    (second right)) ))\n\n        (when (&gt; (count (rest indices)) 0)\n          (recur (rest indices) (inc (first right))\n            (if (= (inc (first right)) (first (second indices)))\n              *\n              +))) ) )\n    @x ))\n</code></pre>\n\n<p>Here are some examples</p>\n\n<pre><code>(operator.numbers/words-to-number ["six" "thousand" "five" "hundred" "twenty" "two"])\n(operator.numbers/words-to-number ["fifty" "seven" "hundred"])\n(operator.numbers/words-to-number ["hundred"])\n</code></pre>\n'}, {'answer_id': 41387461, 'author': 'duhaime', 'author_id': 1727392, 'author_profile': 'https://Stackoverflow.com/users/1727392', 'pm_score': 0, 'selected': False, 'text': '<p>I was converting ordinal edition statements from early modern books (e.g. "2nd edition", "Editio quarta") to integers and needed support for ordinals 1-100 in English and ordinals 1-10 in a few Romance languages. Here\'s what I came up with in Python:</p>\n\n<pre><code>def get_data_mapping():\n  data_mapping = {\n    "1st": 1,\n    "2nd": 2,\n    "3rd": 3,\n\n    "tenth": 10,\n    "eleventh": 11,\n    "twelfth": 12,\n    "thirteenth": 13,\n    "fourteenth": 14,\n    "fifteenth": 15,\n    "sixteenth": 16,\n    "seventeenth": 17,\n    "eighteenth": 18,\n    "nineteenth": 19,\n    "twentieth": 20,\n\n    "new": 2,\n    "newly": 2,\n    "nova": 2,\n    "nouvelle": 2,\n    "altera": 2,\n    "andere": 2,\n\n    # latin\n    "primus": 1,\n    "secunda": 2,\n    "tertia": 3,\n    "quarta": 4,\n    "quinta": 5,\n    "sexta": 6,\n    "septima": 7,\n    "octava": 8,\n    "nona": 9,\n    "decima": 10,\n\n    # italian\n    "primo": 1,\n    "secondo": 2,\n    "terzo": 3,\n    "quarto": 4,\n    "quinto": 5,\n    "sesto": 6,\n    "settimo": 7,\n    "ottavo": 8,\n    "nono": 9,\n    "decimo": 10,\n\n    # french\n    "premier": 1,\n    "deuxième": 2,\n    "troisième": 3,\n    "quatrième": 4,\n    "cinquième": 5,\n    "sixième": 6,\n    "septième": 7,\n    "huitième": 8,\n    "neuvième": 9,\n    "dixième": 10,\n\n    # spanish\n    "primero": 1,\n    "segundo": 2,\n    "tercero": 3,\n    "cuarto": 4,\n    "quinto": 5,\n    "sexto": 6,\n    "septimo": 7,\n    "octavo": 8,\n    "noveno": 9,\n    "decimo": 10\n  }\n\n  # create 4th, 5th, ... 20th\n  for i in xrange(16):\n    data_mapping[str(4+i) + "th"] = 4+i\n\n  # create 21st, 22nd, ... 99th\n  for i in xrange(79):\n    last_char = str(i)[-1]\n\n    if last_char == "0":\n      data_mapping[str(20+i) + "th"] = 20+i\n\n    elif last_char == "1":\n      data_mapping[str(20+i) + "st"] = 20+i\n\n    elif last_char == "2":\n      data_mapping[str(20+i) + "nd"] = 20+i\n\n    elif last_char == "3":\n      data_mapping[str(20+i) + "rd"] = 20+i\n\n    else:\n      data_mapping[str(20+i) + "th"] = 20+i\n\n  ordinals = [\n    "first", "second", "third", \n    "fourth", "fifth", "sixth", \n    "seventh", "eighth", "ninth"\n  ]\n\n  # create first, second ... ninth\n  for c, i in enumerate(ordinals):\n    data_mapping[i] = c+1\n\n  # create twenty-first, twenty-second ... ninty-ninth\n  for ci, i in enumerate([\n    "twenty", "thirty", "forty", \n    "fifty", "sixty", "seventy", \n    "eighty", "ninety"\n  ]):\n    for cj, j in enumerate(ordinals):\n      data_mapping[i + "-" + j] = 20 + (ci*10) + (cj+1)\n    data_mapping[i.replace("y", "ieth")] = 20 + (ci*10)\n\n  return data_mapping\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/70161', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11414/']