Search code examples
flutterflutter-dependencieshuman-language

Flutter: Understanding human language


this might be a bit of a unusual question. I have implemented voice input in my application and I need some kind of mechanism that will understand the semantics of human language. Assume the string msg contains the sentence that the user said.

For example lets say the user said "Turn off the device." In this case I could do something like this:

if(msg.contains("Turn off")){
   ...
}

However this would not cover other cases like if the user said "Power off the device" or other languages.

So I'd need some kind of library/package that can pull off something like this:

if(LanguageParser.stringHasMeaning(msg, "Turn off")){
   ...
}

Is there any library/package to achieve something like this in Flutter or in general. I know that the detection is not going be 100% accurate. Any guidance is appreciated. Thanks ^^


Solution

  • I ended up following @paulsm4 advice. I used a dedicated NLP (natural language processing) backend to parse the meaning of msg. In my case I used wit.ai because it is free and pretty straightforward to use.

    wit.ai takes your message and you can configure intents and entities to extract data from the message.

    Let's say your message is "turn on":

    1. You can send that message to the wit.ai backend like this: https://api.wit.ai/message?q=turn%20on
    2. wit.ai returns something like this:
    {
        "text": "turn on",
        "intents": [
            {
                "id": "391529335929866",
                "name": "turn_on",
                "confidence": 0.9496
            }
        ],
        "entities": {},
        "traits": {
            "wit$on_off": [
                {
                    "id": "53714d27-f6f6-43a0-ab93-1786e8cf6663",
                    "value": "on",
                    "confidence": 0.6317
                }
            ]
        }
    }
    
    1. You can parse the list of intents and execute the desired action based upon the intents you received.
    2. Keep in mind that you need to configure and train wit.ai with examples for this to work. There are however some inbuilt intents like turn_on.

    As for Flutter I used the standard http package for making the requests. You might want to do some local string manipulation before sending a request.