use of ai.saiy.android.nlu.bluemix.NLUBluemix 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.nlu.bluemix.NLUBluemix in project Saiy-PS by brandall76.
the class RecognitionBluemix method onMessage.
@Override
public void onMessage(final String message) {
if (DEBUG) {
MyLog.i(CLS_NAME, "onMessage: " + message);
}
final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
NLUBluemix nluBluemix = null;
try {
nluBluemix = gson.fromJson(message, NLUBluemix.class);
} catch (final JsonSyntaxException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "JsonSyntaxException");
e.printStackTrace();
}
} catch (final NullPointerException e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "NullPointerException");
e.printStackTrace();
}
} catch (final Exception e) {
if (DEBUG) {
MyLog.w(CLS_NAME, "Exception");
e.printStackTrace();
}
}
if (nluBluemix != null) {
if (UtilsString.notNaked(nluBluemix.getState())) {
if (DEBUG) {
MyLog.d(CLS_NAME, "Status message: " + nluBluemix.getState());
}
} else {
partialArray.clear();
resultsArray.clear();
confidenceArray.clear();
bundle.clear();
final List<Result> results = nluBluemix.getResults();
if (UtilsList.notNaked(results)) {
final int resultsSize = results.size();
if (DEBUG) {
MyLog.i(CLS_NAME, "results size: " + resultsSize);
}
if (!detectFinal(results)) {
if (DEBUG) {
MyLog.i(CLS_NAME, "onMessage: have partial");
}
for (int i = 0; i < resultsSize; i++) {
final List<Alternative> alternatives = results.get(i).getAlternatives();
final int size = alternatives.size();
if (DEBUG) {
MyLog.i(CLS_NAME, "partial alternatives size: " + size);
}
for (int j = 0; j < size; j++) {
if (DEBUG) {
MyLog.i(CLS_NAME, "final result: " + alternatives.get(j).getTranscript() + " ~ " + alternatives.get(j).getConfidence());
}
partialArray.add(alternatives.get(j).getTranscript().trim());
}
}
bundle.putStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION, partialArray);
listener.onPartialResults(bundle);
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "onMessage: have final");
}
haveFinal.set(true);
if (doError.get()) {
stopListening();
}
for (int i = 0; i < resultsSize; i++) {
final List<Alternative> alternatives = results.get(i).getAlternatives();
final int size = alternatives.size();
if (DEBUG) {
MyLog.i(CLS_NAME, "final alternatives size: " + size);
}
for (int j = 0; j < size; j++) {
if (DEBUG) {
MyLog.i(CLS_NAME, "final result: " + alternatives.get(j).getTranscript() + " ~ " + alternatives.get(j).getConfidence());
}
confidenceArray.add(alternatives.get(j).getConfidence());
resultsArray.add(alternatives.get(j).getTranscript().trim());
}
}
Recognition.setState(Recognition.State.IDLE);
if (languageModel == SaiyDefaults.LanguageModel.IBM) {
if (DEBUG) {
MyLog.i(CLS_NAME, "final: nlu required");
}
if (servingRemote) {
bundle.putString(Request.RESULTS_NLU, message);
bundle.putFloatArray(SpeechRecognizer.CONFIDENCE_SCORES, ArrayUtils.toPrimitive(confidenceArray.toArray(new Float[0]), 0.0F));
bundle.putStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION, resultsArray);
listener.onResults(bundle);
} else {
new ResolveBluemix(mic.getContext(), sl, UtilsLocale.stringToLocale(vrLocale.toString()), ttsLocale, ArrayUtils.toPrimitive(confidenceArray.toArray(new Float[0]), 0.0F), resultsArray).unpack(nluBluemix);
}
} else {
if (DEBUG) {
MyLog.i(CLS_NAME, "final: nlu not required");
}
bundle.putFloatArray(SpeechRecognizer.CONFIDENCE_SCORES, ArrayUtils.toPrimitive(confidenceArray.toArray(new Float[0]), 0.0F));
bundle.putStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION, resultsArray);
listener.onResults(bundle);
}
}
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "onMessage: nluBluemix results naked");
}
}
}
} else {
if (DEBUG) {
MyLog.w(CLS_NAME, "onMessage: nluBluemix null");
}
}
}
Aggregations