Search in sources :

Example 1 with CC

use of ai.saiy.android.command.helper.CC in project Saiy-PS by brandall76.

the class MemoryHelper method getUnknown.

/**
 * No {@link Memory} was stored, so we need to let the user know. We get the utterance we will
 * announce from the {@link PersonalityResponse} class.
 *
 * @param ctx the application context
 * @return a constructed {@link Memory} object
 */
private static Memory getUnknown(@NonNull final Context ctx) {
    final String vrLanguage = SPH.getVRLocale(ctx).toString();
    final String ttsLanguage = SPH.getTTSLocale(ctx).toString();
    final ArrayList<String> utteranceArray = new ArrayList<>();
    final int action = LocalRequest.ACTION_SPEAK_ONLY;
    final CC command = CC.COMMAND_UNKNOWN;
    final int condition = Condition.CONDITION_NONE;
    final SupportedLanguage sl = SupportedLanguage.getSupportedLanguage(SPH.getVRLocale(ctx));
    final String utterance = PersonalityResponse.getNoMemory(ctx, sl);
    return new Memory(action, vrLanguage, ttsLanguage, utterance, utteranceArray, command, condition, sl);
}
Also used : CC(ai.saiy.android.command.helper.CC) SupportedLanguage(ai.saiy.android.localisation.SupportedLanguage) ArrayList(java.util.ArrayList)

Example 2 with CC

use of ai.saiy.android.command.helper.CC in project Saiy-PS by brandall76.

the class FrequencyAnalysis method analyse.

/**
 * Sort the ArrayList<Integer> to decide on the most probable command.
 *
 * @return the constant command integer {@link CC}
 */
public CC analyse() {
    if (DEBUG) {
        MyLog.v(CLS_NAME, "analyse");
    }
    long then = System.nanoTime();
    final CC commandInt;
    switch(commandArray.size()) {
        case 0:
            if (DEBUG) {
                MyLog.v(CLS_NAME, "empty commandArray");
            }
            commandInt = CC.COMMAND_UNKNOWN;
            break;
        case 1:
            if (DEBUG) {
                MyLog.v(CLS_NAME, "Only one - unanimous");
            }
            commandInt = commandArray.get(0).first;
            break;
        default:
            final ArrayList<CC> ccArray = new ArrayList<>();
            for (final Pair<CC, Float> pair : commandArray) {
                ccArray.add(pair.first);
            }
            if (ccArray.size() > 1) {
                final Map<CC, Integer> cardinalityMap = CollectionUtils.getCardinalityMap(ccArray);
                CC firstCommand = CC.COMMAND_UNKNOWN;
                int firstQuantity = 0;
                CC secondCommand = CC.COMMAND_UNKNOWN;
                int secondQuantity = 0;
                int count = 0;
                for (final Map.Entry<CC, Integer> e : cardinalityMap.entrySet()) {
                    if (DEBUG) {
                        MyLog.v(CLS_NAME, "cardinalityMap: Quantity: " + e.getValue() + " Command: " + e.getKey());
                    }
                    if (count == 0) {
                        firstCommand = e.getKey();
                        firstQuantity = e.getValue();
                    } else if (count == 1) {
                        secondCommand = e.getKey();
                        secondQuantity = e.getValue();
                        break;
                    }
                    count++;
                }
                if (DEBUG) {
                    MyLog.d(CLS_NAME, "firstCommand: " + firstCommand.name());
                    MyLog.d(CLS_NAME, "firstQuantity: " + firstQuantity);
                    MyLog.d(CLS_NAME, "secondCommand: " + secondCommand.name());
                    MyLog.d(CLS_NAME, "secondQuantity: " + secondQuantity);
                }
                final double total = firstQuantity + secondQuantity;
                final double percentage = ((firstQuantity / total) * 100);
                if (percentage > THRESHOLD) {
                    if (DEBUG) {
                        MyLog.v(CLS_NAME, "Percentage: " + percentage + "%");
                    }
                    commandInt = firstCommand;
                } else {
                    if (DEBUG) {
                        MyLog.v(CLS_NAME, "Percentage: " + percentage + "%");
                    }
                    final float firstConfidence = commandArray.get(ccArray.indexOf(firstCommand)).second;
                    final float secondConfidence = commandArray.get(ccArray.indexOf(secondCommand)).second;
                    if (DEBUG) {
                        MyLog.v(CLS_NAME, "firstConfidence: " + String.valueOf(firstConfidence));
                        MyLog.v(CLS_NAME, "secondConfidence: " + String.valueOf(secondConfidence));
                    }
                    if (firstConfidence > secondConfidence) {
                        if (DEBUG) {
                            MyLog.v(CLS_NAME, "firstConfidence: higher confidence");
                        }
                        commandInt = firstCommand;
                    } else if (secondConfidence > firstConfidence) {
                        if (DEBUG) {
                            MyLog.v(CLS_NAME, "secondConfidence: higher confidence");
                        }
                        commandInt = secondCommand;
                    } else {
                        if (DEBUG) {
                            MyLog.v(CLS_NAME, "equal confidence");
                        }
                        if (firstQuantity > secondQuantity) {
                            if (DEBUG) {
                                MyLog.v(CLS_NAME, "first: had more entries");
                            }
                            commandInt = firstCommand;
                        } else if (secondQuantity > firstQuantity) {
                            if (DEBUG) {
                                MyLog.v(CLS_NAME, "second: had more entries");
                            }
                            commandInt = secondCommand;
                        } else {
                            if (DEBUG) {
                                MyLog.v(CLS_NAME, "dead-heat: Selecting priority commands or first-come-first-serve");
                            }
                            if (secondCommand == CC.COMMAND_USER_NAME) {
                                commandInt = secondCommand;
                            } else {
                                commandInt = firstCommand;
                            }
                        }
                    }
                }
            } else {
                if (DEBUG) {
                    MyLog.v(CLS_NAME, "unanimous");
                }
                commandInt = commandArray.get(0).first;
            }
            break;
    }
    if (DEBUG) {
        MyLog.i(CLS_NAME, "returning command ~ " + commandInt);
        MyLog.getElapsed(CLS_NAME, then);
    }
    return commandInt;
}
Also used : CC(ai.saiy.android.command.helper.CC) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 3 with CC

