Search in sources :

Example 1 with Interpretation

use of ai.saiy.android.nlu.nuance.Interpretation in project Saiy-PS by brandall76.

the class NLUCoerce method validateNLUNuance.

/**
 * Validate the parameters prior to use.
 *
 * @param nluNuance the {@link NLUNuance} response object
 * @return true if the minimum parameters are present, false otherwise.
 */
private boolean validateNLUNuance(@NonNull final NLUNuance nluNuance) {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "validateNLUNuance");
    }
    final List<Interpretation> interpretations = nluNuance.getInterpretations();
    if (interpretations != null) {
        for (final Interpretation interpretation : interpretations) {
            if (UtilsString.notNaked(interpretation.getLiteral())) {
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "interpretation: getLiteral: " + interpretation.getLiteral());
                }
                if (interpretation.getAction() != null) {
                    if (interpretation.getAction().getIntent() != null) {
                        if (UtilsString.notNaked(interpretation.getAction().getIntent().getValue())) {
                            if (DEBUG) {
                                MyLog.i(CLS_NAME, "interpretation: getValue: " + interpretation.getAction().getIntent().getValue());
                                MyLog.i(CLS_NAME, "interpretation: getConfidence: " + interpretation.getAction().getIntent().getConfidence());
                            }
                            final Map<String, List<Concept>> map = interpretation.getConcept();
                            if (UtilsMap.notNaked(map)) {
                                for (final Map.Entry<String, List<Concept>> entry : map.entrySet()) {
                                    if (DEBUG) {
                                        MyLog.i(CLS_NAME, "key: " + entry.getKey() + " ~ " + entry.getValue());
                                    }
                                    final List<Concept> concepts = entry.getValue();
                                    if (UtilsList.notNaked(concepts)) {
                                        final int conceptSize = concepts.size();
                                        for (int i = 0; i < conceptSize; i++) {
                                            if (UtilsString.notNaked(concepts.get(i).getLiteral())) {
                                                if (DEBUG) {
                                                    MyLog.i(CLS_NAME, "concept: getLiteral: " + concepts.get(i).getLiteral());
                                                    MyLog.i(CLS_NAME, "concept: getValue: " + concepts.get(i).getValue());
                                                    MyLog.i(CLS_NAME, "concept: getRanges: " + Arrays.deepToString(concepts.get(i).getRanges()));
                                                }
                                                if (i == (conceptSize - 1)) {
                                                    return true;
                                                }
                                            } else {
                                                if (DEBUG) {
                                                    MyLog.w(CLS_NAME, "validateNLUNuance: literal naked");
                                                }
                                                break;
                                            }
                                        }
                                    } else {
                                        if (DEBUG) {
                                            MyLog.w(CLS_NAME, "validateNLUNuance: concepts naked");
                                        }
                                        break;
                                    }
                                }
                            } else {
                                if (DEBUG) {
                                    MyLog.i(CLS_NAME, "validateNLUNuance: no concepts to examine");
                                }
                                return true;
                            }
                        } else {
                            if (DEBUG) {
                                MyLog.w(CLS_NAME, "validateNLUNuance: value naked");
                            }
                            break;
                        }
                    } else {
                        if (DEBUG) {
                            MyLog.w(CLS_NAME, "validateNLUNuance: intent null");
                        }
                        break;
                    }
                } else {
                    if (DEBUG) {
                        MyLog.w(CLS_NAME, "validateNLUNuance: action null");
                    }
                    break;
                }
            } else {
                if (DEBUG) {
                    MyLog.w(CLS_NAME, "validateNLUNuance: literal naked");
                }
                break;
            }
        }
    } else {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "validateNLUNuance: interpretations null");
        }
    }
    return false;
}
Also used : Concept(ai.saiy.android.nlu.nuance.Concept) ArrayList(java.util.ArrayList) UtilsList(ai.saiy.android.utils.UtilsList) List(java.util.List) UtilsString(ai.saiy.android.utils.UtilsString) Interpretation(ai.saiy.android.nlu.nuance.Interpretation) Map(java.util.Map) UtilsMap(ai.saiy.android.utils.UtilsMap)

Example 2 with Interpretation

use of ai.saiy.android.nlu.nuance.Interpretation in project Saiy-PS by brandall76.

the class NLUCoerce method coerce.

/**
 * Coerce the NLP results into a generic {@link CommandRequest} object, validating the minimal
 * requirements for each implementation.
 */
