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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations