Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

131
LINES

< > BotCompany Repo | #1013121 // parseEnglishNumbers

JavaX fragment (include)

1  
static final String[] DIGITS = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
2  
static final String[] TENS = {null, "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
3  
static final String[] TEENS = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
4  
static final String[] MAGNITUDES = {"hundred", "thousand", "million", "point"};
5  
static final String[] ZERO = {"zero", "oh"};
6  
7  
static S parseEnglishNumbers(S input) {
8  
  if (empty(input)) ret input;
9  
  input = trim(simpleSpaces(lower(input)));
10  
  String result = "";
11  
  String[] decimal = input.split(MAGNITUDES[3]);
12  
  String[] millions = decimal[0].split(MAGNITUDES[2]);
13  
14  
  for (int i = 0; i < millions.length; i++) {
15  
    String[] thousands = millions[i].split(MAGNITUDES[1]);
16  
17  
    for (int j = 0; j < thousands.length; j++) {
18  
        int[] triplet = {0, 0, 0};
19  
        StringTokenizer set = new StringTokenizer(thousands[j]);
20  
21  
        if (set.countTokens() == 1) { //If there is only one token given in triplet
22  
            String uno = set.nextToken();
23  
            triplet[0] = 0;
24  
            for (int k = 0; k < DIGITS.length; k++) {
25  
                if (uno.equals(DIGITS[k])) {
26  
                    triplet[1] = 0;
27  
                    triplet[2] = k + 1;
28  
                }
29  
                if (uno.equals(TENS[k])) {
30  
                    triplet[1] = k + 1;
31  
                    triplet[2] = 0;
32  
                }
33  
                if (uno.equals(TEENS[k])) triplet[2] = k+10;
34  
            }
35  
        }
36  
37  
38  
        else if (set.countTokens() == 2) {  //If there are two tokens given in triplet
39  
            String uno = set.nextToken();
40  
            String dos = set.nextToken();
41  
            if (dos.equals(MAGNITUDES[0])) {  //If one of the two tokens is "hundred"
42  
                for (int k = 0; k < DIGITS.length; k++) {
43  
                    if (uno.equals(DIGITS[k])) {
44  
                        triplet[0] = k + 1;
45  
                        triplet[1] = 0;
46  
                        triplet[2] = 0;
47  
                    }
48  
                }
49  
            }
50  
            else {
51  
                triplet[0] = 0;
52  
                for (int k = 0; k < DIGITS.length; k++) {
53  
                    if (uno.equals(TENS[k])) {
54  
                        triplet[1] = k + 1;
55  
                    }
56  
                    if (dos.equals(DIGITS[k])) {
57  
                        triplet[2] = k + 1;
58  
                    }
59  
                }
60  
            }
61  
        }
62  
63  
        else if (set.countTokens() == 3) {  //If there are three tokens given in triplet
64  
            String uno = set.nextToken();
65  
            String dos = set.nextToken();
66  
            String tres = set.nextToken();
67  
            for (int k = 0; k < DIGITS.length; k++) {
68  
                if (uno.equals(DIGITS[k])) {
69  
                    triplet[0] = k + 1;
70  
                }
71  
                if (tres.equals(DIGITS[k])) {
72  
                    triplet[1] = 0;
73  
                    triplet[2] = k + 1;
74  
                }
75  
                if (tres.equals(TENS[k])) {
76  
                    triplet[1] = k + 1;
77  
                    triplet[2] = 0;
78  
                }
79  
            }
80  
        }
81  
82  
        else if (set.countTokens() == 4) {  //If there are four tokens given in triplet
83  
            String uno = set.nextToken();
84  
            String dos = set.nextToken();
85  
            String tres = set.nextToken();
86  
            String cuatro = set.nextToken();
87  
            for (int k = 0; k < DIGITS.length; k++) {
88  
                if (uno.equals(DIGITS[k])) {
89  
                    triplet[0] = k + 1;
90  
                }
91  
                if (cuatro.equals(DIGITS[k])) {
92  
                    triplet[2] = k + 1;
93  
                }
94  
                if (tres.equals(TENS[k])) {
95  
                    triplet[1] = k + 1;
96  
                }
97  
            }
98  
        }
99  
        else {
100  
            triplet[0] = 0;
101  
            triplet[1] = 0;
102  
            triplet[2] = 0;
103  
        }
104  
105  
        result = result + Integer.toString(triplet[0]) + Integer.toString(triplet[1]) + Integer.toString(triplet[2]);
106  
    }
107  
  }
108  
  
109  
  if (decimal.length > 1) {  //The number is a decimal
110  
    StringTokenizer decimalDigits = new StringTokenizer(decimal[1]);
111  
    result = result + ".";
112  
    System.out.println(decimalDigits.countTokens() + " decimal digits");
113  
    while (decimalDigits.hasMoreTokens()) {
114  
        String w = decimalDigits.nextToken();
115  
        System.out.println(w);
116  
117  
        if (w.equals(ZERO[0]) || w.equals(ZERO[1])) {
118  
            result = result + "0";
119  
        }
120  
        for (int j = 0; j < DIGITS.length; j++) {
121  
            if (w.equals(DIGITS[j])) {
122  
                result = result + Integer.toString(j + 1);
123  
            }   
124  
        }
125  
126  
    }
127  
  }
128  
129  
  while (startsWith(result, "0") && !startsWith(result, "0.")) result = dropFirst(result);
130  
  ret result;
131  
}

Author comment

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: 404 / 489
Version history: 5 change(s)
Referenced in: [show references]