Search in sources :

Example 6 with CustomCommandContainer

use of ai.saiy.android.custom.CustomCommandContainer in project Saiy-PS by brandall76.

the class EndsWithHelper method executeCustomCommand.

/**
 * Method to iterate through the voice data and attempt to match the user's custom commands
 * using that are marked as {@link Algorithm#REGEX}
 * specifically {@link ai.saiy.android.api.request.Regex#ENDS_WITH}
 *
 * @return a {@link CustomCommand} should the regular express be successful, otherwise null
 */
public CustomCommand executeCustomCommand() {
    long then = System.nanoTime();
    CustomCommand customCommand = null;
    String phrase;
    CustomCommandContainer container;
    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();
            if (DEBUG) {
                MyLog.i(CLS_NAME, "ends with: " + vd + " ~ " + phrase);
            }
            if (vd.endsWith(phrase)) {
                final CustomCommandContainer ccc = SerializationUtils.clone(container);
                final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
                customCommand = gson.fromJson(ccc.getSerialised(), CustomCommand.class);
                customCommand.setExactMatch(true);
                customCommand.setUtterance(vd);
                customCommand.setScore(1.0);
                customCommand.setAlgorithm(Algorithm.REGEX);
                break outer;
            }
        }
    }
    if (DEBUG) {
        MyLog.getElapsed(EndsWithHelper.class.getSimpleName(), then);
    }
    return customCommand;
}
Also used : CustomCommandContainer(ai.saiy.android.custom.CustomCommandContainer) CustomCommand(ai.saiy.android.custom.CustomCommand) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson)

Example 7 with CustomCommandContainer

use of ai.saiy.android.custom.CustomCommandContainer in project Saiy-PS by brandall76.

the class StartsWithHelper method executeCustomCommand.

/**
 * Method to iterate through the voice data and attempt to match the user's custom commands
 * using that are marked as {@link Algorithm#REGEX}
 * specifically {@link ai.saiy.android.api.request.Regex#STARTS_WITH}
 *
 * @return a {@link CustomCommand} should the regular express be successful, otherwise null
 */
public CustomCommand executeCustomCommand() {
    long then = System.nanoTime();
    CustomCommand customCommand = null;
    String phrase;
    CustomCommandContainer container;
    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();
            if (DEBUG) {
                MyLog.i(CLS_NAME, "starts with: " + vd + " ~ " + phrase);
            }
            if (vd.startsWith(phrase)) {
                final CustomCommandContainer ccc = SerializationUtils.clone(container);
                final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
                customCommand = gson.fromJson(ccc.getSerialised(), CustomCommand.class);
                customCommand.setExactMatch(true);
                customCommand.setUtterance(vd);
                customCommand.setScore(1.0);
                customCommand.setAlgorithm(Algorithm.REGEX);
                break outer;
            }
        }
    }
    if (DEBUG) {
        MyLog.getElapsed(StartsWithHelper.class.getSimpleName(), then);
    }
    return customCommand;
}
Also used : CustomCommandContainer(ai.saiy.android.custom.CustomCommandContainer) CustomCommand(ai.saiy.android.custom.CustomCommand) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson)

Example 8 with CustomCommandContainer

use of ai.saiy.android.custom.CustomCommandContainer in project Saiy-PS by brandall76.

the class DBCustomCommand method getKeyphrases.

/**
 * Get all keyphrases from the database, including the corresponding row identifier and
 * serialised command data.
 *
 * @return the {@link Pair} keyphrase and row identifier
 */
