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

问题描述 投票:1回答:1

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

[另一个问题,是他们执行严格的数学方程式,如sin,cos集成和所有高级数学的任何API或库。并给出为达成解决方案而执行的过程。

java android math
1个回答
1
投票

您要问的并不是特别困难,但是随着您增加复杂性而变得更加棘手。根据您需要了解的内容,这可能会变得非常复杂。但是对于简单的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
© www.soinside.com 2019 - 2024. All rights reserved.