Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best method to parse strings for multiple word combinations?

I'm writing a program that attempts to derive meaning from natural language. The program will accept a String, and see if it contains certain combinations of words. See the following code snippet for an example:

if (phrase.contains("turn")) { // turn something on/off
    if (phrase.contains("on") && !phrase.contains("off")) { // turn something ON
        if (phrase.contains("pc") || phrase.contains("computer")) // turn on computer
            turnOnComputer();
        else if (phrase.contains("light") || phrase.contains("lamp")) // turn on lights
            turnOnLights();
        else
            badPhrase();
    }
    else if (phrase.contains("off") && !phrase.contains("on")) { // turn something OFF
        if (phrase.contains("pc") || phrase.contains("computer")) // turn off computer
            turnOffComputer();
        else if (phrase.contains("light") || phrase.contains("lamp")) // turn off lights
            turnOffLights();
        else
            badPhrase();
    }
    else {
        badPhrase();
    }
}
else {
    badPhrase();
}

As you can see, this can quickly become an unmanageable mess of code if I want to interpret more than a few meanings. How can I manage this better?

like image 446
BLuFeNiX Avatar asked Mar 24 '23 01:03

BLuFeNiX


1 Answers

Apache OpenNLP is a machine learning based toolkit for the processing of natural language text.

It includes a sentence detector, a tokenizer, a parts-of-speech (POS) tagger, and a treebank parser.

Manual for NLP

Download

Hope it helps ; )

like image 95
aran Avatar answered Mar 29 '23 23:03

aran