use of ai.saiy.android.algorithms.distance.jarowinkler.JaroWinklerDistance 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;
}
use of ai.saiy.android.algorithms.distance.jarowinkler.JaroWinklerDistance 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;
}
use of ai.saiy.android.algorithms.distance.jarowinkler.JaroWinklerDistance 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.algorithms.distance.jarowinkler.JaroWinklerDistance 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;
}
use of ai.saiy.android.algorithms.distance.jarowinkler.JaroWinklerDistance in project Saiy-PS by brandall76.
the class FuzzyHelper method executeCustomCommand.
/**
* Method to iterate through the voice data and attempt to match the user's custom commands
* using the {@link StringUtils#getFuzzyDistance(CharSequence, CharSequence, Locale)}
* 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 fuzzyMultiplier = SPH.getFuzzyMultiplier(mContext);
CustomCommand customCommand = null;
final ArrayList<CustomCommandContainer> toKeep = new ArrayList<>();
final JaroWinklerDistance jwd = new JaroWinklerDistance();
String phrase;
CustomCommandContainer container;
double score;
double distance;
int size = genericData.size();
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 = StringUtils.getFuzzyDistance(phrase, vd, loc);
if (distance > (vd.length() * fuzzyMultiplier)) {
if (DEBUG) {
MyLog.i(CLS_NAME, "Potential " + phrase);
}
score = jwd.apply(phrase, vd);
if (DEBUG) {
MyLog.i(CLS_NAME, "Potential: double check JW " + phrase + " ~ " + vd + " " + score);
}
if (score > jwdLowerThreshold) {
if (DEBUG) {
MyLog.i(CLS_NAME, "Potential: double check JW: accepted");
}
container.setScore(score);
container.setUtterance(vd);
toKeep.add(SerializationUtils.clone(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() + " 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.FUZZY);
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "no custom phrases above threshold");
}
}
if (DEBUG) {
MyLog.getElapsed(FuzzyHelper.class.getSimpleName(), then);
}
return customCommand;
}
Aggregations