use of ai.saiy.android.command.helper.CC in project Saiy-PS by brandall76.

the class InitStrings method init.

public void init() {
    if (DEBUG) {
        MyLog.d(CLS_NAME, "init: availableProcessors: " + Runtime.getRuntime().availableProcessors());
    }
    final long then = System.nanoTime();
    final ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    try {
        final List<Future<ArrayList<Pair<CC, Float>>>> futures = executorService.invokeAll(callableList, THREADS_TIMEOUT, TimeUnit.MILLISECONDS);
        for (final Future<ArrayList<Pair<CC, Float>>> future : futures) {
            future.get();
        }
    } catch (final ExecutionException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "future: ExecutionException");
            e.printStackTrace();
        }
    } catch (final CancellationException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "future: CancellationException");
            e.printStackTrace();
        }
    } catch (final InterruptedException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "future: InterruptedException");
            e.printStackTrace();
        }
    } finally {
        executorService.shutdown();
    }
    if (DEBUG) {
        MyLog.getElapsed(CLS_NAME, then);
    }
}
Also used : CC(ai.saiy.android.command.helper.CC) CancellationException(java.util.concurrent.CancellationException) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Pair(android.util.Pair)

Example 4 with CC

use of ai.saiy.android.command.helper.CC in project Saiy-PS by brandall76.

the class Resolve method resolve.

/**
 * Create an ArrayList<Integer> containing the frequency of commands.
 *
 * @return an ArrayList<Integer> of all recognised commands
 */
public ArrayList<Pair<CC, Float>> resolve() {
    if (DEBUG) {
        MyLog.d(CLS_NAME, "analyse: voiceData: " + voiceData.size() + " : " + voiceData.toString());
        MyLog.d(CLS_NAME, "analyse: availableProcessors: " + Runtime.getRuntime().availableProcessors());
    }
    final long then = System.nanoTime();
    final ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    final ArrayList<ArrayList<Pair<CC, Float>>> pairList = new ArrayList<>();
    try {
        final List<Future<ArrayList<Pair<CC, Float>>>> futures = executorService.invokeAll(callableList, THREADS_TIMEOUT, TimeUnit.MILLISECONDS);
        for (final Future<ArrayList<Pair<CC, Float>>> future : futures) {
            pairList.add(future.get());
        }
    } catch (final ExecutionException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "future: ExecutionException");
            e.printStackTrace();
        }
    } catch (final CancellationException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "future: CancellationException");
            e.printStackTrace();
        }
    } catch (final InterruptedException e) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "future: InterruptedException");
            e.printStackTrace();
        }
    } finally {
        executorService.shutdown();
    }
    final ArrayList<Pair<CC, Float>> toReturn = new ArrayList<>();
    if (!pairList.isEmpty()) {
        for (final ArrayList<Pair<CC, Float>> pairs : pairList) {
            toReturn.addAll(pairs);
        }
        if (!toReturn.isEmpty()) {
            Collections.sort(toReturn, new Comparator<Pair<CC, Float>>() {

                @Override
                public int compare(final Pair<CC, Float> p1, final Pair<CC, Float> p2) {
                    return Float.compare(p2.second, p1.second);
                }
            });
        }
        if (DEBUG) {
            for (final Pair<CC, Float> pairs : toReturn) {
                MyLog.i(CLS_NAME, "command: " + pairs.first.name() + " ~ " + String.valueOf(pairs.second));
            }
        }
    }
    if (DEBUG) {
        MyLog.getElapsed(CLS_NAME, then);
    }
    return toReturn;
}
Also used : CC(ai.saiy.android.command.helper.CC) ArrayList(java.util.ArrayList) CancellationException(java.util.concurrent.CancellationException) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Pair(android.util.Pair)

Aggregations

CC (ai.saiy.android.command.helper.CC)4 ArrayList (java.util.ArrayList)4 Pair (android.util.Pair)2 CancellationException (java.util.concurrent.CancellationException)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 SupportedLanguage (ai.saiy.android.localisation.SupportedLanguage)1 Map (java.util.Map)1