use of org.simmetrics.metrics.SmithWatermanGotoh 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;
}
use of org.simmetrics.metrics.SmithWatermanGotoh in project Saiy-PS by brandall76.
the class MongeElkanHelper method executeCustomCommand.
/**
* Method to iterate through the voice data and attempt to match the user's custom commands
* using the {@link MongeElkan} 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 meUpperThreshold = SPH.getMongeElkanUpper(mContext);
CustomCommand customCommand = null;
final ArrayList<CustomCommandContainer> toKeep = new ArrayList<>();
final StringMetric me = with(new MongeElkan(new SmithWatermanGotoh())).tokenize(whitespace()).build();
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 = me.compare(phrase, vd);
if (distance > meUpperThreshold) {
if (DEBUG) {
MyLog.i(CLS_NAME, "Keeping " + phrase);
}
container.setUtterance(vd);
container.setScore(distance);
if (distance == Algorithm.ME_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.MONGE_ELKAN);
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "no custom phrases above threshold");
}
}
if (DEBUG) {
MyLog.getElapsed(MongeElkanHelper.class.getSimpleName(), then);
}
return customCommand;
}
Aggregations