Search in sources :

Example 1 with NeedlemanWunch

use of ai.saiy.android.algorithms.needlemanwunch.simmetrics.NeedlemanWunch in project Saiy-PS by brandall76.

the class NeedlemanWunschHelper method executeCustomCommand.

/**
 * Method to iterate through the voice data and attempt to match the user's custom commands
 * using the {@link NeedlemanWunch} within ranges applied by the associated thresholds constants.
 *
 * @return the highest scoring {@link CustomCommand} or null if thresholds aren't satisfied
 */
public CustomCommand executeCustomCommand() {
    long then = System.nanoTime();
    final double nwUpperThreshold = SPH.getNeedlemanWunschUpper(mContext);
    CustomCommand customCommand = null;
    final ArrayList<CustomCommandContainer> toKeep = new ArrayList<>();
    final StringMetric nw = new NeedlemanWunch();
    String phrase;
    CustomCommandContainer container;
    double distance;
    int size = genericData.size();
    outer: for (int i = 0; i < size; i++) {
        container = (CustomCommandContainer) genericData.get(i);
        phrase = container.getKeyphrase().toLowerCase(loc).trim();
        for (String vd : inputData) {
            vd = vd.toLowerCase(loc).trim();
            distance = nw.compare(phrase, vd);
            if (distance > nwUpperThreshold) {
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "Keeping " + phrase);
                }
                container.setUtterance(vd);
                container.setScore(distance);
                if (distance == Algorithm.NW_MAX_THRESHOLD) {
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "Exact match " + phrase);
                    }
                    container.setExactMatch(true);
                    toKeep.add(SerializationUtils.clone(container));
                    break outer;
                } else {
                    toKeep.add(SerializationUtils.clone(container));
                }
            }
        }
    }
    if (UtilsList.notNaked(toKeep)) {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "Have " + toKeep.size() + " phrase matches");
            for (final CustomCommandContainer c : toKeep) {
                MyLog.i(CLS_NAME, "before order: " + c.getKeyphrase() + " ~ " + c.getScore());
            }
        }
        Collections.sort(toKeep, new Comparator<CustomCommandContainer>() {

            @Override
            public int compare(final CustomCommandContainer c1, final CustomCommandContainer c2) {
                return Double.compare(c2.getScore(), c1.getScore());
            }
        });
        if (DEBUG) {
            for (final CustomCommandContainer c : toKeep) {
                MyLog.i(CLS_NAME, "after order: " + c.getKeyphrase() + " ~ " + c.getScore());
            }
            MyLog.i(CLS_NAME, "would select: " + toKeep.get(0).getKeyphrase());
        }
        final CustomCommandContainer ccc = toKeep.get(0);
        final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        customCommand = gson.fromJson(ccc.getSerialised(), CustomCommand.class);
        customCommand.setExactMatch(ccc.isExactMatch());
        customCommand.setUtterance(ccc.getUtterance());
        customCommand.setScore(ccc.getScore());
        customCommand.setAlgorithm(Algorithm.NEEDLEMAN_WUNCH);
    } else {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "no custom phrases above threshold");
        }
    }
    if (DEBUG) {
        MyLog.getElapsed(NeedlemanWunschHelper.class.getSimpleName(), then);
    }
    return customCommand;
}
Also used : CustomCommandContainer(ai.saiy.android.custom.CustomCommandContainer) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) StringMetric(org.simmetrics.StringMetric) CustomCommand(ai.saiy.android.custom.CustomCommand) NeedlemanWunch(ai.saiy.android.algorithms.needlemanwunch.simmetrics.NeedlemanWunch)

Example 2 with NeedlemanWunch

use of ai.saiy.android.algorithms.needlemanwunch.simmetrics.NeedlemanWunch 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)

Aggregations

NeedlemanWunch (ai.saiy.android.algorithms.needlemanwunch.simmetrics.NeedlemanWunch)2 ArrayList (java.util.ArrayList)2 StringMetric (org.simmetrics.StringMetric)2 CustomCommand (ai.saiy.android.custom.CustomCommand)1 CustomCommandContainer (ai.saiy.android.custom.CustomCommandContainer)1 AlgorithmicContainer (ai.saiy.android.nlu.local.AlgorithmicContainer)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1