use of ai.saiy.android.custom.CustomCommand 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;
}
use of ai.saiy.android.custom.CustomCommand in project Saiy-PS by brandall76.
the class SoundexHelper method executeCustomCommand.
/**
* Method to iterate through the voice data and attempt to match the user's custom commands
* using the {@link Soundex} 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 soundexUpperThreshold = SPH.getSoundexUpper(mContext);
CustomCommand customCommand = null;
final ArrayList<CustomCommandContainer> toKeep = new ArrayList<>();
final Soundex soundex = new Soundex();
final JaroWinklerDistance jwd = new JaroWinklerDistance();
String phrase;
CustomCommandContainer container;
double score;
double distance;
int size = genericData.size();
try {
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 = soundex.difference(phrase, vd);
if (distance > soundexUpperThreshold && Algorithm.checkLength(phrase, vd)) {
score = jwd.apply(phrase, vd);
if (score > jwdLowerThreshold) {
container.setUtterance(vd);
container.setScore(score);
if (distance == Algorithm.SOUNDEX_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));
}
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "Possible match: double check JW: rejected");
}
}
}
}
}
} catch (final EncoderException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "EncoderException");
e.printStackTrace();
}
}
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.SOUNDEX);
} 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.CustomCommand in project Saiy-PS by brandall76.
the class BRRemote method onReceive.
@Override
public void onReceive(final Context context, final Intent intent) {
if (DEBUG) {
MyLog.i(CLS_NAME, "onReceive");
}
if (intent == null) {
if (DEBUG) {
MyLog.w(CLS_NAME, " onHandleIntent: Intent null");
}
return;
}
final String action = intent.getAction();
if (DEBUG) {
examineIntent(intent);
}
if (!UtilsString.notNaked(action)) {
if (DEBUG) {
MyLog.w(CLS_NAME, " onHandleIntent: action null");
}
return;
}
if (!intent.getAction().equals(SaiyKeyphrase.SAIY_REQUEST_RECEIVER)) {
Log.e("Saiy Remote Request", "Incorrect ACTION: rejecting");
return;
}
switch(intent.getIntExtra(SaiyKeyphrase.REQUEST_TYPE, 0)) {
case SaiyKeyphrase.REQUEST_KEYPHRASE:
if (DEBUG) {
MyLog.i(CLS_NAME, "onHandleIntent: REQUEST_KEYPHRASE");
}
final String keyphrase = intent.getStringExtra(SaiyKeyphrase.SAIY_KEYPHRASE);
if (UtilsString.notNaked(keyphrase)) {
final String packageName = intent.getStringExtra(SaiyKeyphrase.REQUESTING_PACKAGE);
if (UtilsString.notNaked(packageName)) {
final BlackListHelper blackListHelper = new BlackListHelper();
if (!blackListHelper.isBlacklisted(context, packageName)) {
final Pair<Boolean, String> appPair = UtilsApplication.getAppNameFromPackage(context.getApplicationContext(), packageName);
if (appPair.first && UtilsString.notNaked(appPair.second)) {
final Locale vrLocale = SPH.getVRLocale(context.getApplicationContext());
final SupportedLanguage sl = SupportedLanguage.getSupportedLanguage(vrLocale);
final ArrayList<String> voiceData = new ArrayList<>(1);
voiceData.add(keyphrase);
final float[] confidence = new float[1];
confidence[0] = 1f;
final ArrayList<Pair<CC, Float>> resolvePair = new Resolve(context.getApplicationContext(), voiceData, confidence, sl).resolve();
if (!UtilsList.notNaked(resolvePair)) {
final CustomCommand customCommand = new CustomCommand(CCC.CUSTOM_INTENT_SERVICE, CC.COMMAND_USER_CUSTOM, keyphrase, SaiyRequestParams.SILENCE, SaiyRequestParams.SILENCE, SPH.getTTSLocale(context.getApplicationContext()).toString(), vrLocale.toString(), LocalRequest.ACTION_SPEAK_ONLY);
final Regex regex = (Regex) intent.getSerializableExtra(SaiyKeyphrase.KEYPHRASE_REGEX);
switch(regex) {
case MATCHES:
case STARTS_WITH:
case ENDS_WITH:
case CONTAINS:
customCommand.setRegex(regex);
break;
case CUSTOM:
customCommand.setRegex(regex);
customCommand.setRegularExpression(intent.getStringExtra(SaiyKeyphrase.REGEX_CONTENT));
break;
}
final Intent remoteIntent = new Intent(SAIY_INTENT_RECEIVER);
remoteIntent.setPackage(packageName);
final Bundle bundle = intent.getExtras();
if (UtilsBundle.notNaked(bundle)) {
if (!UtilsBundle.isSuspicious(bundle)) {
if (DEBUG) {
examineIntent(intent);
}
remoteIntent.putExtras(bundle);
customCommand.setIntent(remoteIntent.toUri(0));
final Pair<Boolean, Long> successPair = CustomCommandHelper.setCommand(context.getApplicationContext(), customCommand, -1);
if (DEBUG) {
MyLog.i(CLS_NAME, "Custom command created: " + successPair.first);
}
final Bundle responseBundle = new Bundle();
final int responseCode = bundle.getInt(SaiyKeyphrase.SAIY_KEYPHRASE_ID, 0);
if (DEBUG) {
MyLog.i(CLS_NAME, "Custom command responseCode: " + responseCode);
}
responseBundle.putInt(SaiyKeyphrase.SAIY_KEYPHRASE_ID, responseCode);
final LocalRequest request = new LocalRequest(context.getApplicationContext());
request.setVRLocale(vrLocale);
request.setTTSLocale(SPH.getTTSLocale(context.getApplicationContext()));
request.setSupportedLanguage(sl);
request.setQueueType(SaiyTextToSpeech.QUEUE_ADD);
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
if (successPair.first) {
request.setUtterance(PersonalityResponse.getRemoteCommandRegisterSuccess(context.getApplicationContext(), sl, appPair.second, keyphrase));
request.execute();
setResult(Activity.RESULT_OK, SaiyKeyphrase.class.getSimpleName(), responseBundle);
} else {
request.setUtterance(PersonalityResponse.getErrorRemoteCommandRegister(context.getApplicationContext(), sl, appPair.second));
request.execute();
setResult(Activity.RESULT_CANCELED, SaiyKeyphrase.class.getSimpleName(), responseBundle);
}
} else {
Log.e("Saiy Remote Request", "Bundle rejected due to contents");
}
} else {
Log.e("Saiy Remote Request", "Request bundle missing contents: rejected");
}
} else {
Log.e("Saiy Remote Request", "Conflict with inbuilt command: rejected");
}
} else {
Log.e("Saiy Remote Request", "Application name undetectable: rejected");
}
} else {
Log.e("Saiy Remote Request", "Application blacklisted: rejected");
}
} else {
Log.e("Saiy Remote Request", "Package name missing: rejected");
}
} else {
Log.e("Saiy Remote Request", "Keyphrase missing: rejected");
}
break;
default:
Log.e("Saiy Remote Request", "Internal type error: rejected");
break;
}
}
Aggregations