Java – A better way to interpret user text input

A better way to interpret user text input… here is a solution to the problem.

A better way to interpret user text input

There is a text box in my app where the user can type anything. I’m creating a Controller that explains some commands based on keywords.

The user can choose whether to set a password. And depending on this, does my interpreter need a password.

For example, if a password is set and the user enters: “password1 show list”.
The interpreter checks the password in the first word, then the command (show), and then the argument (list).

If no password is set, the user enters a “show list” and the interpreter understands.

My current code is:

// 'words' is a String array which contains the user input.

if (passwordEnabled()==true) {

if (words[0].contentEquals(getPassword())
                    && words[1].contentEquals("show")
                    && words[2].contentEquals("list")) {

 Perform action

}
}
else {

if (words[0].contentEquals("show")
                    && words[1].contentEquals("list")) {

 Perform action

}

}

My question is, is there a more efficient/better way to handle commands? I’m sure my approach is a very crude approach.

Solution

If the user is able to remember the syntax of the command, your method (or any grammar-based method) will do.

If you want to understand users as they express their intent using unrestricted natural language, you need to do a few things Natural Language Understanding .

Something like Wit ( http://wit.ai ) might do the job for you. It is based on machine learning. It uses user intent and normalization parameters to convert free-text sentences to JSON.

Related Problems and Solutions