Search in sources :

Example 6 with Outcome

use of ai.saiy.android.processing.Outcome 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)

Example 7 with Outcome

use of ai.saiy.android.processing.Outcome in project Saiy-PS by brandall76.

the class CommandTasker 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();
    final TaskerHelper taskerHelper = new TaskerHelper();
    final Pair<Boolean, String> taskerPair = taskerHelper.isTaskerInstalled(ctx);
    String taskerPackage;
    if (taskerPair.first) {
        taskerPackage = taskerPair.second;
        if (DEBUG) {
            MyLog.i(CLS_NAME, "tasker installed: " + taskerPackage);
        }
        final Pair<Boolean, Boolean> taskerStatusPair = taskerHelper.canInteract(ctx);
        if (taskerStatusPair.first) {
            if (taskerStatusPair.second) {
                if (taskerHelper.receiverExists(ctx)) {
                    final ArrayList<TaskerTask> taskList = taskerHelper.getTasks(ctx);
                    if (UtilsList.notNaked(taskList)) {
                        if (DEBUG) {
                            MyLog.i(CLS_NAME, "task count: " + taskList.size());
                        }
                        final ArrayList<String> taskNames = new ArrayList<>();
                        String taskName;
                        if (cr.isResolved()) {
                            if (DEBUG) {
                                MyLog.i(CLS_NAME, "isResolved: true");
                            }
                            final CommandTaskerValues cwav = (CommandTaskerValues) cr.getVariableData();
                            taskName = cwav.getTaskName();
                            if (UtilsString.notNaked(taskName)) {
                                taskNames.add(taskName);
                            }
                        } else {
                            if (DEBUG) {
                                MyLog.i(CLS_NAME, "isResolved: false");
                            }
                            taskNames.addAll(new CommandTaskerLocal().getResponse(ctx, voiceData, sl));
                        }
                        if (UtilsList.notNaked(taskNames)) {
                            if (DEBUG) {
                                MyLog.v(CLS_NAME, "taskNames size: " + taskNames.size());
                            }
                            final ArrayList<String> taskNameList = new ArrayList<>(taskList.size());
                            for (final TaskerTask taskerTask : taskList) {
                                taskNameList.add(taskerTask.getTaskName());
                            }
                            final AlgorithmicResolver resolver = new AlgorithmicResolver(ctx, Algorithm.getAlgorithms(ctx, sl), sl.getLocale(), taskNames, taskNameList, AlgorithmicResolver.THREADS_TIMEOUT_500, false);
                            final AlgorithmicContainer container = resolver.resolve();
                            if (container != null) {
                                final boolean exactMatch = container.isExactMatch();
                                if (DEBUG) {
                                    MyLog.d(CLS_NAME, "container exactMatch: " + exactMatch);
                                    MyLog.d(CLS_NAME, "container getInput: " + container.getInput());
                                    MyLog.d(CLS_NAME, "container getGenericMatch: " + container.getGenericMatch());
                                    MyLog.d(CLS_NAME, "container getAlgorithm: " + container.getAlgorithm().name());
                                    MyLog.d(CLS_NAME, "container getScore: " + container.getScore());
                                    MyLog.d(CLS_NAME, "container getParentPosition: " + container.getParentPosition());
                                    MyLog.d(CLS_NAME, "container getVariableData: " + container.getVariableData());
                                }
                                TaskerTask taskerTask = null;
                                try {
                                    taskerTask = taskList.get(container.getParentPosition());
                                } catch (final IndexOutOfBoundsException e) {
                                    if (DEBUG) {
                                        MyLog.w(CLS_NAME, "taskList IndexOutOfBoundsException");
                                        e.printStackTrace();
                                    }
                                }
                                if (taskerTask != null) {
                                    if (DEBUG) {
                                        MyLog.d(CLS_NAME, "taskerTask getProjectName: " + taskerTask.getProjectName());
                                        MyLog.d(CLS_NAME, "taskerTask getTaskName: " + taskerTask.getTaskName());
                                    }
                                    if (taskerHelper.executeTask(ctx, taskerTask.getTaskName())) {
                                        if (SPH.getAnnounceTasker(ctx)) {
                                            outcome.setUtterance(PersonalityResponse.getTaskerTaskExecutedResponse(ctx, sl, taskerTask.getTaskName()));
                                        } else {
                                            if (DEBUG) {
                                                MyLog.w(CLS_NAME, "taskerTask don't announce");
                                            }
                                            outcome.setUtterance(SaiyRequestParams.SILENCE);
                                        }
                                        outcome.setOutcome(Outcome.SUCCESS);
                                    } else {
                                        if (DEBUG) {
                                            MyLog.w(CLS_NAME, "taskerTask failed to execute");
                                        }
                                        outcome.setUtterance(PersonalityResponse.getTaskerTaskFailedResponse(ctx, sl, taskerTask.getTaskName()));
                                        outcome.setOutcome(Outcome.FAILURE);
                                        return returnOutcome(outcome);
                                    }
                                } else {
                                    if (DEBUG) {
                                        MyLog.w(CLS_NAME, "index out of bounds");
                                    }
                                    outcome.setUtterance(PersonalityResponse.getTaskerTaskNotMatchedResponse(ctx, sl));
                                    outcome.setOutcome(Outcome.FAILURE);
                                    return returnOutcome(outcome);
                                }
                            } else {
                                if (DEBUG) {
                                    MyLog.w(CLS_NAME, "failed to find a match");
                                }
                                outcome.setUtterance(PersonalityResponse.getTaskerTaskNotMatchedResponse(ctx, sl));
                                outcome.setOutcome(Outcome.FAILURE);
                                return returnOutcome(outcome);
                            }
                        } else {
                            if (DEBUG) {
                                MyLog.w(CLS_NAME, "empty task name request");
                            }
                            outcome.setUtterance(PersonalityResponse.getTaskerTaskNotMatchedResponse(ctx, sl));
                            outcome.setOutcome(Outcome.FAILURE);
                            return returnOutcome(outcome);
                        }
                    } else {
                        if (DEBUG) {
                            MyLog.w(CLS_NAME, "no tasks");
                        }
                        outcome.setUtterance(PersonalityResponse.getTaskerNoTasksResponse(ctx, sl));
                        outcome.setOutcome(Outcome.FAILURE);
                        return returnOutcome(outcome);
                    }
                } else {
                    if (DEBUG) {
                        MyLog.w(CLS_NAME, "no receiver");
                    }
                    final Bundle bundle = new Bundle();
                    bundle.putInt(ActivityHome.FRAGMENT_INDEX, ActivityHome.INDEX_FRAGMENT_BUGS);
                    ExecuteIntent.saiyActivity(ctx, ActivityHome.class, bundle, true);
                    outcome.setUtterance(PersonalityResponse.getTaskerInstallOrderResponse(ctx, sl));
                    outcome.setOutcome(Outcome.FAILURE);
                    return returnOutcome(outcome);
                }
            } else {
                if (DEBUG) {
                    MyLog.w(CLS_NAME, "remote access error");
                }
                taskerHelper.showTaskerExternalAccess(ctx);
                outcome.setUtterance(PersonalityResponse.getTaskerExternalAccessResponse(ctx, sl));
                outcome.setOutcome(Outcome.FAILURE);
                return returnOutcome(outcome);
            }
        } else {
            if (DEBUG) {
                MyLog.w(CLS_NAME, "tasker disabled");
            }
            UtilsApplication.launchAppFromPackageName(ctx, taskerPackage);
            outcome.setUtterance(PersonalityResponse.getTaskerDisabledResponse(ctx, sl));
            outcome.setOutcome(Outcome.FAILURE);
            return returnOutcome(outcome);
        }
    } else {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "tasker not installed");
        }
        Install.showInstallLink(ctx, Installed.PACKAGE_TASKER_MARKET);
        outcome.setUtterance(PersonalityResponse.getTaskerInstallResponse(ctx, sl));
        outcome.setOutcome(Outcome.FAILURE);
        return returnOutcome(outcome);
    }
    return returnOutcome(outcome);
}
Also used : TaskerHelper(ai.saiy.android.thirdparty.tasker.TaskerHelper) Bundle(android.os.Bundle) TaskerTask(ai.saiy.android.thirdparty.tasker.TaskerTask) ArrayList(java.util.ArrayList) UtilsString(ai.saiy.android.utils.UtilsString) AlgorithmicContainer(ai.saiy.android.nlu.local.AlgorithmicContainer) Outcome(ai.saiy.android.processing.Outcome) AlgorithmicResolver(ai.saiy.android.nlu.local.AlgorithmicResolver)

Aggregations

Outcome (ai.saiy.android.processing.Outcome)7 UtilsString (ai.saiy.android.utils.UtilsString)5 Bundle (android.os.Bundle)2 Speaker (ai.saiy.android.cognitive.identity.provider.microsoft.Speaker)1 ProfileItem (ai.saiy.android.cognitive.identity.provider.microsoft.containers.ProfileItem)1 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 ExecuteIntent (ai.saiy.android.intent.ExecuteIntent)1 AlgorithmicContainer (ai.saiy.android.nlu.local.AlgorithmicContainer)1 AlgorithmicResolver (ai.saiy.android.nlu.local.AlgorithmicResolver)1 Qubit (ai.saiy.android.processing.Qubit)1 TaskerHelper (ai.saiy.android.thirdparty.tasker.TaskerHelper)1 TaskerTask (ai.saiy.android.thirdparty.tasker.TaskerTask)1 SaiyAccount (ai.saiy.android.user.SaiyAccount)1 SaiyAccountList (ai.saiy.android.user.SaiyAccountList)1 Intent (android.content.Intent)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1