public void coerce() {
    if (nluProvider instanceof NLUMicrosoft) {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "coerce: instanceof NLUMicrosoft");
        }
        if (validateNLUNLUMicrosoft((NLUMicrosoft) nluProvider)) {
            for (final Intent i : ((NLUMicrosoft) nluProvider).getIntents()) {
                if (i.getScore() > NLUMicrosoft.MIN_THRESHOLD) {
                    commandRequest.setCC(NLUConstants.intentToCC(i.getIntent()));
                    if (!commandRequest.getCC().equals(CC.COMMAND_UNKNOWN)) {
                        final NLUMicrosoftHelper microsoftHelper = new NLUMicrosoftHelper();
                        commandRequest = microsoftHelper.prepareCommand(mContext, commandRequest, getSupportedLanguage(), ((NLUMicrosoft) nluProvider).getEntities());
                        if (commandRequest.isResolved()) {
                            break;
                        }
                        break;
                    } else {
                        if (DEBUG) {
                            MyLog.i(CLS_NAME, "coerce: COMMAND_UNKNOWN");
                        }
                        commandRequest.setCC(CC.COMMAND_UNKNOWN);
                        commandRequest.setResolved(false);
                    }
                } else {
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "coerce: below threshold: " + i.getScore());
                    }
                    commandRequest.setCC(CC.COMMAND_UNKNOWN);
                    commandRequest.setResolved(false);
                }
            }
            if (!commandRequest.isResolved()) {
                commandRequest.setCC(CC.COMMAND_UNKNOWN);
                commandRequest.setResolved(false);
            }
        } else {
            if (DEBUG) {
                MyLog.w(CLS_NAME, "coerce: NLUMicrosoft validation failed");
            }
            commandRequest.setCC(CC.COMMAND_UNKNOWN);
            commandRequest.setResolved(false);
        }
    } else if (nluProvider instanceof NLUNuance) {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "coerce: instanceof NLUNuance");
        }
        if (validateNLUNuance((NLUNuance) nluProvider)) {
            for (final Interpretation interpretation : ((NLUNuance) nluProvider).getInterpretations()) {
                if (interpretation.getAction().getIntent().getConfidence() > NLUNuance.MIN_THRESHOLD) {
                    commandRequest.setCC(NLUConstants.intentToCC(interpretation.getAction().getIntent().getValue()));
                    if (!commandRequest.getCC().equals(CC.COMMAND_UNKNOWN)) {
                        final NLUNuanceHelper nuanceHelper = new NLUNuanceHelper();
                        commandRequest = nuanceHelper.prepareCommand(mContext, commandRequest, getSupportedLanguage(), interpretation.getConcept());
                        if (commandRequest.isResolved()) {
                            break;
                        }
                    } else {
                        if (DEBUG) {
                            MyLog.i(CLS_NAME, "coerce: COMMAND_UNKNOWN");
                        }
                        commandRequest.setCC(CC.COMMAND_UNKNOWN);
                        commandRequest.setResolved(false);
                    }
                } else {
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "coerce: below threshold: " + interpretation.getAction().getIntent().getConfidence());
                    }
                    commandRequest.setCC(CC.COMMAND_UNKNOWN);
                    commandRequest.setResolved(false);
                }
            }
            if (!commandRequest.isResolved()) {
                commandRequest.setCC(CC.COMMAND_UNKNOWN);
                commandRequest.setResolved(false);
            }
        } else {
            if (DEBUG) {
                MyLog.w(CLS_NAME, "coerce: NLUNuance validation failed");
            }
            commandRequest.setCC(CC.COMMAND_UNKNOWN);
            commandRequest.setResolved(false);
        }
    } else if (nluProvider instanceof NLUAPIAI) {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "coerce: instanceof NLUAPIAI");
        }
        if (validateNLUAPIAI((NLUAPIAI) nluProvider)) {
            commandRequest.setCC(NLUConstants.intentToCC(((NLUAPIAI) nluProvider).getIntent()));
            if (!commandRequest.getCC().equals(CC.COMMAND_UNKNOWN)) {
                final NLUAPIAIHelper apiaiHelper = new NLUAPIAIHelper();
                commandRequest = apiaiHelper.prepareCommand(mContext, commandRequest, getSupportedLanguage(), ((NLUAPIAI) nluProvider).getParameters());
                if (!commandRequest.isResolved()) {
                    commandRequest.setCC(CC.COMMAND_UNKNOWN);
                    commandRequest.setResolved(false);
                }
            } else {
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "coerce: COMMAND_UNKNOWN");
                }
                commandRequest.setCC(CC.COMMAND_UNKNOWN);
                commandRequest.setResolved(false);
            }
        } else {
            if (DEBUG) {
                MyLog.w(CLS_NAME, "coerce: NLUAPIAI validation failed");
            }
            commandRequest.setCC(CC.COMMAND_UNKNOWN);
            commandRequest.setResolved(false);
        }
    } else if (nluProvider instanceof NLUWit) {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "coerce: instanceof NLUWit");
        }
    // TODO
    } else if (nluProvider instanceof NLUBluemix) {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "coerce: instanceof NLUBluemix");
        }
    // TODO
    } else {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "coerce: instanceof NLUSaiy");
        }
        if (validateNLUSaiy((NLUSaiy) nluProvider)) {
            for (final ai.saiy.android.nlu.saiy.Intent intent : ((NLUSaiy) nluProvider).getIntents()) {
                commandRequest.setCC(NLUConstants.intentToCC(intent.getIntent()));
                if (!commandRequest.getCC().equals(CC.COMMAND_UNKNOWN)) {
                    final NLUSaiyHelper saiyHelper = new NLUSaiyHelper();
                    commandRequest = saiyHelper.prepareCommand(mContext, commandRequest, getSupportedLanguage(), intent.getEntities());
                    if (commandRequest.isResolved()) {
                        break;
                    }
                    break;
                } else {
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "coerce: COMMAND_UNKNOWN");
                    }
                    commandRequest.setCC(CC.COMMAND_UNKNOWN);
                    commandRequest.setResolved(false);
                }
            }
            if (!commandRequest.isResolved()) {
                commandRequest.setCC(CC.COMMAND_UNKNOWN);
                commandRequest.setResolved(false);
            }
        } else {
            if (DEBUG) {
                MyLog.w(CLS_NAME, "coerce: NLUSaiy validation failed");
            }
            commandRequest.setCC(CC.COMMAND_UNKNOWN);
            commandRequest.setResolved(false);
        }
    }
    commandRequest.setResultsArray(getResultsArray());
    new Quantum(mContext).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, commandRequest);
}
Also used : NLUSaiyHelper(ai.saiy.android.nlu.saiy.NLUSaiyHelper) NLUNuanceHelper(ai.saiy.android.nlu.nuance.NLUNuanceHelper) Intent(ai.saiy.android.nlu.microsoft.Intent) NLUMicrosoft(ai.saiy.android.nlu.microsoft.NLUMicrosoft) Interpretation(ai.saiy.android.nlu.nuance.Interpretation) NLUMicrosoftHelper(ai.saiy.android.nlu.microsoft.NLUMicrosoftHelper) NLUNuance(ai.saiy.android.nlu.nuance.NLUNuance) NLUAPIAI(ai.saiy.android.nlu.apiai.NLUAPIAI) Quantum(ai.saiy.android.processing.Quantum) NLUAPIAIHelper(ai.saiy.android.nlu.apiai.NLUAPIAIHelper) NLUBluemix(ai.saiy.android.nlu.bluemix.NLUBluemix) NLUWit(ai.saiy.android.nlu.wit.NLUWit) NLUSaiy(ai.saiy.android.nlu.saiy.NLUSaiy)

