static final String[] DIGITS = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; static final String[] TENS = {null, "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; static final String[] TEENS = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; static final String[] MAGNITUDES = {"hundred", "thousand", "million", "point"}; static final String[] ZERO = {"zero", "oh"}; static S parseEnglishNumbers(S input) { if (empty(input)) ret input; input = trim(simpleSpaces(lower(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; } if (uno.equals(TEENS[k])) triplet[2] = k+10; } } 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); } } } } while (startsWith(result, "0") && !startsWith(result, "0.")) result = dropFirst(result); ret result; }
From https://stackoverflow.com/questions/4062022/how-to-convert-words-to-a-number
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lnbujpyubztb, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney
No comments. add comment
Snippet ID: | #1013121 |
Snippet name: | parseEnglishNumbers |
Eternal ID of this version: | #1013121/6 |
Text MD5: | 172463a74b7e68535a189b5b2a12e33b |
Author: | stefan |
Category: | javax / a.i. |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2019-03-21 00:54:06 |
Source code size: | 4913 bytes / 131 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 403 / 488 |
Version history: | 5 change(s) |
Referenced in: | [show references] |