use of ai.saiy.android.processing.Qubit in project Saiy-PS by brandall76.
the class CommandTranslateLocal method getResponse.
/**
* Resolve the translation request and return the {@link Outcome}
*
* @param ctx the application context
* @param voiceData the array list of voice data
* @param sl the {@link SupportedLanguage}
* @param cr the {@link CommandRequest}
* @return the created {@link Outcome}
*/
public Outcome getResponse(@NonNull final Context ctx, @NonNull final ArrayList<String> voiceData, @NonNull final SupportedLanguage sl, @NonNull final CommandRequest cr) {
if (DEBUG) {
MyLog.i(CLS_NAME, "voiceData: " + voiceData.size() + " : " + voiceData.toString());
}
outcome.setTTSLocale(cr.getTTSLocale(ctx));
switch(SPH.getDefaultTranslationProvider(ctx)) {
case TranslationProvider.TRANSLATION_PROVIDER_BING:
if (DEBUG) {
MyLog.i(CLS_NAME, "TRANSLATION_PROVIDER_BING");
}
final Pair<TranslationLanguageBing, String> bingPair = BingTranslate.extract(ctx, voiceData, sl);
if (bingPair.first != null && bingPair.second != null) {
String translationRequest = bingPair.second;
if (ClipboardHelper.isClipboard(ctx, translationRequest)) {
try {
Thread.sleep(CLIPBOARD_DELAY);
} catch (final InterruptedException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "InterruptedException");
e.printStackTrace();
}
}
final Pair<Boolean, String> clipboardPair = ClipboardHelper.getClipboardContentPair(ctx, cr.getSupportedLanguage());
if (clipboardPair.first) {
translationRequest = clipboardPair.second;
} else {
outcome.setUtterance(clipboardPair.second);
outcome.setOutcome(Outcome.FAILURE);
return outcome;
}
}
if (!BingTranslate.tooLong(translationRequest)) {
if (DEBUG) {
MyLog.i(CLS_NAME, "translationRequest: " + translationRequest);
}
final Pair<Boolean, String> translationResult = BingTranslate.execute(ctx, bingPair.first, translationRequest);
if (translationResult.first) {
outcome.setUtterance(translationResult.second);
outcome.setOutcome(Outcome.SUCCESS);
entangledPair.setToastContent(translationResult.second);
outcome.setEntangledPair(entangledPair);
final Qubit qubit = new Qubit();
qubit.setTranslatedText(translationResult.second);
outcome.setQubit(qubit);
outcome.setTTSLocale(bingPair.first.getLocale());
} else {
outcome.setUtterance(ctx.getString(ai.saiy.android.R.string.error_translate, PersonalityHelper.getUserNameOrNot(ctx)));
outcome.setOutcome(Outcome.FAILURE);
}
} else {
outcome.setUtterance(ctx.getString(ai.saiy.android.R.string.error_translate_length, PersonalityHelper.getUserNameOrNot(ctx)));
outcome.setOutcome(Outcome.FAILURE);
}
} else {
outcome.setUtterance(ctx.getString(ai.saiy.android.R.string.error_translate_unsupported, PersonalityHelper.getUserNameOrNot(ctx)));
outcome.setOutcome(Outcome.FAILURE);
}
return outcome;
case TranslationProvider.TRANSLATION_PROVIDER_GOOGLE:
if (DEBUG) {
MyLog.i(CLS_NAME, "TRANSLATION_PROVIDER_GOOGLE");
}
final Pair<TranslationLanguageGoogle, String> googlePair = GoogleTranslate.extract(ctx, voiceData, sl);
if (googlePair.first != null && googlePair.second != null) {
String translationRequest = googlePair.second;
if (ClipboardHelper.isClipboard(ctx, translationRequest)) {
try {
Thread.sleep(CLIPBOARD_DELAY);
} catch (final InterruptedException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "InterruptedException");
e.printStackTrace();
}
}
final Pair<Boolean, String> clipboardPair = ClipboardHelper.getClipboardContentPair(ctx, cr.getSupportedLanguage());
if (clipboardPair.first) {
translationRequest = clipboardPair.second;
} else {
outcome.setUtterance(clipboardPair.second);
outcome.setOutcome(Outcome.FAILURE);
return outcome;
}
}
if (!GoogleTranslate.tooLong(translationRequest)) {
if (DEBUG) {
MyLog.i(CLS_NAME, "translationRequest: " + translationRequest);
}
final Pair<Boolean, String> translationResult = GoogleTranslate.execute(ctx, googlePair.first, translationRequest);
if (translationResult.first) {
outcome.setUtterance(translationResult.second);
outcome.setOutcome(Outcome.SUCCESS);
entangledPair.setToastContent(translationResult.second);
outcome.setEntangledPair(entangledPair);
final Qubit qubit = new Qubit();
qubit.setTranslatedText(translationResult.second);
outcome.setQubit(qubit);
outcome.setTTSLocale(UtilsLocale.stringToLocale(googlePair.first.getLanguage()));
} else {
outcome.setUtterance(ctx.getString(ai.saiy.android.R.string.error_translate, PersonalityHelper.getUserNameOrNot(ctx)));
outcome.setOutcome(Outcome.FAILURE);
}
} else {
outcome.setUtterance(ctx.getString(ai.saiy.android.R.string.error_translate_length, PersonalityHelper.getUserNameOrNot(ctx)));
outcome.setOutcome(Outcome.FAILURE);
}
} else {
outcome.setUtterance(ctx.getString(ai.saiy.android.R.string.error_translate_unsupported, PersonalityHelper.getUserNameOrNot(ctx)));
outcome.setOutcome(Outcome.FAILURE);
}
return outcome;
case TranslationProvider.TRANSLATION_PROVIDER_UNKNOWN:
default:
if (DEBUG) {
MyLog.i(CLS_NAME, "TRANSLATION_PROVIDER_UNKNOWN");
}
outcome.setUtterance(ctx.getString(ai.saiy.android.R.string.error_translate_unsupported, PersonalityHelper.getUserNameOrNot(ctx)));
outcome.setOutcome(Outcome.FAILURE);
return outcome;
}
}
use of ai.saiy.android.processing.Qubit in project Saiy-PS by brandall76.
the class BingTranslate method getResponse.
/**
* Resolve the translation request and return the {@link Outcome}
*
* @return the created {@link Outcome}
*/
public Outcome getResponse() {
final CommandTranslateValues ctv = (CommandTranslateValues) cr.getVariableData();
final TranslationLanguageBing language = resolveLanguage(ctv.getLanguage());
if (language != TranslationLanguageBing.AUTO_DETECT) {
String translationRequest = ctv.getText();
if (DEBUG) {
MyLog.d(CLS_NAME, "language: " + language.name());
MyLog.d(CLS_NAME, "request: " + translationRequest);
}
if (ClipboardHelper.isClipboard(mContext, translationRequest)) {
try {
Thread.sleep(CLIPBOARD_DELAY);
} catch (final InterruptedException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "InterruptedException");
e.printStackTrace();
}
}
final Pair<Boolean, String> clipboardPair = ClipboardHelper.getClipboardContentPair(mContext, sl);
if (clipboardPair.first) {
translationRequest = clipboardPair.second;
} else {
outcome.setUtterance(clipboardPair.second);
outcome.setOutcome(Outcome.FAILURE);
return outcome;
}
}
if (!tooLong(translationRequest)) {
final Pair<Boolean, String> translationResult = execute(mContext, language, translationRequest);
if (translationResult.first) {
outcome.setUtterance(translationResult.second);
outcome.setOutcome(Outcome.SUCCESS);
final EntangledPair entangledPair = new EntangledPair(Position.TOAST_LONG, CC.COMMAND_TRANSLATE);
entangledPair.setToastContent(translationResult.second);
outcome.setEntangledPair(entangledPair);
final Qubit qubit = new Qubit();
qubit.setTranslatedText(translationResult.second);
outcome.setQubit(qubit);
outcome.setTTSLocale(language.getLocale());
} else {
outcome.setUtterance(mContext.getString(ai.saiy.android.R.string.error_translate, PersonalityHelper.getUserNameOrNot(mContext)));
outcome.setOutcome(Outcome.FAILURE);
return outcome;
}
} else {
outcome.setUtterance(mContext.getString(ai.saiy.android.R.string.error_translate_length, PersonalityHelper.getUserNameOrNot(mContext)));
outcome.setOutcome(Outcome.FAILURE);
return outcome;
}
} else {
outcome.setUtterance(mContext.getString(ai.saiy.android.R.string.error_translate_unsupported, PersonalityHelper.getUserNameOrNot(mContext)));
outcome.setOutcome(Outcome.FAILURE);
return outcome;
}
return outcome;
}
use of ai.saiy.android.processing.Qubit in project Saiy-PS by brandall76.
the class CommandSpell method setOutcomeParams.
/**
* Set the parameters to the {@link Outcome}
*
* @param ctx the application context
* @param sl the {@link SupportedLanguage}
* @param toSpell to word to spell
*/
private void setOutcomeParams(@NonNull final Context ctx, @NonNull final SupportedLanguage sl, @NonNull final String toSpell) {
if (DEBUG) {
MyLog.v(CLS_NAME, "spell: " + toSpell);
}
final Qubit qubit = new Qubit();
qubit.setSpellContent(toSpell);
outcome.setQubit(qubit);
final String separated = getSeparated(toSpell);
outcome.setUtterance(getResponseUtterance(ctx, sl, separated));
entangledPair.setToastContent(separated);
outcome.setEntangledPair(entangledPair);
outcome.setOutcome(Outcome.SUCCESS);
}
use of ai.saiy.android.processing.Qubit in project Saiy-PS by brandall76.
the class CommandVocalRecognition method getResponse.
/**
* Resolve the required command returning an {@link Outcome} object
*
* @param ctx the application context
* @param sl the {@link SupportedLanguage} we are using to analyse the voice data.
* @return {@link Outcome} containing everything we need to respond to the command.
*/
public Outcome getResponse(@NonNull final Context ctx, @NonNull final SupportedLanguage sl) {
final long then = System.nanoTime();
final Outcome outcome = new Outcome();
outcome.setQubit(new Qubit());
final SaiyAccountList saiyAccountList = SaiyAccountHelper.getAccounts(ctx);
if (saiyAccountList != null && saiyAccountList.size() > 0) {
if (DEBUG) {
MyLog.v(CLS_NAME, "saiyAccountList.size: " + saiyAccountList.size());
}
switch(saiyAccountList.size()) {
case 1:
default:
final SaiyAccount saiyAccount = saiyAccountList.getSaiyAccountList().get(0);
if (saiyAccount != null) {
final ProfileItem profileItem = saiyAccount.getProfileItem();
if (profileItem != null) {
final String profileId = profileItem.getId();
if (UtilsString.notNaked(profileId)) {
if (DEBUG) {
MyLog.d(CLS_NAME, "profileId: " + profileId);
}
final Speaker.Status status = Speaker.Status.getStatus(profileItem.getStatus());
if (DEBUG) {
MyLog.d(CLS_NAME, "status: " + status.name());
}
switch(status) {
case SUCCEEDED:
outcome.setUtterance(SaiyResourcesHelper.getStringResource(ctx, sl, R.string.speech_enroll_instructions_15));
outcome.setAction(LocalRequest.ACTION_SPEAK_LISTEN);
outcome.setOutcome(Outcome.SUCCESS);
outcome.setExtra(profileId);
break;
default:
if (DEBUG) {
MyLog.w(CLS_NAME, "enrollment status");
}
outcome.setOutcome(Outcome.FAILURE);
outcome.setUtterance(SaiyResourcesHelper.getStringResource(ctx, sl, R.string.error_vi_status));
break;
}
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "profile id naked");
}
outcome.setOutcome(Outcome.FAILURE);
outcome.setUtterance(SaiyResourcesHelper.getStringResource(ctx, sl, R.string.error_vi_status));
}
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "profile item null");
}
outcome.setOutcome(Outcome.FAILURE);
outcome.setUtterance(SaiyResourcesHelper.getStringResource(ctx, sl, R.string.error_vi_status));
}
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "account null");
}
outcome.setOutcome(Outcome.FAILURE);
outcome.setUtterance(SaiyResourcesHelper.getStringResource(ctx, sl, R.string.error_vi_no_account));
}
break;
}
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "no accounts");
}
outcome.setOutcome(Outcome.FAILURE);
outcome.setUtterance(SaiyResourcesHelper.getStringResource(ctx, sl, R.string.error_vi_no_account));
}
if (DEBUG) {
MyLog.getElapsed(CLS_NAME, then);
}
return outcome;
}
use of ai.saiy.android.processing.Qubit in project Saiy-PS by brandall76.
the class GoogleTranslate method getResponse.
/**
* Resolve the translation request and return the {@link Outcome}
*
* @return the created {@link Outcome}
*/
public Outcome getResponse() {
final CommandTranslateValues ctv = (CommandTranslateValues) cr.getVariableData();
final TranslationLanguageGoogle language = resolveLanguage(ctv.getLanguage());
if (language != TranslationLanguageGoogle.AUTO_DETECT) {
String translationRequest = ctv.getText();
if (DEBUG) {
MyLog.d(CLS_NAME, "language: " + language.name());
MyLog.d(CLS_NAME, "request: " + translationRequest);
}
if (ClipboardHelper.isClipboard(mContext, translationRequest)) {
try {
Thread.sleep(CLIPBOARD_DELAY);
} catch (final InterruptedException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "InterruptedException");
e.printStackTrace();
}
}
final Pair<Boolean, String> clipboardPair = ClipboardHelper.getClipboardContentPair(mContext, sl);
if (clipboardPair.first) {
translationRequest = clipboardPair.second;
} else {
outcome.setUtterance(clipboardPair.second);
outcome.setOutcome(Outcome.FAILURE);
return outcome;
}
}
if (!tooLong(translationRequest)) {
final Pair<Boolean, String> translationResult = execute(mContext, language, translationRequest);
if (translationResult.first) {
outcome.setUtterance(translationResult.second);
outcome.setOutcome(Outcome.SUCCESS);
final EntangledPair entangledPair = new EntangledPair(Position.TOAST_LONG, CC.COMMAND_TRANSLATE);
entangledPair.setToastContent(translationResult.second);
outcome.setEntangledPair(entangledPair);
final Qubit qubit = new Qubit();
qubit.setTranslatedText(translationResult.second);
outcome.setQubit(qubit);
outcome.setTTSLocale(UtilsLocale.stringToLocale(language.getLanguage()));
} else {
outcome.setUtterance(mContext.getString(ai.saiy.android.R.string.error_translate, PersonalityHelper.getUserNameOrNot(mContext)));
outcome.setOutcome(Outcome.FAILURE);
return outcome;
}
} else {
outcome.setUtterance(mContext.getString(ai.saiy.android.R.string.error_translate_length, PersonalityHelper.getUserNameOrNot(mContext)));
outcome.setOutcome(Outcome.FAILURE);
return outcome;
}
} else {
outcome.setUtterance(mContext.getString(ai.saiy.android.R.string.error_translate_unsupported, PersonalityHelper.getUserNameOrNot(mContext)));
outcome.setOutcome(Outcome.FAILURE);
return outcome;
}
return outcome;
}
Aggregations