use of ai.saiy.android.custom.CustomCommandContainer in project Saiy-PS by brandall76.
the class JaroWinklerHelper method executeCustomCommand.
/**
* Method to iterate through the voice data and attempt to match the user's custom commands
* using the {@link JaroWinklerDistance} 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 jwdUpperThreshold = SPH.getJaroWinklerUpper(mContext);
CustomCommand customCommand = null;
final ArrayList<CustomCommandContainer> toKeep = new ArrayList<>();
final JaroWinklerDistance jwd = new JaroWinklerDistance();
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 = jwd.apply(phrase, vd);
if (distance > jwdUpperThreshold) {
container.setUtterance(vd);
container.setScore(distance);
if (distance == Algorithm.JWD_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.JARO_WINKLER);
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "no custom phrases above threshold");
}
}
if (DEBUG) {
MyLog.getElapsed(CLS_NAME, then);
}
return customCommand;
}
use of ai.saiy.android.custom.CustomCommandContainer in project Saiy-PS by brandall76.
the class MetaphoneHelper method executeCustomCommand.
/**
* Method to iterate through the voice data and attempt to match the user's custom commands
* using the {@link Metaphone} 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 Metaphone metaphone = new Metaphone();
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.isMetaphoneEqual(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.setAlgorithm(Algorithm.METAPHONE);
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "no custom phrases matched");
}
}
if (DEBUG) {
MyLog.getElapsed(CLS_NAME, then);
}
return customCommand;
}
use of ai.saiy.android.custom.CustomCommandContainer 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;
}
use of ai.saiy.android.custom.CustomCommandContainer in project Saiy-PS by brandall76.
the class ContainsHelper 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#CONTAINS}
*
* @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, "contains: " + vd + " ~ " + phrase);
}
if (vd.contains(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(CLS_NAME, then);
}
return customCommand;
}
use of ai.saiy.android.custom.CustomCommandContainer in project Saiy-PS by brandall76.
the class CustomHelper 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#CUSTOM}
*
* @return a {@link CustomCommand} should the regular express be successful, otherwise null
*/
public CustomCommand executeCustomCommand() {
long then = System.nanoTime();
CustomCommand customCommand = null;
final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
String phrase;
CustomCommandContainer container;
Pattern pattern;
Matcher matcher;
int size = genericData.size();
outer: for (int i = 0; i < size; i++) {
container = (CustomCommandContainer) genericData.get(i);
phrase = container.getKeyphrase().toLowerCase(loc).trim();
pattern = Pattern.compile(gson.fromJson(container.getSerialised(), CustomCommand.class).getRegularExpression());
for (String vd : inputData) {
vd = vd.toLowerCase(loc).trim();
matcher = pattern.matcher(vd);
if (DEBUG) {
MyLog.i(CLS_NAME, "custom: " + vd + " ~ " + phrase);
}
if (matcher.matches()) {
if (DEBUG) {
MyLog.i(CLS_NAME, "custom: matched " + Pattern.quote(pattern.pattern()));
}
final CustomCommandContainer ccc = SerializationUtils.clone(container);
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(CustomHelper.class.getSimpleName(), then);
}
return customCommand;
}
Aggregations