简体   繁体   中英

Converting text mathematical equations to number and signs

I actually have two questions. first how do I convert text (ex. One) to actual numbers (1) and plus to +. As I am trying to take the speech which starts with calculate and do the maths within that speech. But for some reason speech recognition write down numbers and signs in text ( one plus three ) rather than (1+3).

The other question, is their any API or libraries that carry out heavy math equation like sin,cos integration and all the a level math. and giving out the process that it carried out to reach the solution.

What you're asking is not particularly difficult, but gets trickier as you increase the complexity. Depending on how much you need to understand, this may become very complicated. However for simple number plus number or number minus number , it's reasonably easy. To get you started, the following code will be able to deal with these two scenarios. Feel free to expand it as you wish. Also note that it has minimal error checking - in a production system you'll need quite a bit more of it.

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

This code handles numbers from 0 to 999,999,999 and doesn't handle negative numbers. Again, it shouldn't be too difficult to extend it to increase the range or handle negative numbers. Note that if you extend it to handle billions, you may need to switch from integer to long variables to hold the results.

Here are some test runs:

$ 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM