Search in sources :

Example 6 with AlgorithmicContainer

use of ai.saiy.android.nlu.local.AlgorithmicContainer in project Saiy-PS by brandall76.

the class FuzzyHelper method executeGeneric.

/**
 * Method to iterate through the given input data and attempt to match the given String data
 * using the {@link StringUtils#getFuzzyDistance(CharSequence, CharSequence, Locale)}
 * within ranges applied by the associated thresholds constants.
 *
 * @return an {@link AlgorithmicContainer} or null if thresholds aren't satisfied
 */
public AlgorithmicContainer executeGeneric() {
    long then = System.nanoTime();
    final double jwdLowerThreshold = SPH.getJaroWinklerLower(mContext);
    final double fuzzyMultiplier = SPH.getFuzzyMultiplier(mContext);
    final ArrayList<AlgorithmicContainer> toKeep = new ArrayList<>();
    final JaroWinklerDistance jwd = new JaroWinklerDistance();
    String generic;
    String genericLower;
    AlgorithmicContainer container = null;
    double distance;
    double score;
    int size = genericData.size();
    for (int i = 0; i < size; i++) {
        generic = (String) genericData.get(i);
        genericLower = generic.toLowerCase(loc).trim();
        for (String vd : inputData) {
            vd = vd.toLowerCase(loc).trim();
            distance = StringUtils.getFuzzyDistance(genericLower, vd, loc);
            if (distance > (vd.length() * fuzzyMultiplier)) {
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "Potential " + genericLower);
                }
                score = jwd.apply(genericLower, vd);
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "Potential: double check JW " + genericLower + " ~ " + vd + " " + score);
                }
                if (score > jwdLowerThreshold) {
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "Potential: double check JW: accepted");
                    }
                    container = new AlgorithmicContainer();
                    container.setInput(vd);
                    container.setGenericMatch(generic);
                    container.setScore(score);
                    container.setAlgorithm(Algorithm.FUZZY);
                    container.setParentPosition(i);
                    toKeep.add(container);
                } else {
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "Matches: double check JW: rejected");
                    }
                }
            }
        }
    }
    if (UtilsList.notNaked(toKeep)) {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "Have " + toKeep.size() + " input matches");
            for (final AlgorithmicContainer c : toKeep) {
                MyLog.i(CLS_NAME, "before order: " + c.getGenericMatch() + " ~ " + c.getScore());
            }
        }
        Collections.sort(toKeep, new Comparator<AlgorithmicContainer>() {

            @Override
            public int compare(final AlgorithmicContainer c1, final AlgorithmicContainer c2) {
                return Double.compare(c2.getScore(), c1.getScore());
            }
        });
        if (DEBUG) {
            for (final AlgorithmicContainer c : toKeep) {
                MyLog.i(CLS_NAME, "after order: " + c.getGenericMatch() + " ~ " + c.getScore());
            }
            MyLog.i(CLS_NAME, "would select: " + toKeep.get(0).getGenericMatch());
        }
        container = toKeep.get(0);
    } else {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "no matches above threshold");
        }
    }
    if (DEBUG) {
        MyLog.getElapsed(FuzzyHelper.class.getSimpleName(), then);
    }
    return container;
}
Also used : JaroWinklerDistance(ai.saiy.android.algorithms.distance.jarowinkler.JaroWinklerDistance) AlgorithmicContainer(ai.saiy.android.nlu.local.AlgorithmicContainer) ArrayList(java.util.ArrayList)

Example 7 with AlgorithmicContainer

use of ai.saiy.android.nlu.local.AlgorithmicContainer in project Saiy-PS by brandall76.

the class NeedlemanWunschHelper method executeGeneric.

/**
 * Method to iterate through the given input data and attempt to match the given String data
 * using the {@link NeedlemanWunch} within ranges applied by the associated thresholds constants.
 *
 * @return an {@link AlgorithmicContainer} or null if thresholds aren't satisfied
 */
public AlgorithmicContainer executeGeneric() {
    long then = System.nanoTime();
    final double nwUpperThreshold = SPH.getNeedlemanWunschUpper(mContext);
    final ArrayList<AlgorithmicContainer> toKeep = new ArrayList<>();
    final StringMetric nw = new NeedlemanWunch();
    String generic;
    String genericLower;
    AlgorithmicContainer container = null;
    double distance;
    int size = genericData.size();
    outer: for (int i = 0; i < size; i++) {
        generic = (String) genericData.get(i);
        genericLower = generic.toLowerCase(loc).trim();
        for (String vd : inputData) {
            vd = vd.toLowerCase(loc).trim();
            distance = nw.compare(genericLower, vd);
            if (distance > nwUpperThreshold) {
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "Keeping " + genericLower);
                }
                container = new AlgorithmicContainer();
                container.setInput(vd);
                container.setGenericMatch(generic);
                container.setScore(distance);
                container.setAlgorithm(Algorithm.NEEDLEMAN_WUNCH);
                container.setParentPosition(i);
                if (distance == Algorithm.NW_MAX_THRESHOLD) {
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "Exact match " + genericLower);
                    }
                    container.setExactMatch(true);
                    toKeep.add(container);
                    break outer;
                } else {
                    container.setExactMatch(false);
                    toKeep.add(container);
                }
            }
        }
    }
    if (UtilsList.notNaked(toKeep)) {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "Have " + toKeep.size() + " input matches");
            for (final AlgorithmicContainer c : toKeep) {
                MyLog.i(CLS_NAME, "before order: " + c.getGenericMatch() + " ~ " + c.getScore());
            }
        }
        Collections.sort(toKeep, new Comparator<AlgorithmicContainer>() {

            @Override
            public int compare(final AlgorithmicContainer c1, final AlgorithmicContainer c2) {
                return Double.compare(c2.getScore(), c1.getScore());
            }
        });
        if (DEBUG) {
            for (final AlgorithmicContainer c : toKeep) {
                MyLog.i(CLS_NAME, "after order: " + c.getGenericMatch() + " ~ " + c.getScore());
            }
            MyLog.i(CLS_NAME, "would select: " + toKeep.get(0).getGenericMatch());
        }
        container = toKeep.get(0);
    } else {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "no matches above threshold");
        }
    }
    if (DEBUG) {
        MyLog.getElapsed(NeedlemanWunschHelper.class.getSimpleName(), then);
    }
    return container;
}
Also used : AlgorithmicContainer(ai.saiy.android.nlu.local.AlgorithmicContainer) NeedlemanWunch(ai.saiy.android.algorithms.needlemanwunch.simmetrics.NeedlemanWunch) ArrayList(java.util.ArrayList) StringMetric(org.simmetrics.StringMetric)

Example 8 with AlgorithmicContainer

use of ai.saiy.android.nlu.local.AlgorithmicContainer in project Saiy-PS by brandall76.

the class SoundexHelper method executeGeneric.

/**
 * Method to iterate through the given input data and attempt to match the given String data
 * using the {@link Soundex} within ranges applied by the associated thresholds constants.
 *
 * @return an {@link AlgorithmicContainer} or null if thresholds aren't satisfied
 */
private AlgorithmicContainer executeGeneric() {
    long then = System.nanoTime();
    final double jwdLowerThreshold = SPH.getJaroWinklerLower(mContext);
    final double soundexUpperThreshold = SPH.getSoundexUpper(mContext);
    final ArrayList<AlgorithmicContainer> toKeep = new ArrayList<>();
    final Soundex soundex = new Soundex();
    final JaroWinklerDistance jwd = new JaroWinklerDistance();
    String generic;
    String genericLower;
    AlgorithmicContainer container = null;
    double distance;
    double score;
    int size = genericData.size();
    try {
        outer: for (int i = 0; i < size; i++) {
            generic = (String) genericData.get(i);
            genericLower = generic.toLowerCase(loc).trim();
            for (String vd : inputData) {
                vd = vd.toLowerCase(loc).trim();
                distance = soundex.difference(genericLower, vd);
                if (distance > soundexUpperThreshold && Algorithm.checkLength(genericLower, vd)) {
                    score = jwd.apply(genericLower, vd);
                    if (score > jwdLowerThreshold) {
                        container = new AlgorithmicContainer();
                        container.setInput(vd);
                        container.setGenericMatch(generic);
                        container.setScore(score);
                        container.setAlgorithm(Algorithm.SOUNDEX);
                        container.setParentPosition(i);
                        if (distance == Algorithm.SOUNDEX_MAX_THRESHOLD) {
                            if (DEBUG) {
                                MyLog.i(CLS_NAME, "Exact match " + genericLower);
                            }
                            container.setExactMatch(true);
                            toKeep.add(container);
                            break outer;
                        } else {
                            container.setExactMatch(false);
                            toKeep.add(container);
                        }
                    } else {
                        if (DEBUG) {
                            MyLog.i(CLS_NAME, "Possible match: double check JW: rejected");
                        }
                    }
                }
            }
        }
    } catch (final EncoderException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "EncoderException");
            e.printStackTrace();
        }
    }
    if (UtilsList.notNaked(toKeep)) {
        if (DEBUG) {
            debugBefore(toKeep);
        }
        Collections.sort(toKeep, new Comparator<AlgorithmicContainer>() {

            @Override
            public int compare(final AlgorithmicContainer c1, final AlgorithmicContainer c2) {
                return Double.compare(c2.getScore(), c1.getScore());
            }
        });
        if (DEBUG) {
            debugAfter(toKeep);
        }
        container = toKeep.get(0);
    } else {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "no matches above threshold");
        }
    }
    if (DEBUG) {
        MyLog.getElapsed(CLS_NAME, then);
    }
    return container;
}
Also used : JaroWinklerDistance(ai.saiy.android.algorithms.distance.jarowinkler.JaroWinklerDistance) Soundex(org.apache.commons.codec.language.Soundex) EncoderException(org.apache.commons.codec.EncoderException) AlgorithmicContainer(ai.saiy.android.nlu.local.AlgorithmicContainer) ArrayList(java.util.ArrayList)

Example 9 with AlgorithmicContainer

use of ai.saiy.android.nlu.local.AlgorithmicContainer 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

AlgorithmicContainer (ai.saiy.android.nlu.local.AlgorithmicContainer)9 ArrayList (java.util.ArrayList)9 JaroWinklerDistance (ai.saiy.android.algorithms.distance.jarowinkler.JaroWinklerDistance)4 StringMetric (org.simmetrics.StringMetric)2 NeedlemanWunch (ai.saiy.android.algorithms.needlemanwunch.simmetrics.NeedlemanWunch)1 AlgorithmicResolver (ai.saiy.android.nlu.local.AlgorithmicResolver)1 Outcome (ai.saiy.android.processing.Outcome)1 TaskerHelper (ai.saiy.android.thirdparty.tasker.TaskerHelper)1 TaskerTask (ai.saiy.android.thirdparty.tasker.TaskerTask)1 UtilsString (ai.saiy.android.utils.UtilsString)1 Bundle (android.os.Bundle)1 EncoderException (org.apache.commons.codec.EncoderException)1 DoubleMetaphone (org.apache.commons.codec.language.DoubleMetaphone)1 Metaphone (org.apache.commons.codec.language.Metaphone)1 Soundex (org.apache.commons.codec.language.Soundex)1 MongeElkan (org.simmetrics.metrics.MongeElkan)1 SmithWatermanGotoh (org.simmetrics.metrics.SmithWatermanGotoh)1