@Test public void testDetectIntentWithSlot() { System.out.println("detectIntent"); String sessionId = "0"; Map<String,String> intentMap = new HashMap(); intentMap.put("FOWL", "(?<number>\\d)\\s(?<type>ducks|geese)(\\s(?<where>on the lake|in the air))?"); intentMap.put("GOOSE", "goose"); Map<String,List<RegexIntentSlot>> slotMap = new HashMap(); List<RegexIntentSlot> slots = new ArrayList(); slots.add(new RegexIntentSlot("number",true)); slots.add(new RegexIntentSlot("type",true)); slots.add(new RegexIntentSlot("where",false)); slotMap.put("FOWL", slots); RegexIntentDetector instance = new RegexIntentDetector(intentMap, slotMap); Optional<IConversationIntent> result = instance.detectIntent("I see ducks", "en_US", sessionId); assertTrue(!result.isPresent()); result = instance.detectIntent("I see 2 ducks", "en_US", sessionId); assertTrue(result.isPresent()); assertTrue(result.get().getAllRequiredSlotsFilled()); assertEquals("FOWL", result.get().getIntentId()); result = instance.detectIntent("I see 3 geese in the air", "en_US", sessionId); assertTrue(result.isPresent()); assertTrue(result.get().getAllRequiredSlotsFilled()); assertEquals("FOWL", result.get().getIntentId()); assertEquals("3",result.get().getSlots().get("number")); assertEquals("geese",result.get().getSlots().get("type")); }
@Override public Optional<IConversationIntent> detectIntent(String text, String languageCode, String sessionId) { for (Map.Entry<String, Pattern> entry : intentRegexMap.entrySet()) { final Matcher matcher = entry.getValue().matcher(text); if (matcher.find()) { logger.info(String.format("Matched intent %s with regex %s", entry.getKey(), entry.getValue())); final Map<String, Object> slots = new HashMap(); final List<RegexIntentSlot> intentSlots = intentSlotMap.get(entry.getKey()); if (intentSlots != null) { for (RegexIntentSlot intentSlot : intentSlots) { slots.put(intentSlot.getGroupName(), matcher.group(intentSlot.getGroupName())); } } IConversationIntent intent = new IConversationIntent() { @Override public String getIntentId() { return entry.getKey(); } @Override public Map<String, Object> getSlots() { return slots; } @Override public boolean getAllRequiredSlotsFilled() { boolean unfilled = false; if (intentSlots != null) { for (RegexIntentSlot intentSlot : intentSlots) { if (intentSlot.isRequired()) { Object slot = slots.get(intentSlot.getGroupName()); if (slot == null) { unfilled = true; } } } } return !unfilled; } }; return Optional.of(intent); } } logger.info(String.format("No matching intent for %s", text)); return Optional.empty(); }
RegexIntentDetector implements IntentDetector<IConversationIntent> { @Override public Optional<IConversationIntent> detectIntent(String text, String languageCode, String sessionId) { for (Map.Entry<String, Pattern> entry : intentRegexMap.entrySet()) { final Matcher matcher = entry.getValue().matcher(text); if (matcher.find()) { logger.info(String.format("Matched intent %s with regex %s", entry.getKey(), entry.getValue())); final Map<String, Object> slots = new HashMap(); final List<RegexIntentSlot> intentSlots = intentSlotMap.get(entry.getKey()); if (intentSlots != null) { for (RegexIntentSlot intentSlot : intentSlots) { slots.put(intentSlot.getGroupName(), matcher.group(intentSlot.getGroupName())); } } IConversationIntent intent = new IConversationIntent() { @Override public String getIntentId() { return entry.getKey(); } @Override public Map<String, Object> getSlots() { return slots; } @Override public boolean getAllRequiredSlotsFilled() { boolean unfilled = false; if (intentSlots != null) { for (RegexIntentSlot intentSlot : intentSlots) { if (intentSlot.isRequired()) { Object slot = slots.get(intentSlot.getGroupName()); if (slot == null) { unfilled = true; } } } } return !unfilled; } }; return Optional.of(intent); } } logger.info(String.format("No matching intent for %s", text)); return Optional.empty(); } }
RegexIntentDetector implements IntentDetector<IConversationIntent> { @Override public Optional<IConversationIntent> detectIntent(String text, String languageCode, String sessionId) { for (Map.Entry<String, Pattern> entry : intentRegexMap.entrySet()) { final Matcher matcher = entry.getValue().matcher(text); if (matcher.find()) { logger.info(String.format("Matched intent %s with regex %s", entry.getKey(), entry.getValue())); final Map<String, Object> slots = new HashMap(); final List<RegexIntentSlot> intentSlots = intentSlotMap.get(entry.getKey()); if (intentSlots != null) { for (RegexIntentSlot intentSlot : intentSlots) { slots.put(intentSlot.getGroupName(), matcher.group(intentSlot.getGroupName())); } } IConversationIntent intent = new IConversationIntent() { @Override public String getIntentId() { return entry.getKey(); } @Override public Map<String, Object> getSlots() { return slots; } @Override public boolean getAllRequiredSlotsFilled() { boolean unfilled = false; if (intentSlots != null) { for (RegexIntentSlot intentSlot : intentSlots) { if (intentSlot.isRequired()) { Object slot = slots.get(intentSlot.getGroupName()); if (slot == null) { unfilled = true; } } } } return !unfilled; } }; return Optional.of(intent); } } logger.info(String.format("No matching intent for %s", text)); return Optional.empty(); } RegexIntentDetector(Map<String, String> intentRegexMap);  RegexIntentDetector(Map<String, String> intentRegexMap, Map<String, List<RegexIntentSlot>> intentSlotMap); }
RegexIntentDetector implements IntentDetector<IConversationIntent> { @Override public Optional<IConversationIntent> detectIntent(String text, String languageCode, String sessionId) { for (Map.Entry<String, Pattern> entry : intentRegexMap.entrySet()) { final Matcher matcher = entry.getValue().matcher(text); if (matcher.find()) { logger.info(String.format("Matched intent %s with regex %s", entry.getKey(), entry.getValue())); final Map<String, Object> slots = new HashMap(); final List<RegexIntentSlot> intentSlots = intentSlotMap.get(entry.getKey()); if (intentSlots != null) { for (RegexIntentSlot intentSlot : intentSlots) { slots.put(intentSlot.getGroupName(), matcher.group(intentSlot.getGroupName())); } } IConversationIntent intent = new IConversationIntent() { @Override public String getIntentId() { return entry.getKey(); } @Override public Map<String, Object> getSlots() { return slots; } @Override public boolean getAllRequiredSlotsFilled() { boolean unfilled = false; if (intentSlots != null) { for (RegexIntentSlot intentSlot : intentSlots) { if (intentSlot.isRequired()) { Object slot = slots.get(intentSlot.getGroupName()); if (slot == null) { unfilled = true; } } } } return !unfilled; } }; return Optional.of(intent); } } logger.info(String.format("No matching intent for %s", text)); return Optional.empty(); } RegexIntentDetector(Map<String, String> intentRegexMap);  RegexIntentDetector(Map<String, String> intentRegexMap, Map<String, List<RegexIntentSlot>> intentSlotMap); @Override Optional<IConversationIntent> detectIntent(String text, String languageCode, String sessionId); }
RegexIntentDetector implements IntentDetector<IConversationIntent> { @Override public Optional<IConversationIntent> detectIntent(String text, String languageCode, String sessionId) { for (Map.Entry<String, Pattern> entry : intentRegexMap.entrySet()) { final Matcher matcher = entry.getValue().matcher(text); if (matcher.find()) { logger.info(String.format("Matched intent %s with regex %s", entry.getKey(), entry.getValue())); final Map<String, Object> slots = new HashMap(); final List<RegexIntentSlot> intentSlots = intentSlotMap.get(entry.getKey()); if (intentSlots != null) { for (RegexIntentSlot intentSlot : intentSlots) { slots.put(intentSlot.getGroupName(), matcher.group(intentSlot.getGroupName())); } } IConversationIntent intent = new IConversationIntent() { @Override public String getIntentId() { return entry.getKey(); } @Override public Map<String, Object> getSlots() { return slots; } @Override public boolean getAllRequiredSlotsFilled() { boolean unfilled = false; if (intentSlots != null) { for (RegexIntentSlot intentSlot : intentSlots) { if (intentSlot.isRequired()) { Object slot = slots.get(intentSlot.getGroupName()); if (slot == null) { unfilled = true; } } } } return !unfilled; } }; return Optional.of(intent); } } logger.info(String.format("No matching intent for %s", text)); return Optional.empty(); } RegexIntentDetector(Map<String, String> intentRegexMap);  RegexIntentDetector(Map<String, String> intentRegexMap, Map<String, List<RegexIntentSlot>> intentSlotMap); @Override Optional<IConversationIntent> detectIntent(String text, String languageCode, String sessionId); static final String YES; static final String NO; }