Search code examples
javamathnlp

Converting natural language to a math equation


I've got a home automation system working in Java, and I want to add simple math capabilities such as addition, subtraction, multiplication, division, roots, and powers.

At the system current state, it can convert a phrase into tags, as shown in the following examples:

example:

Phrase: "what is one hundred twenty two to the power of seven"
Tagged: {QUESTION/math} {NUMBER/122} {MATH/pwr} {NUMBER/7}

example:

Phrase: "twenty seven plus pi 3 squared"
Tagged: {NUMBER/27} {MATH/add} {NUMBER/3.14159} {MATH/multiply} {MATH/pwr} {NUMBER/2}

This example could be just as easily converted to something like this:

27 + 3.14159 * 3^2

Each tag is an object that can be queried for it value.

Edit: Specific question:

So now I need a way to read that group of tags as an equation, and return a numerical result. As a last resort I could use google or wolfram alpha, but that will be slower, and I'm trying to keep the automation system completely self contained.

If you would like to see the entire source, here it is in github. Note that I have not committed the last few few changes, so some of the math related things I gave examples will not work.


Solution

  • After some more googleing (I didn't know the name for what I was doing at first) I found someone who has done something similar already:

    http://www.objecthunter.net/exp4j/

    Edit: Finished: https://github.com/Sprakle/HomeAutomation/blob/master/src/net/sprakle/homeAutomation/interpretation/module/modules/math/Math.java