1

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

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

4

1 回答 1

1

你所问的并不是特别困难,但随着复杂性的增加会变得更加棘手。根据您需要了解多少,这可能会变得非常复杂。但是对于简单的number plus numberor number 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 的数字,并且不处理负数。同样,扩展它以增加范围或处理负数应该不会太难。请注意,如果您将其扩展到处理数十亿,您可能需要从变量切换到保存结果integerlong

以下是一些测试运行:

$ 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
于 2013-06-11T14:46:13.453 回答