gpt4 book ai didi

java - 如何将单词转换为数字?

转载 作者:IT老高 更新时间:2023-10-28 20:46:26 26 4
gpt4 key购买 nike

我想将包含字母字符的单词转换为 Java 中的代表数字。

例如,404 应计算为数字 404

如果字母像 asdf 那样乱码,那就是错误。

我知道我可以 convert bare Characters to their ascii equivalent Integer ,将它们附加在一起,但我只想提取英文单词短语后面的数字。

最佳答案

这是我在尝试解决相同问题时想出的一些代码。请记住,我不是专业人士,也没有疯狂的经验。它并不慢,但我相信它可以更快/更清洁/等。我用它来将语音识别的单词转换为数字,以便在我自己的“Jarvis”a la Iron Man 中进行计算。它可以处理 10 亿以下的数字,尽管它可以很容易地扩展到包含更高数量级的数据,而只需花费很少的时间。

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;
}

输入必须是语法正确的语法,否则会出现问题(创建一个函数来删除“and”)。一个字符串输入“二亿二百万五万三千点零八五八哦二”返回:

two hundred two million fifty three thousand point zero eight five eight oh two
202053000.085802
It took 2 milliseconds.

关于java - 如何将单词转换为数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4062022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com