Aggregations

Interpretation (ai.saiy.android.nlu.nuance.Interpretation)2 NLUAPIAI (ai.saiy.android.nlu.apiai.NLUAPIAI)1 NLUAPIAIHelper (ai.saiy.android.nlu.apiai.NLUAPIAIHelper)1 NLUBluemix (ai.saiy.android.nlu.bluemix.NLUBluemix)1 Intent (ai.saiy.android.nlu.microsoft.Intent)1 NLUMicrosoft (ai.saiy.android.nlu.microsoft.NLUMicrosoft)1 NLUMicrosoftHelper (ai.saiy.android.nlu.microsoft.NLUMicrosoftHelper)1 Concept (ai.saiy.android.nlu.nuance.Concept)1 NLUNuance (ai.saiy.android.nlu.nuance.NLUNuance)1 NLUNuanceHelper (ai.saiy.android.nlu.nuance.NLUNuanceHelper)1 NLUSaiy (ai.saiy.android.nlu.saiy.NLUSaiy)1 NLUSaiyHelper (ai.saiy.android.nlu.saiy.NLUSaiyHelper)1 NLUWit (ai.saiy.android.nlu.wit.NLUWit)1 Quantum (ai.saiy.android.processing.Quantum)1 UtilsList (ai.saiy.android.utils.UtilsList)1 UtilsMap (ai.saiy.android.utils.UtilsMap)1 UtilsString (ai.saiy.android.utils.UtilsString)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1