Search in sources :

Example 1 with WolframAlphaResponse

use of ai.saiy.android.cognitive.knowledge.provider.wolframalpha.resolve.WolframAlphaResponse in project Saiy-PS by brandall76.

the class CommandWolframAlpha method getResponse.

/**
 * Resolve the required command returning an {@link Outcome} object
 *
 * @param ctx       the application context
 * @param voiceData ArrayList<String> containing the voice data
 * @param sl        the {@link SupportedLanguage} we are using to analyse the voice data.
 * @param cr        the {@link CommandRequest}
 * @return {@link Outcome} containing everything we need to respond to the command.
 */
public Outcome getResponse(@NonNull final Context ctx, @NonNull final ArrayList<String> voiceData, @NonNull final SupportedLanguage sl, @NonNull final CommandRequest cr) {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "voiceData: " + voiceData.size() + " : " + voiceData.toString());
    }
    then = System.nanoTime();
    final Outcome outcome = new Outcome();
    String question;
    if (cr.isResolved()) {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "isResolved: true");
        }
        final CommandWolframAlphaValues cwav = (CommandWolframAlphaValues) cr.getVariableData();
        question = cwav.getQuestion();
    } else {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "isResolved: false");
        }
        question = new CommandWolframAlphaLocal().getResponse(ctx, voiceData, sl);
    }
    if (UtilsString.notNaked(question)) {
        if (DEBUG) {
            MyLog.v(CLS_NAME, "question: " + question);
        }
        final String response;
        if (new WolframAlphaCognitive().validate(question)) {
            if (DEBUG) {
                MyLog.d(CLS_NAME, "validated: true");
            }
            final WolframAlphaRequest request = new WolframAlphaRequest();
            request.setAutoShow(false);
            request.setQuery(question);
            request.setType(WolframAlphaRequest.Type.GENERAL);
            final Pair<Boolean, WolframAlphaResponse> responsePair = new WolframAlphaCognitive().execute(request);
            if (responsePair.first) {
                response = PersonalityResponse.getWolframAlphaIntro(ctx, sl) + ". " + responsePair.second.getInterpretation() + ". " + responsePair.second.getResult();
                outcome.setUtterance(response);
                outcome.setOutcome(Outcome.SUCCESS);
            } else {
                if (DEBUG) {
                    MyLog.w(CLS_NAME, "responsePair: false");
                }
                question = PersonalityResponse.getWolframAlphaError(ctx, sl);
                outcome.setUtterance(question);
                outcome.setOutcome(Outcome.FAILURE);
                return returnOutcome(outcome);
            }
        } else {
            if (DEBUG) {
                MyLog.d(CLS_NAME, "validation: false");
            }
            question = PersonalityResponse.getWolframAlphaError(ctx, sl);
            outcome.setUtterance(question);
            outcome.setOutcome(Outcome.FAILURE);
            return returnOutcome(outcome);
        }
    } else {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "question naked");
        }
        question = PersonalityResponse.getWolframAlphaError(ctx, sl);
        outcome.setUtterance(question);
        outcome.setOutcome(Outcome.FAILURE);
        return returnOutcome(outcome);
    }
    return returnOutcome(outcome);
}
Also used : WolframAlphaCognitive(ai.saiy.android.cognitive.knowledge.provider.wolframalpha.WolframAlphaCognitive) Outcome(ai.saiy.android.processing.Outcome) WolframAlphaResponse(ai.saiy.android.cognitive.knowledge.provider.wolframalpha.resolve.WolframAlphaResponse) WolframAlphaRequest(ai.saiy.android.cognitive.knowledge.provider.wolframalpha.resolve.WolframAlphaRequest) UtilsString(ai.saiy.android.utils.UtilsString)

Aggregations

WolframAlphaCognitive (ai.saiy.android.cognitive.knowledge.provider.wolframalpha.WolframAlphaCognitive)1 WolframAlphaRequest (ai.saiy.android.cognitive.knowledge.provider.wolframalpha.resolve.WolframAlphaRequest)1 WolframAlphaResponse (ai.saiy.android.cognitive.knowledge.provider.wolframalpha.resolve.WolframAlphaResponse)1 Outcome (ai.saiy.android.processing.Outcome)1 UtilsString (ai.saiy.android.utils.UtilsString)1