Search in sources :

Example 6 with JaroWinklerDistance

use of ai.saiy.android.algorithms.distance.jarowinkler.JaroWinklerDistance 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 JaroWinklerDistance

use of ai.saiy.android.algorithms.distance.jarowinkler.JaroWinklerDistance 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 8 with JaroWinklerDistance

use of ai.saiy.android.algorithms.distance.jarowinkler.JaroWinklerDistance in project Saiy-PS by brandall76.

the class SoundexHelper method executeCustomCommand.

/**
 * Method to iterate through the voice data and attempt to match the user's custom commands
 * using the {@link Soundex} 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 jwdLowerThreshold = SPH.getJaroWinklerLower(mContext);
    final double soundexUpperThreshold = SPH.getSoundexUpper(mContext);
    CustomCommand customCommand = null;
    final ArrayList<CustomCommandContainer> toKeep = new ArrayList<>();
    final Soundex soundex = new Soundex();
    final JaroWinklerDistance jwd = new JaroWinklerDistance();
    String phrase;
    CustomCommandContainer container;
    double score;
    double distance;
    int size = genericData.size();
    try {
        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 = soundex.difference(phrase, vd);
                if (distance > soundexUpperThreshold && Algorithm.checkLength(phrase, vd)) {
                    score = jwd.apply(phrase, vd);
                    if (score > jwdLowerThreshold) {
                        container.setUtterance(vd);
                        container.setScore(score);
                        if (distance == Algorithm.SOUNDEX_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));
                        }
                    } 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) {
            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.SOUNDEX);
    } else {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "no custom phrases above threshold");
        }
    }
    if (DEBUG) {
        MyLog.getElapsed(CLS_NAME, 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) JaroWinklerDistance(ai.saiy.android.algorithms.distance.jarowinkler.JaroWinklerDistance) Soundex(org.apache.commons.codec.language.Soundex) EncoderException(org.apache.commons.codec.EncoderException) CustomCommand(ai.saiy.android.custom.CustomCommand)

Aggregations

JaroWinklerDistance (ai.saiy.android.algorithms.distance.jarowinkler.JaroWinklerDistance)8 ArrayList (java.util.ArrayList)8 CustomCommand (ai.saiy.android.custom.CustomCommand)4 CustomCommandContainer (ai.saiy.android.custom.CustomCommandContainer)4 AlgorithmicContainer (ai.saiy.android.nlu.local.AlgorithmicContainer)4 Gson (com.google.gson.Gson)4 GsonBuilder (com.google.gson.GsonBuilder)4 EncoderException (org.apache.commons.codec.EncoderException)2 DoubleMetaphone (org.apache.commons.codec.language.DoubleMetaphone)2 Metaphone (org.apache.commons.codec.language.Metaphone)2 Soundex (org.apache.commons.codec.language.Soundex)2