private VoiceResultProcessor(Context context) { mContext = context; // Bin types, litter types and brands all pulled from cloud mBrands = new ArrayList<>(); mLitterTypes = new ArrayList<>(); mBinTypes = new ArrayList<>(); populateBrandList(); populateLitterTypes(); populateBinTypes(); ArrayList<LocalEntity> menuCommands = new ArrayList<>(); LocalEntity brand = new LocalEntity("brand"); brand.setName(context.getString(R.string.brand_command)); brand.addSearchTerms(new String[] {"friends", "ground", "run", "front", "round"}); menuCommands.add(brand); LocalEntity litterType = new LocalEntity("type"); litterType.setName(mContext.getString(R.string.type_command)); litterType.addSearchTerms(new String[] {"typ", "types", "tight", "tired"}); menuCommands.add(litterType); LocalEntity bin = new LocalEntity("bin"); bin.addSearchTerms(new String[] {"pin", "binh", "spin", "Ben", "friend", "pen", "fin"}); menuCommands.add(bin); ArrayList<LocalEntity> confirmation = new ArrayList<>(); LocalEntity confirmationTerms = new LocalEntity("yes"); confirmationTerms.addSearchTerms(CONFIRMATION_TERMS); confirmation.add(confirmationTerms); // Populate Search objects for use in logic and UI menuSearch = new Search("menu", menuCommands, mContext.getString(R.string.menu_caption)); binSearch = new Search( mContext.getString(R.string.bin_command), mBinTypes, mContext.getString(R.string.bin_caption)); typeSearch = new Search( mContext.getString(R.string.type_command), mLitterTypes, mContext.getString(R.string.type_caption)); brandSearch = new Search( mContext.getString(R.string.brand_command), mBrands, mContext.getString(R.string.brand_caption)); yesNoSearch = new ConfirmationSearch("yesno", confirmation, mContext.getString(R.string.yes_no_caption)); yesNoSearch.setYesTerms(YES_TERMS); yesNoSearch.setNoTerms(NO_TERMS); mActionHandler = new ActionHandler(); mOutingManager = OutingManager.get(context); }
private void createBinTypeList(List<BinType> binTypeEntities) { if (binTypeEntities != null && binTypeEntities.size() > 0) { for (BinType binTypeEntity : binTypeEntities) { LocalEntity binType = new LocalEntity(binTypeEntity.getName()); if (binTypeEntity.getSearchTerms() != null) { binType.setSearchTerms((ArrayList<String>) binTypeEntity.getSearchTerms()); } mBinTypes.add(binType); Log.d(TAG, binType.getName()); } } }
private void createLitterTypeList(List<LitterType> litterTypeEntities) { if (litterTypeEntities != null && litterTypeEntities.size() > 0) { for (LitterType litterTypeEntity : litterTypeEntities) { LocalEntity litterType = new LocalEntity(litterTypeEntity.getName()); if (litterTypeEntity.getSearchTerms() != null) { litterType.setSearchTerms((ArrayList<String>) litterTypeEntity.getSearchTerms()); } mLitterTypes.add(litterType); Log.d(TAG, litterType.getName()); } } }
private void createBrandList(List<Brand> brandEntities) { if (brandEntities != null && brandEntities.size() > 0) { for (Brand brandEntity : brandEntities) { LocalEntity brand = new LocalEntity(brandEntity.getName()); if (brandEntity.getSearchTerms() != null) { brand.setSearchTerms((ArrayList<String>) brandEntity.getSearchTerms()); } mBrands.add(brand); Log.d(TAG, brand.getName()); } } }
// Deal with unexpected results private String processUnexpectedResult(String result) { String toastText = ""; // Cancel item and start again if needed if (result.equals("cancel") || result.equals("council") || result.equals("Council") || result.equals("cancer") || result.equals("pencil")) { mOutingManager.speak("item cancelled"); setCurrentSearch(brandSearch, result); mBin = null; mLitter = null; return null; } for (LocalEntity menuItem : menuSearch.getSearchObjects()) { if (menuItem.getSearchTerms().contains(result)) { setCurrentSearchByString(menuItem.getName()); return null; } } if (!Search.getCurrentSearch().getName().equals("menu")) { toastText = "I haven't heard of " + result + ". Should I log it as other?"; // log current item as 'other' and record the result yesNoSearch.setActive(true); yesNoSearch.setResponse(toastText); yesNoSearch.setPotentialOtherField(result); if (Search.getCurrentSearch().equals(brandSearch)) { mActionHandler.getCurrentAction().setActionType(Action.Type.LOG_OTHER_BRAND); } if (Search.getCurrentSearch().equals(typeSearch)) { mActionHandler.getCurrentAction().setActionType(Action.Type.LOG_OTHER_LITTER_TYPE); } if (Search.getCurrentSearch().equals(binSearch)) { mActionHandler.getCurrentAction().setActionType(Action.Type.LOG_OTHER_BIN_TYPE); } Log.d(TAG, "result at other: " + result); Log.d(TAG, "yesnosearch potentialotherfield set to " + yesNoSearch.getPotentialOtherField()); Log.d(TAG, "currentSearch at unexpected result = " + Search.getCurrentSearch().getName()); setCurrentSearch(yesNoSearch, ""); return toastText; } else { setCurrentSearch(brandSearch, result); return null; } }
public String processVoiceResults(ArrayList<String> matchedStrings) { Log.d(TAG, "Initial results = " + matchedStrings); // Default result is the most likely match of those returned String result = matchedStrings.get(0); boolean resultFound = false; for (int i = 0; i < Search.getCurrentSearch().getSearchObjects().size() && !resultFound; i++) { LocalEntity currentSearchItem = Search.getCurrentSearch().getSearchObjects().get(i); for (int j = 0; j < matchedStrings.size() && !resultFound; j++) { for (int k = 0; k < currentSearchItem.getSearchTerms().size() && !resultFound; k++) { // Log.d(TAG, "matched string = " + matchedStrings.get(j)); // Log.d(TAG, "search string = " + // currentSearchItem.getSearchTerms().get(k)); // If magic algorithm finds a match if (StringUtils.getLevenshteinDistance( matchedStrings.get(j), currentSearchItem.getSearchTerms().get(k)) < (currentSearchItem.getSearchTerms().get(k).length() / 3) || currentSearchItem.getSearchTerms().get(k).equals(matchedStrings.get(j))) { resultFound = true; if (Search.getCurrentSearch().getName().equals("yesno")) { Log.d(TAG, "got here"); result = currentSearchItem.getSearchTerms().get(k); } else { result = Search.getCurrentSearch().getSearchObjects().get(i).getName(); } Log.d(TAG, "result = " + result); // If result found then update next search based on match processExpectedResult(result); return null; } } } } return processUnexpectedResult(result); }
public CopiedEntity(LocalEntity entity) { this.entity = entity; this.relativePosition = entity.getPosition().getPosition().subtract(getOrigin()); }