use of ai.saiy.android.processing.Quantum in project Saiy-PS by brandall76.
the class NLUCoerce method coerce.
/**
* Coerce the NLP results into a generic {@link CommandRequest} object, validating the minimal
* requirements for each implementation.
*/
public void coerce() {
if (nluProvider instanceof NLUMicrosoft) {
if (DEBUG) {
MyLog.i(CLS_NAME, "coerce: instanceof NLUMicrosoft");
}
if (validateNLUNLUMicrosoft((NLUMicrosoft) nluProvider)) {
for (final Intent i : ((NLUMicrosoft) nluProvider).getIntents()) {
if (i.getScore() > NLUMicrosoft.MIN_THRESHOLD) {
commandRequest.setCC(NLUConstants.intentToCC(i.getIntent()));
if (!commandRequest.getCC().equals(CC.COMMAND_UNKNOWN)) {
final NLUMicrosoftHelper microsoftHelper = new NLUMicrosoftHelper();
commandRequest = microsoftHelper.prepareCommand(mContext, commandRequest, getSupportedLanguage(), ((NLUMicrosoft) nluProvider).getEntities());
if (commandRequest.isResolved()) {
break;
}
break;
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "coerce: COMMAND_UNKNOWN");
}
commandRequest.setCC(CC.COMMAND_UNKNOWN);
commandRequest.setResolved(false);
}
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "coerce: below threshold: " + i.getScore());
}
commandRequest.setCC(CC.COMMAND_UNKNOWN);
commandRequest.setResolved(false);
}
}
if (!commandRequest.isResolved()) {
commandRequest.setCC(CC.COMMAND_UNKNOWN);
commandRequest.setResolved(false);
}
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "coerce: NLUMicrosoft validation failed");
}
commandRequest.setCC(CC.COMMAND_UNKNOWN);
commandRequest.setResolved(false);
}
} else if (nluProvider instanceof NLUNuance) {
if (DEBUG) {
MyLog.i(CLS_NAME, "coerce: instanceof NLUNuance");
}
if (validateNLUNuance((NLUNuance) nluProvider)) {
for (final Interpretation interpretation : ((NLUNuance) nluProvider).getInterpretations()) {
if (interpretation.getAction().getIntent().getConfidence() > NLUNuance.MIN_THRESHOLD) {
commandRequest.setCC(NLUConstants.intentToCC(interpretation.getAction().getIntent().getValue()));
if (!commandRequest.getCC().equals(CC.COMMAND_UNKNOWN)) {
final NLUNuanceHelper nuanceHelper = new NLUNuanceHelper();
commandRequest = nuanceHelper.prepareCommand(mContext, commandRequest, getSupportedLanguage(), interpretation.getConcept());
if (commandRequest.isResolved()) {
break;
}
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "coerce: COMMAND_UNKNOWN");
}
commandRequest.setCC(CC.COMMAND_UNKNOWN);
commandRequest.setResolved(false);
}
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "coerce: below threshold: " + interpretation.getAction().getIntent().getConfidence());
}
commandRequest.setCC(CC.COMMAND_UNKNOWN);
commandRequest.setResolved(false);
}
}
if (!commandRequest.isResolved()) {
commandRequest.setCC(CC.COMMAND_UNKNOWN);
commandRequest.setResolved(false);
}
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "coerce: NLUNuance validation failed");
}
commandRequest.setCC(CC.COMMAND_UNKNOWN);
commandRequest.setResolved(false);
}
} else if (nluProvider instanceof NLUAPIAI) {
if (DEBUG) {
MyLog.i(CLS_NAME, "coerce: instanceof NLUAPIAI");
}
if (validateNLUAPIAI((NLUAPIAI) nluProvider)) {
commandRequest.setCC(NLUConstants.intentToCC(((NLUAPIAI) nluProvider).getIntent()));
if (!commandRequest.getCC().equals(CC.COMMAND_UNKNOWN)) {
final NLUAPIAIHelper apiaiHelper = new NLUAPIAIHelper();
commandRequest = apiaiHelper.prepareCommand(mContext, commandRequest, getSupportedLanguage(), ((NLUAPIAI) nluProvider).getParameters());
if (!commandRequest.isResolved()) {
commandRequest.setCC(CC.COMMAND_UNKNOWN);
commandRequest.setResolved(false);
}
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "coerce: COMMAND_UNKNOWN");
}
commandRequest.setCC(CC.COMMAND_UNKNOWN);
commandRequest.setResolved(false);
}
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "coerce: NLUAPIAI validation failed");
}
commandRequest.setCC(CC.COMMAND_UNKNOWN);
commandRequest.setResolved(false);
}
} else if (nluProvider instanceof NLUWit) {
if (DEBUG) {
MyLog.i(CLS_NAME, "coerce: instanceof NLUWit");
}
// TODO
} else if (nluProvider instanceof NLUBluemix) {
if (DEBUG) {
MyLog.i(CLS_NAME, "coerce: instanceof NLUBluemix");
}
// TODO
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "coerce: instanceof NLUSaiy");
}
if (validateNLUSaiy((NLUSaiy) nluProvider)) {
for (final ai.saiy.android.nlu.saiy.Intent intent : ((NLUSaiy) nluProvider).getIntents()) {
commandRequest.setCC(NLUConstants.intentToCC(intent.getIntent()));
if (!commandRequest.getCC().equals(CC.COMMAND_UNKNOWN)) {
final NLUSaiyHelper saiyHelper = new NLUSaiyHelper();
commandRequest = saiyHelper.prepareCommand(mContext, commandRequest, getSupportedLanguage(), intent.getEntities());
if (commandRequest.isResolved()) {
break;
}
break;
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "coerce: COMMAND_UNKNOWN");
}
commandRequest.setCC(CC.COMMAND_UNKNOWN);
commandRequest.setResolved(false);
}
}
if (!commandRequest.isResolved()) {
commandRequest.setCC(CC.COMMAND_UNKNOWN);
commandRequest.setResolved(false);
}
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "coerce: NLUSaiy validation failed");
}
commandRequest.setCC(CC.COMMAND_UNKNOWN);
commandRequest.setResolved(false);
}
}
commandRequest.setResultsArray(getResultsArray());
new Quantum(mContext).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, commandRequest);
}
use of ai.saiy.android.processing.Quantum in project Saiy-PS by brandall76.
the class RecognitionAction method resolve.
/**
* Resolve the command and check for other conditions such as conversation or root commands.
* <p/>
* Will either forward to {@link Quantum} or start an error response.
* <p/>
* The instance of {@link Quantum} should never be requested to run more than once and in
* theory shouldn't be possible. For the sake of weird and wonderful error handling, we will use
* an {@link AsyncTask#THREAD_POOL_EXECUTOR} so even if an instance is running, a new instance
* is started in parallel to prevent a bottle-neck. This could produce some undesired side-effects
* though.
*/
@SuppressWarnings("unchecked")
private void resolve() {
if (DEBUG) {
MyLog.i(CLS_NAME, "resolve");
}
if (UtilsList.notNaked(resultsRecognition)) {
final CommandRequest cr = new CommandRequest(vrLocale, ttsLocale, sl);
cr.setResultsArray(resultsRecognition);
cr.setConfidenceArray(confidenceScores);
cr.setWasSecure(bundle.getBoolean(RecognizerIntent.EXTRA_SECURE, false));
final Quantum quantum = new Quantum(mContext);
switch(bundle.getInt(LocalRequest.EXTRA_CONDITION, Condition.CONDITION_NONE)) {
case Condition.CONDITION_NONE:
if (DEBUG) {
MyLog.i(CLS_NAME, "Condition.CONDITION_NONE");
}
quantum.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, cr);
break;
case Condition.CONDITION_CONVERSATION:
if (DEBUG) {
MyLog.i(CLS_NAME, "Condition.CONDITION_CONVERSATION");
}
// TODO
break;
case Condition.CONDITION_ROOT:
if (DEBUG) {
MyLog.i(CLS_NAME, "Condition.CONDITION_ROOT");
}
// TODO
break;
case Condition.CONDITION_TRANSLATION:
if (DEBUG) {
MyLog.i(CLS_NAME, "Condition.CONDITION_TRANSLATION");
}
// TODO
break;
case Condition.CONDITION_USER_CUSTOM:
if (DEBUG) {
MyLog.i(CLS_NAME, "Condition.CONDITION_USER_CUSTOM");
}
// TODO
break;
case Condition.CONDITION_EMOTION:
if (DEBUG) {
MyLog.i(CLS_NAME, "Condition.CONDITION_EMOTION");
}
// TODO
break;
case Condition.CONDITION_IDENTITY:
if (DEBUG) {
MyLog.i(CLS_NAME, "Condition.CONDITION_IDENTITY");
}
// TODO
break;
case Condition.CONDITION_IDENTIFY:
if (DEBUG) {
MyLog.i(CLS_NAME, "Condition.CONDITION_IDENTIFY");
}
// TODO
break;
case Condition.CONDITION_GOOGLE_NOW:
if (DEBUG) {
MyLog.i(CLS_NAME, "Condition.CONDITION_GOOGLE_NOW");
}
// TODO - this is in danger of creating multiple instances
quantum.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, cr);
break;
case Condition.CONDITION_IGNORE:
if (DEBUG) {
MyLog.w(CLS_NAME, "Condition.CONDITION_IGNORE");
}
sendErrorLocalRequest();
break;
default:
if (DEBUG) {
MyLog.w(CLS_NAME, "Condition.default");
}
sendErrorLocalRequest();
break;
}
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "recognition results naked");
}
sendErrorLocalRequest();
}
}
Aggregations