use of ai.saiy.android.nlu.local.Resolve in project Saiy-PS by brandall76.
the class SaiyAccessibilityService method interimMatch.
/**
* Check if the interim text matches a command we want to intercept
*
* @param text the intercepted text
* @return true if the text matches a command false otherwise
*/
private boolean interimMatch(@NonNull final String text) {
if (isListening(text)) {
if (DEBUG) {
MyLog.i(CLS_NAME, "interimMatch: listening");
}
return false;
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "interimMatch: Processing: " + text);
}
}
final ArrayList<String> toResolve = new ArrayList<>(1);
toResolve.add(text);
final float[] confidence = new float[1];
confidence[0] = 1f;
final ArrayList<Pair<CC, Float>> resolveArray = new Resolve(getApplicationContext(), toResolve, confidence, sl, true).resolve();
if (DEBUG) {
if (UtilsList.notNaked(resolveArray)) {
MyLog.i(CLS_NAME, "interimMatch: resolveArray size: " + resolveArray.size());
MyLog.i(CLS_NAME, "interimMatch: resolveArray CC: " + resolveArray.get(0).first.name());
} else {
MyLog.i(CLS_NAME, "interimMatch: resolveArray naked");
}
}
return UtilsList.notNaked(resolveArray);
}
use of ai.saiy.android.nlu.local.Resolve in project Saiy-PS by brandall76.
the class Quantum method doTunnelling.
@Override
protected Qubit doTunnelling(final CommandRequest cr) {
if (DEBUG) {
MyLog.i(CLS_NAME, "doTunnelling");
}
Qubit qubit = null;
if (DEBUG) {
MyLog.i(CLS_NAME, "voiceData before: " + cr.getResultsArray().toString());
}
final CustomResolver customResolver = new QuantumHelper().resolve(mContext, sl, cr);
final ArrayList<String> toResolve = customResolver.getVoiceData();
if (customResolver.isCustom()) {
cch = customResolver.getCustomCommandHelper();
COMMAND = cch.getCommandConstant();
} else if (UtilsList.notNaked(toResolve)) {
final float[] confidence = cr.getConfidenceArray();
final SaiyDefaults.LanguageModel languageModel = SPH.getDefaultLanguageModel(mContext);
switch(languageModel) {
case LOCAL:
if (DEBUG) {
MyLog.i(CLS_NAME, "LanguageModel LOCAL");
}
COMMAND = new FrequencyAnalysis(new Resolve(mContext, toResolve, confidence, sl).resolve()).analyse();
break;
default:
if (DEBUG) {
MyLog.i(CLS_NAME, "LanguageModel EXTERNAL");
}
COMMAND = cr.getCC();
if (CommandRequest.inError(COMMAND)) {
if (DEBUG) {
MyLog.i(CLS_NAME, "LanguageModelDefault.EXTERNAL inError: reverting to LOCAL");
}
COMMAND = new FrequencyAnalysis(new Resolve(mContext, toResolve, confidence, sl).resolve()).analyse();
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "LanguageModelDefault.EXTERNAL: populated");
}
}
break;
}
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "toResolve naked");
}
COMMAND = CC.COMMAND_EMPTY_ARRAY;
}
final Outcome outcome;
request.setCommand(COMMAND);
if (Network.networkProceed(mContext, COMMAND)) {
if (DEBUG) {
MyLog.i(CLS_NAME, "DT networkProceed passed");
MyLog.i(CLS_NAME, "DT command secure: " + COMMAND.isSecure());
MyLog.i(CLS_NAME, "DT command requested securely: " + cr.wasSecure());
MyLog.i(CLS_NAME, "DT device secure: " + UtilsDevice.isDeviceLocked(this.mContext));
}
secure = COMMAND.isSecure() && cr.wasSecure() && UtilsDevice.isDeviceLocked(this.mContext);
switch(COMMAND) {
case COMMAND_CANCEL:
if (DEBUG) {
MyLog.i(CLS_NAME, "DT " + CC.COMMAND_CANCEL.name());
}
request.setUtterance(PersonalityResponse.getCancelled(mContext, sl));
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
break;
case COMMAND_SPELL:
if (DEBUG) {
MyLog.i(CLS_NAME, "DT " + CC.COMMAND_SPELL.name());
}
outcome = new CommandSpell().getResponse(mContext, toResolve, sl, cr);
request.setUtterance(outcome.getUtterance());
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
result = outcome.getOutcome();
qubit = outcome.getQubit();
publishProgress(outcome.getEntangledPair());
break;
case COMMAND_TRANSLATE:
if (DEBUG) {
MyLog.i(CLS_NAME, "DT " + CC.COMMAND_TRANSLATE.name());
}
outcome = new CommandTranslate().getResponse(mContext, toResolve, sl, cr);
request.setUtterance(outcome.getUtterance());
request.setTTSLocale(outcome.getTTSLocale());
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
result = outcome.getOutcome();
if (result == Outcome.SUCCESS) {
request.setCondition(Condition.CONDITION_TRANSLATION);
publishProgress(outcome.getEntangledPair());
qubit = outcome.getQubit();
}
break;
case COMMAND_PARDON:
if (DEBUG) {
MyLog.i(CLS_NAME, "DT " + CC.COMMAND_PARDON.name());
}
final Memory memory = MemoryHelper.getMemory(mContext);
if (!secure) {
request.setUtterance(memory.getUtterance());
request.setTTSLocale(UtilsLocale.stringToLocale(memory.getTTSLanguage()));
request.setVRLocale(UtilsLocale.stringToLocale(memory.getVRLanguage()));
request.setAction(memory.getAction());
request.setUtteranceArray(memory.getUtteranceArray());
result = Outcome.SUCCESS;
switch(memory.getCondition()) {
case Condition.CONDITION_TRANSLATION:
request.setCondition(Condition.CONDITION_TRANSLATION);
break;
case Condition.CONDITION_NONE:
case Condition.CONDITION_CONVERSATION:
case Condition.CONDITION_ROOT:
case Condition.CONDITION_USER_CUSTOM:
case Condition.CONDITION_EMOTION:
case Condition.CONDITION_IDENTITY:
case Condition.CONDITION_GOOGLE_NOW:
case Condition.CONDITION_IGNORE:
default:
request.setCondition(Condition.CONDITION_IGNORE);
break;
}
} else {
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
request.setUtterance(PersonalityResponse.getSecureErrorResponse(mContext, sl));
result = Outcome.SUCCESS;
}
break;
case COMMAND_USER_NAME:
if (DEBUG) {
MyLog.i(CLS_NAME, "DT " + CC.COMMAND_USER_NAME.name());
}
outcome = new CommandUserName().getResponse(mContext, toResolve, sl, cr);
request.setUtterance(outcome.getUtterance());
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
result = outcome.getOutcome();
break;
case COMMAND_BATTERY:
if (DEBUG) {
MyLog.i(CLS_NAME, "DT " + CC.COMMAND_BATTERY.name());
}
outcome = new CommandBattery().getResponse(mContext, toResolve, sl, cr);
request.setUtterance(outcome.getUtterance());
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
result = outcome.getOutcome();
break;
case COMMAND_SONG_RECOGNITION:
if (DEBUG) {
MyLog.i(CLS_NAME, "DT " + CC.COMMAND_SONG_RECOGNITION.name());
}
outcome = new CommandSongRecognition().getResponse(mContext, toResolve, sl);
request.setUtterance(outcome.getUtterance());
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
result = outcome.getOutcome();
qubit = outcome.getQubit();
break;
case COMMAND_WOLFRAM_ALPHA:
if (DEBUG) {
MyLog.i(CLS_NAME, "DT " + CC.COMMAND_WOLFRAM_ALPHA.name());
}
outcome = new CommandWolframAlpha().getResponse(mContext, toResolve, sl, cr);
request.setUtterance(outcome.getUtterance());
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
result = outcome.getOutcome();
break;
case COMMAND_TASKER:
if (DEBUG) {
MyLog.i(CLS_NAME, "DT " + CC.COMMAND_TASKER.name());
}
if (!secure) {
outcome = new CommandTasker().getResponse(mContext, toResolve, sl, cr);
request.setUtterance(outcome.getUtterance());
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
result = outcome.getOutcome();
} else {
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
request.setUtterance(PersonalityResponse.getSecureErrorResponse(mContext, sl));
result = Outcome.SUCCESS;
}
break;
case COMMAND_EMOTION:
if (DEBUG) {
MyLog.i(CLS_NAME, "DT " + CC.COMMAND_EMOTION.name());
}
if (!secure) {
outcome = new CommandEmotion().getResponse(mContext, sl);
request.setUtterance(outcome.getUtterance());
request.setAction(LocalRequest.ACTION_SPEAK_LISTEN);
request.setCondition(Condition.CONDITION_EMOTION);
result = outcome.getOutcome();
} else {
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
request.setUtterance(PersonalityResponse.getSecureErrorResponse(mContext, sl));
result = Outcome.SUCCESS;
}
break;
case COMMAND_HOTWORD:
if (DEBUG) {
MyLog.i(CLS_NAME, "DT " + CC.COMMAND_HOTWORD.name());
}
// noinspection NewApi
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !PermissionHelper.checkUsageStatsPermission(mContext)) {
if (ExecuteIntent.settingsIntent(mContext, IntentConstants.SETTINGS_USAGE_STATS)) {
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
request.setUtterance(SaiyResourcesHelper.getStringResource(mContext, sl, R.string.app_speech_usage_stats));
} else {
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
request.setUtterance(SaiyResourcesHelper.getStringResource(mContext, sl, R.string.issue_usage_stats_bug));
}
} else {
request.setAction(LocalRequest.ACTION_TOGGLE_HOTWORD);
}
result = Outcome.SUCCESS;
break;
case COMMAND_VOICE_IDENTIFY:
if (DEBUG) {
MyLog.i(CLS_NAME, "DT " + CC.COMMAND_VOICE_IDENTIFY.name());
}
outcome = new CommandVocalRecognition().getResponse(mContext, sl);
result = outcome.getOutcome();
request.setUtterance(outcome.getUtterance());
if (result == Outcome.SUCCESS) {
request.setAction(LocalRequest.ACTION_SPEAK_LISTEN);
request.setCondition(Condition.CONDITION_IDENTIFY);
request.setIdentityProfile((String) outcome.getExtra());
} else {
qubit = outcome.getQubit();
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
}
break;
case COMMAND_UNKNOWN:
if (DEBUG) {
MyLog.i(CLS_NAME, "DT " + CC.COMMAND_UNKNOWN.name());
}
final int unknownAction = SPH.getCommandUnknownAction(mContext);
switch(unknownAction) {
case Unknown.UNKNOWN_STATE:
case Unknown.UNKNOWN_REPEAT:
if (SPH.getToastUnknown(mContext)) {
final EntangledPair entangledPair = new EntangledPair(Position.TOAST_SHORT, CC.COMMAND_UNKNOWN);
entangledPair.setToastContent(toResolve.get(0) + "??");
publishProgress(entangledPair);
}
break;
}
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
switch(unknownAction) {
case Unknown.UNKNOWN_STATE:
if (DEBUG) {
MyLog.i(CLS_NAME, "Unknown.UNKNOWN_STATE");
}
request.setUtterance(PersonalityResponse.getNoComprende(mContext, sl));
break;
case Unknown.UNKNOWN_REPEAT:
if (DEBUG) {
MyLog.i(CLS_NAME, "Unknown.UNKNOWN_STATE");
}
request.setUtterance(PersonalityResponse.getRepeatCommand(mContext, sl));
request.setAction(LocalRequest.ACTION_SPEAK_LISTEN);
break;
case Unknown.UNKNOWN_GOOGLE_SEARCH:
if (DEBUG) {
MyLog.i(CLS_NAME, "Unknown.UNKNOWN_STATE");
}
if (!ExecuteIntent.googleNow(mContext, toResolve.get(0))) {
request.setUtterance(PersonalityResponse.getNoComprende(mContext, sl));
SPH.setCommandUnknownAction(mContext, Unknown.UNKNOWN_STATE);
final EntangledPair entangledPair = new EntangledPair(Position.TOAST_SHORT, CC.COMMAND_UNKNOWN);
entangledPair.setToastContent(mContext.getString(R.string.error_google_now_broadcast));
publishProgress(entangledPair);
}
break;
case Unknown.UNKNOWN_WOLFRAM_ALPHA:
if (DEBUG) {
MyLog.i(CLS_NAME, "Unknown.UNKNOWN_STATE");
}
if (!ExecuteIntent.wolframAlpha(mContext, toResolve.get(0))) {
request.setUtterance(PersonalityResponse.getNoComprende(mContext, sl));
SPH.setCommandUnknownAction(mContext, Unknown.UNKNOWN_STATE);
final EntangledPair entangledPair = new EntangledPair(Position.TOAST_SHORT, CC.COMMAND_UNKNOWN);
entangledPair.setToastContent(mContext.getString(R.string.error_wolfram_alpha_broadcast));
publishProgress(entangledPair);
}
break;
case Unknown.UNKNOWN_TASKER:
if (DEBUG) {
MyLog.i(CLS_NAME, "Unknown.UNKNOWN_STATE");
}
if (!TaskerHelper.broadcastVoiceData(mContext, toResolve)) {
request.setUtterance(PersonalityResponse.getNoComprende(mContext, sl));
SPH.setCommandUnknownAction(mContext, Unknown.UNKNOWN_STATE);
final EntangledPair entangledPair = new EntangledPair(Position.TOAST_SHORT, CC.COMMAND_UNKNOWN);
entangledPair.setToastContent(mContext.getString(R.string.error_tasker_broadcast));
publishProgress(entangledPair);
}
break;
}
break;
case COMMAND_EMPTY_ARRAY:
if (DEBUG) {
MyLog.i(CLS_NAME, "DT " + CC.COMMAND_EMPTY_ARRAY.name());
}
request.setUtterance(PersonalityResponse.getErrorProfanityFilter(mContext, sl));
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
break;
case COMMAND_USER_CUSTOM:
if (DEBUG) {
MyLog.i(CLS_NAME, "DT " + CC.COMMAND_USER_CUSTOM.name());
}
if (!secure) {
outcome = new CommandCustom().getResponse(mContext, cch.getCommand(), sl, cr);
request.setUtterance(outcome.getUtterance());
request.setAction(outcome.getAction());
result = outcome.getOutcome();
} else {
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
request.setUtterance(PersonalityResponse.getSecureErrorResponse(mContext, sl));
result = Outcome.SUCCESS;
}
break;
case COMMAND_SOMETHING_WEIRD:
if (DEBUG) {
MyLog.i(CLS_NAME, "DT " + CC.COMMAND_SOMETHING_WEIRD.name());
}
request.setUtterance(PersonalityResponse.getNoComprende(mContext, sl));
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
break;
default:
if (DEBUG) {
MyLog.w(CLS_NAME, "DT DEFAULT");
}
request.setUtterance(PersonalityResponse.getNoComprende(mContext, sl));
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
request.setCommand(CC.COMMAND_UNKNOWN);
break;
}
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "DT networkProceed failed");
}
request.setUtterance(PersonalityResponse.getNoNetwork(mContext, sl));
request.setAction(LocalRequest.ACTION_SPEAK_ONLY);
request.setCommand(CC.COMMAND_UNKNOWN);
}
return qubit;
}
use of ai.saiy.android.nlu.local.Resolve 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;
}
}
use of ai.saiy.android.nlu.local.Resolve in project Saiy-PS by brandall76.
the class SaiyAccessibilityService method finalMatch.
/**
* Check if the final text matches a command we want to intercept
*
* @param text the intercepted text
* @return true if the text matches a command false otherwise
*/
private boolean finalMatch(@NonNull final String text) {
if (DEBUG) {
MyLog.i(CLS_NAME, "finalMatch");
}
if (isListening(text)) {
if (DEBUG) {
MyLog.i(CLS_NAME, "finalMatch: listening");
}
return false;
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "finalMatch: Processing: " + text);
}
}
final ArrayList<String> toResolve = new ArrayList<>(1);
toResolve.add(text);
final float[] confidence = new float[1];
confidence[0] = 1f;
final ArrayList<Pair<CC, Float>> resolveArray = new Resolve(getApplicationContext(), toResolve, confidence, sl, false).resolve();
if (DEBUG) {
if (UtilsList.notNaked(resolveArray)) {
MyLog.i(CLS_NAME, "finalMatch: resolveArray size: " + resolveArray.size());
MyLog.i(CLS_NAME, "finalMatch: resolveArray CC: " + resolveArray.get(0).first.name());
} else {
MyLog.i(CLS_NAME, "finalMatch: resolveArray naked");
}
}
return UtilsList.notNaked(resolveArray);
}
Aggregations