Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert words to a number? [closed]

Tags:

I want to convert words containing alphabetical characters into a representative number in Java.

For example, four hundred four should evaluate to the number 404.

If the letters are gibberish like asdf then that's an error.

I know I can convert bare Characters to their ascii equivalent Integer, appending those together, but I only want the numbers behind the English word phrases extracted.

like image 639
mimi Avatar asked Oct 31 '10 05:10

mimi


People also ask

How do I convert text to numbers?

Select the cells that have numbers stored as text. On the Home tab, click Paste > Paste Special. Click Multiply, and then click OK. Excel multiplies each cell by 1, and in doing so, converts the text to numbers.

Why can't I convert text to numbers in Excel?

On the Tools menu, click Options. In the Options dialog box, click the Error Checking tab. In the Settings section, click to select the Enable background error checking check box. In the Rules section, make sure the Number stored as text rule is selected, and then click OK.


1 Answers

Here is some code that I came up with when trying to solve the same problem. Keep in mind that I am not a professional and do not have insane amounts of experience. It is not slow, but I'm sure it could be faster/cleaner/etc. I used it in converting voice recognized words into numbers for calculation in my own "Jarvis" a la Iron Man. It can handle numbers under 1 billion, although it could easily be expanded to include much higher magnitudes at the cost of very little time.

public static final String[] DIGITS = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; public static final String[] TENS = {null, "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; public static final String[] TEENS = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; public static final String[] MAGNITUDES = {"hundred", "thousand", "million", "point"}; public static final String[] ZERO = {"zero", "oh"};  public static String replaceNumbers (String input) {     String result = "";     String[] decimal = input.split(MAGNITUDES[3]);     String[] millions = decimal[0].split(MAGNITUDES[2]);      for (int i = 0; i < millions.length; i++) {         String[] thousands = millions[i].split(MAGNITUDES[1]);          for (int j = 0; j < thousands.length; j++) {             int[] triplet = {0, 0, 0};             StringTokenizer set = new StringTokenizer(thousands[j]);              if (set.countTokens() == 1) { //If there is only one token given in triplet                 String uno = set.nextToken();                 triplet[0] = 0;                 for (int k = 0; k < DIGITS.length; k++) {                     if (uno.equals(DIGITS[k])) {                         triplet[1] = 0;                         triplet[2] = k + 1;                     }                     if (uno.equals(TENS[k])) {                         triplet[1] = k + 1;                         triplet[2] = 0;                     }                 }             }               else if (set.countTokens() == 2) {  //If there are two tokens given in triplet                 String uno = set.nextToken();                 String dos = set.nextToken();                 if (dos.equals(MAGNITUDES[0])) {  //If one of the two tokens is "hundred"                     for (int k = 0; k < DIGITS.length; k++) {                         if (uno.equals(DIGITS[k])) {                             triplet[0] = k + 1;                             triplet[1] = 0;                             triplet[2] = 0;                         }                     }                 }                 else {                     triplet[0] = 0;                     for (int k = 0; k < DIGITS.length; k++) {                         if (uno.equals(TENS[k])) {                             triplet[1] = k + 1;                         }                         if (dos.equals(DIGITS[k])) {                             triplet[2] = k + 1;                         }                     }                 }             }              else if (set.countTokens() == 3) {  //If there are three tokens given in triplet                 String uno = set.nextToken();                 String dos = set.nextToken();                 String tres = set.nextToken();                 for (int k = 0; k < DIGITS.length; k++) {                     if (uno.equals(DIGITS[k])) {                         triplet[0] = k + 1;                     }                     if (tres.equals(DIGITS[k])) {                         triplet[1] = 0;                         triplet[2] = k + 1;                     }                     if (tres.equals(TENS[k])) {                         triplet[1] = k + 1;                         triplet[2] = 0;                     }                 }             }              else if (set.countTokens() == 4) {  //If there are four tokens given in triplet                 String uno = set.nextToken();                 String dos = set.nextToken();                 String tres = set.nextToken();                 String cuatro = set.nextToken();                 for (int k = 0; k < DIGITS.length; k++) {                     if (uno.equals(DIGITS[k])) {                         triplet[0] = k + 1;                     }                     if (cuatro.equals(DIGITS[k])) {                         triplet[2] = k + 1;                     }                     if (tres.equals(TENS[k])) {                         triplet[1] = k + 1;                     }                 }             }             else {                 triplet[0] = 0;                 triplet[1] = 0;                 triplet[2] = 0;             }              result = result + Integer.toString(triplet[0]) + Integer.toString(triplet[1]) + Integer.toString(triplet[2]);         }     }      if (decimal.length > 1) {  //The number is a decimal         StringTokenizer decimalDigits = new StringTokenizer(decimal[1]);         result = result + ".";         System.out.println(decimalDigits.countTokens() + " decimal digits");         while (decimalDigits.hasMoreTokens()) {             String w = decimalDigits.nextToken();             System.out.println(w);              if (w.equals(ZERO[0]) || w.equals(ZERO[1])) {                 result = result + "0";             }             for (int j = 0; j < DIGITS.length; j++) {                 if (w.equals(DIGITS[j])) {                     result = result + Integer.toString(j + 1);                 }                }          }     }      return result; } 

Input must be in grammatically correct syntax, otherwise it will have issues (create a function to remove "and"). A string input of "two hundred two million fifty three thousand point zero eight five eight oh two" returns:

two hundred two million fifty three thousand point zero eight five eight oh two 202053000.085802 It took 2 milliseconds. 
like image 132
Elijah Avatar answered Oct 18 '22 23:10

Elijah