public ArrayList<CustomCommandContainer> getKeyphrases() {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "getKeyphrases");
    }
    final ArrayList<CustomCommandContainer> keyPhrases = new ArrayList<>();
    try {
        open();
        if (database.isOpen()) {
            final Cursor cursor = database.query(TABLE_CUSTOM_COMMANDS, ALL_COLUMNS, null, null, null, null, null);
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                keyPhrases.add(new CustomCommandContainer(cursor.getLong(0), cursor.getString(1), cursor.getString(2), cursor.getString(3)));
                cursor.moveToNext();
            }
            cursor.close();
        }
    } catch (final IllegalStateException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getKeyphrases: IllegalStateException");
            e.printStackTrace();
        }
    } catch (final SQLException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getKeyphrases: SQLException");
            e.printStackTrace();
        }
    } catch (final Exception e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getKeyphrases: Exception");
            e.printStackTrace();
        }
    } finally {
        try {
            if (database.isOpen()) {
                close();
            }
        } catch (final IllegalStateException e) {
            if (DEBUG) {
                MyLog.w(CLS_NAME, "getKeyphrases: IllegalStateException");
                e.printStackTrace();
            }
        } catch (final SQLException e) {
            if (DEBUG) {
                MyLog.w(CLS_NAME, "getKeyphrases: SQLException");
                e.printStackTrace();
            }
        } catch (final Exception e) {
            if (DEBUG) {
                MyLog.w(CLS_NAME, "getKeyphrases: Exception");
                e.printStackTrace();
            }
        }
    }
    return keyPhrases;
}
Also used : CustomCommandContainer(ai.saiy.android.custom.CustomCommandContainer) SQLException(android.database.SQLException) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) SQLException(android.database.SQLException)

Example 9 with CustomCommandContainer

use of ai.saiy.android.custom.CustomCommandContainer in project Saiy-PS by brandall76.

the class LevenshteinHelper method executeCustomCommand.

/**
 * Method to iterate through the voice data and attempt to match the user's custom commands
 * using the {@link LevenshteinDistance} 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 levUpperThreshold = SPH.getLevenshteinUpper(mContext);
    CustomCommand customCommand = null;
    final ArrayList<CustomCommandContainer> toKeep = new ArrayList<>();
    final LevenshteinDistance ld = new LevenshteinDistance();
    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 = ld.apply(phrase, vd);
            if (distance <= levUpperThreshold) {
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "Keeping " + phrase);
                }
                container.setUtterance(vd);
                container.setScore(distance);
                if (distance == Algorithm.LEV_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(c1.getScore(), c2.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.LEVENSHTEIN);
    } else {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "no custom phrases above threshold");
        }
    }
    if (DEBUG) {
        MyLog.getElapsed(LevenshteinHelper.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) CustomCommand(ai.saiy.android.custom.CustomCommand)

Example 10 with CustomCommandContainer

use of ai.saiy.android.custom.CustomCommandContainer in project Saiy-PS by brandall76.

the class DoubleMetaphoneHelper method executeCustomCommand.

/**
 * Method to iterate through the voice data and attempt to match the user's custom commands
 * using the {@link DoubleMetaphone} 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);
    CustomCommand customCommand = null;
    final ArrayList<CustomCommandContainer> toKeep = new ArrayList<>();
    final DoubleMetaphone metaphone = new DoubleMetaphone();
    final JaroWinklerDistance jwd = new JaroWinklerDistance();
    String phrase;
    CustomCommandContainer container;
    double score;
    boolean matches;
    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();
            matches = metaphone.isDoubleMetaphoneEqual(phrase, vd);
            if (matches && Algorithm.checkLength(phrase, vd)) {
                score = jwd.apply(phrase, vd);
                if (score > jwdLowerThreshold) {
                    container.setScore(score);
                    container.setUtterance(vd);
                    container.setExactMatch(true);
                    toKeep.add(SerializationUtils.clone(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");
        }
        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.DOUBLE_METAPHONE);
    } else {
        if (DEBUG) {
            MyLog.i(CLS_NAME, "no custom phrases matched");
        }
    }
    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) CustomCommand(ai.saiy.android.custom.CustomCommand) DoubleMetaphone(org.apache.commons.codec.language.DoubleMetaphone)

Aggregations

CustomCommandContainer (ai.saiy.android.custom.CustomCommandContainer)13 CustomCommand (ai.saiy.android.custom.CustomCommand)12 Gson (com.google.gson.Gson)12 GsonBuilder (com.google.gson.GsonBuilder)12 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 Cursor (android.database.Cursor)1 SQLException (android.database.SQLException)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)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