Solving a mathematical equation written in words in Java -
im trying solve equation this: four plus ten divided (fifty-five plus nine)
so response: 4.15625
i know can solve equation of string sentence = "4+10/(55+9)"
so:
scriptenginemanager mgr = new scriptenginemanager(); scriptengine engine = mgr.getenginebyname("javascript"); string infix = sentence; try { system.out.println(engine.eval(infix)); } catch (scriptexception e) { // todo auto-generated catch block e.printstacktrace(); }
but there anyway solve words (nine..plus..divide..) equation. or there way convert words (nine..plus..divide..) string (9...+.../...) , use function shown above solve it?
thanks!
you can go parser library antlr better idea if want full, expressive language , proper error handling.
for quick , dirty solution can write own parser using regular expressions , replacement map:
public class naturallanguagemath { public static void main(string[] args) { string sentence = "four plus ten divided (fifty-five plus nine)"; system.out.println(tomath(sentence)); } // regular expression match our symbols // special case "divided by" since that's 2 words 1 symbol static final pattern symbol_pattern = pattern.compile("(divided by|[a-z-]+)", pattern.case_insensitive); private static string tomath(string sentence) { // builder build translated string stringbuilder builder = new stringbuilder(); // end of last matched group int lastend = 0; // go through symbols in string matcher matcher = symbol_pattern.matcher(sentence); while(matcher.find()) { // matched symbol string symbol = matcher.group(0).tolowercase(); // replacement string replacement = getreplacement(symbol); // append between previous match , match builder.append(sentence.substring(lastend, matcher.start())); // append replacement builder.append(replacement); // update end lastend = matcher.end(); } // append end of string , return builder.append(sentence.substring(lastend)); return builder.tostring(); } // map hold replacement symbols static final hashmap<string, string> replacement_map = new hashmap<>(); static { replacement_map.put("four", "4"); replacement_map.put("plus", "+"); replacement_map.put("ten", "10"); replacement_map.put("fifty", "50"); replacement_map.put("five", "5"); replacement_map.put("nine", "9"); replacement_map.put("divided by", "/"); } private static string getreplacement(string symbol) { // handle compounds such fifty-five if(symbol.contains("-")) { // add each individual symbol , return result // far perfect since allow compounds such // fifty-five-nine become 64 int value = 0; // go through each individual symbol , translate string[] symbols = symbol.split("-"); for(string s : symbols) { if(!replacement_map.containskey(s)) { throw new illegalargumentexception("unknown symbol: " + s); } value += integer.parseint(getreplacement(s)); } return string.valueof(value); // straight translation } else if (replacement_map.containskey(symbol)) { return replacement_map.get(symbol); // unknown symbol } else { throw new illegalargumentexception("unknown symbol: " + symbol); } } }
output:
4 + 10 / (55 + 9)
Comments
Post a Comment