gpt4 book ai didi

java - 将文本数学方程式转换为数字和符号

转载 作者:行者123 更新时间:2023-11-30 03:33:19 28 4
gpt4 key购买 nike

其实我有两个问题。首先,如何将文本(例如 One)转换为实际数字(1)并将加号转换为 +。因为我正在尝试以计算开头的演讲,并在该演讲中进行数学运算。但出于某种原因,语音识别会在文本中记下数字和符号(一加三)而不是(1+3)。

另一个问题是,他们是否有任何 API 或库执行繁重的数学方程式,如 sin、cos 积分和所有 a 级数学。并给出了它为达到解决方案而执行的过程。

最佳答案

您要问的并不是特别困难,但随着复杂性的增加会变得更加棘手。根据您需要了解的程度,这可能会变得非常复杂。但是对于简单的number plus numbernumber minus number,这相当容易。为了让您入门,以下代码将能够处理这两种情况。随意扩展它。另请注意,它具有最少的错误检查 - 在生产系统中,您将需要更多的错误检查。

import java.util.Map;
import java.util.HashMap;

public class Nums {
private static Map<String, Integer> nums = new HashMap<String, Integer>();
public static void main(String[] args) {
nums.put("zero", 0);
nums.put("one", 1);
nums.put("two", 2);
nums.put("three", 3);
nums.put("four", 4);
nums.put("five", 5);
nums.put("six", 6);
nums.put("seven", 7);
nums.put("eight", 8);
nums.put("nine", 9);
nums.put("ten", 10);
nums.put("eleven", 11);
nums.put("twelve", 12);
nums.put("thirteen", 13);
nums.put("fourteen", 14);
nums.put("fifteen", 15);
nums.put("sixteen", 16);
nums.put("seventeen", 17);
nums.put("eighteen", 18);
nums.put("nineteen", 19);
nums.put("twenty", 20);
nums.put("thirty", 30);
nums.put("forty", 40);
nums.put("fifty", 50);
nums.put("sixty", 60);
nums.put("seventy", 70);
nums.put("eighty", 80);
nums.put("ninety", 90);


String input = args[0].toLowerCase();

int pos;
String num1, num2;
int res1, res2;
if((pos = input.indexOf(" plus ")) != -1) {
num1 = input.substring(0, pos);
num2 = input.substring(pos + 6);

res1 = getNumber(num1);
res2 = getNumber(num2);
System.out.println(args[0] + " => " + res1 + " + " + res2 + " = " + (res1 + res2));
}
else if((pos = input.indexOf(" minus ")) != -1) {
num1 = input.substring(0, pos);
num2 = input.substring(pos + 7);

res1 = getNumber(num1);
res2 = getNumber(num2);
System.out.println(args[0] + " => " + res1 + " - " + res2 + " = " + (res1 - res2));
}
else {
System.out.println(args[0] + " => " + getNumber(args[0]));
}
}

private static int getNumber(String input) {
String[] parts = input.split(" +");
int number = 0;
int mult = 1;
String fact;

for(int i=parts.length-1; i>=0; i--) {
parts[i] = parts[i].toLowerCase();
if(parts[i].equals("hundreds") || parts[i].equals("hundred")) {
mult *= 100;
}
else if(parts[i].equals("thousands") || parts[i].equals("thousand")) {
if(number >= 1000) {
throw new NumberFormatException("Invalid input (part " + (i + 1) + ")");
}
mult = 1000;
}
else if(parts[i].equals("millions") || parts[i].equals("million")) {
if(number >= 1000000) {
throw new NumberFormatException("Invalid input (part " + (i + 1) + ")");
}
mult = 1000000;
}
else if(!nums.containsKey(parts[i])) {
throw new NumberFormatException("Invalid input (part " + (i + 1) + ")");
}
else {
number += mult * nums.get(parts[i]);
}
}

if(!nums.containsKey(parts[0])) {
number += mult;
}

return number;
}
}

此代码处理从 0 到 999,999,999 的数字,不处理负数。同样,扩展它以增加范围或处理负数应该不会太困难。请注意,如果您扩展它以处理数十亿,您可能需要从 integer 切换到 long 变量来保存结果。

以下是一些测试运行:

$ java Nums "three hundred nineteen million five hundred twenty three thousand six hundred eighteen"
three hundred nineteen million five hundred twenty three thousand six hundred eighteen => 319523618
$ java Nums "five hundred minus three hundred ninety nine"
five hundred minus three hundred ninety nine => 500 - 399 = 101
$ java Nums "thirty three plus seventeen"
thirty three plus seventeen => 33 + 17 = 50
$ java Nums zero
zero => 0
$ java Nums "one plus three"
one plus three => 1 + 3 = 4
$ java Nums "hundred thousand"
hundred thousand => 100000
$ java Nums "hundred thousand minus ten thousand"
hundred thousand minus ten thousand => 100000 - 10000 = 90000

关于java - 将文本数学方程式转换为数字和符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17043371/

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