Search in sources :

Example 1 with AlgorithmicContainer

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

the class JaroWinklerHelper method executeGeneric.

/**
 * Method to iterate through the given input data and attempt to match the given String data
 * using the {@link JaroWinklerDistance} 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 jwdUpperThreshold = SPH.getJaroWinklerUpper(mContext);
    final ArrayList<AlgorithmicContainer> toKeep = new ArrayList<>();
    final JaroWinklerDistance jwd = new JaroWinklerDistance();
    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 = jwd.apply(genericLower, vd);
            if (distance > jwdUpperThreshold) {
                container = new AlgorithmicContainer();
                container.setInput(vd);
                container.setGenericMatch(generic);
                container.setScore(distance);
                container.setAlgorithm(Algorithm.JARO_WINKLER);
                container.setParentPosition(i);
                if (distance == Algorithm.JWD_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(CLS_NAME, then);
    }
    return container;
}
Also used : AlgorithmicContainer(ai.saiy.android.nlu.local.AlgorithmicContainer) ArrayList(java.util.ArrayList)

Example 2 with AlgorithmicContainer

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

the class LevenshteinHelper method executeGeneric.

/**
 * Method to iterate through the given input data and attempt to match the given String data
 * using the {@link LevenshteinDistance} 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 levUpperThreshold = SPH.getLevenshteinUpper(mContext);
    final ArrayList<AlgorithmicContainer> toKeep = new ArrayList<>();
    final LevenshteinDistance ld = new LevenshteinDistance();
    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 = ld.apply(genericLower, vd);
            if (distance <= levUpperThreshold) {
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "Keeping " + genericLower);
                }
                container = new AlgorithmicContainer();
                container.setInput(vd);
                container.setGenericMatch(generic);
                container.setScore(distance);
                container.setAlgorithm(Algorithm.LEVENSHTEIN);
                container.setParentPosition(i);
                if (distance == Algorithm.LEV_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(c1.getScore(), c2.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(LevenshteinHelper.class.getSimpleName(), then);
    }
    return container;
}
Also used : AlgorithmicContainer(ai.saiy.android.nlu.local.AlgorithmicContainer) ArrayList(java.util.ArrayList)

Example 3 with AlgorithmicContainer

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

the class DoubleMetaphoneHelper method executeGeneric.

/**
 * Method to iterate through the given input data and attempt to match the given String data
 * using the {@link DoubleMetaphone} 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 ArrayList<AlgorithmicContainer> toKeep = new ArrayList<>();
    final DoubleMetaphone metaphone = new DoubleMetaphone();
    final JaroWinklerDistance jwd = new JaroWinklerDistance();
    String generic;
    String genericLower;
    AlgorithmicContainer container = null;
    double score;
    boolean matches;
    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();
            matches = metaphone.isDoubleMetaphoneEqual(genericLower, vd);
            if (matches && 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.DOUBLE_METAPHONE);
                    container.setParentPosition(i);
                    container.setExactMatch(true);
                    toKeep.add(container);
                    break outer;
                } else {
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "Matches: double check JW: rejected");
                    }
                }
            }
        }
    }
    if (UtilsList.notNaked(toKeep)) {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "Have a match");
        }
        container = toKeep.get(0);
    } else {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "no matches");
        }
    }
    if (DEBUG) {
        MyLog.getElapsed(CLS_NAME, then);
    }
    return container;
}
Also used : JaroWinklerDistance(ai.saiy.android.algorithms.distance.jarowinkler.JaroWinklerDistance) AlgorithmicContainer(ai.saiy.android.nlu.local.AlgorithmicContainer) DoubleMetaphone(org.apache.commons.codec.language.DoubleMetaphone) ArrayList(java.util.ArrayList)

Example 4 with AlgorithmicContainer

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

the class MetaphoneHelper method executeGeneric.

/**
 * Method to iterate through the given input data and attempt to match the given String data
 * using the {@link Metaphone} 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 ArrayList<AlgorithmicContainer> toKeep = new ArrayList<>();
    final Metaphone metaphone = new Metaphone();
    final JaroWinklerDistance jwd = new JaroWinklerDistance();
    String generic;
    String genericLower;
    AlgorithmicContainer container = null;
    double score;
    boolean matches;
    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();
            matches = metaphone.isMetaphoneEqual(genericLower, vd);
            if (matches && 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.METAPHONE);
                    container.setParentPosition(i);
                    container.setExactMatch(true);
                    toKeep.add(container);
                    break outer;
                } else {
                    if (DEBUG) {
                        MyLog.i(CLS_NAME, "Matches: double check JW: rejected");
                    }
                }
            }
        }
    }
    if (UtilsList.notNaked(toKeep)) {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "Have a match");
        }
        container = toKeep.get(0);
    } else {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "no matches");
        }
    }
    if (DEBUG) {
        MyLog.getElapsed(CLS_NAME, then);
    }
    return container;
}
Also used : JaroWinklerDistance(ai.saiy.android.algorithms.distance.jarowinkler.JaroWinklerDistance) AlgorithmicContainer(ai.saiy.android.nlu.local.AlgorithmicContainer) ArrayList(java.util.ArrayList) Metaphone(org.apache.commons.codec.language.Metaphone)

Example 5 with AlgorithmicContainer

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

the class MongeElkanHelper method executeGeneric.

/**
 * Method to iterate through the given input data and attempt to match the given String data
 * using the {@link MongeElkan} 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 meUpperThreshold = SPH.getMongeElkanUpper(mContext);
    final ArrayList<AlgorithmicContainer> toKeep = new ArrayList<>();
    final StringMetric me = with(new MongeElkan(new SmithWatermanGotoh())).tokenize(whitespace()).build();
    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 = me.compare(genericLower, vd);
            if (distance > meUpperThreshold) {
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "Keeping " + genericLower);
                }
                container = new AlgorithmicContainer();
                container.setInput(vd);
                container.setGenericMatch(generic);
                container.setScore(distance);
                container.setAlgorithm(Algorithm.MONGE_ELKAN);
                container.setParentPosition(i);
                if (distance == Algorithm.ME_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(MongeElkanHelper.class.getSimpleName(), then);
    }
    return container;
}
Also used : ArrayList(java.util.ArrayList) StringMetric(org.simmetrics.StringMetric) SmithWatermanGotoh(org.simmetrics.metrics.SmithWatermanGotoh) AlgorithmicContainer(ai.saiy.android.nlu.local.AlgorithmicContainer) MongeElkan(org.simmetrics.metrics.MongeElkan)

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