Skip to content

Commit

Permalink
RegEx intent recognition is now case insensitive (fix #259)
Browse files Browse the repository at this point in the history
The training sentences are case insensitive, but not mapping entities (refs #261). To do this we need support for synonyms in the RegExIntentRecognitionProvider
  • Loading branch information
gdaniel committed Nov 29, 2019
1 parent fb21558 commit 7bbd21f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The changelog format is based on [Keep a Changelog](https://keepachangelog.com/e
- Renamed `DefaultIntentProvider` to `RegExIntentProvider` to reflect how intents are extracted. **This change breaks the public API**: classes depending on `DefaultIntentProvider` need to update their dependencies. The behavior of the provider is not changed by this update.
- The monitoring database structure has been changed. Data stored with previous versions of Xatkit won't be accessible with the new version, and existing databases need to be deleted/moved before starting bots to avoid data corruption.
- `XatkitSession.merge(other)` now performs a deep copy of the `other` session variables. This allows to update a merged session without altering the base one.
- `RegExIntentRecognitionProvider` is now case insensitive for *training sentences*. This means that the training sentence `hi` now matches `HI`, `Hi`, etc. This change does not affect **mapping entities**, that are still case sensitive. To extend the behavior to mapping entities we need to implement support for synonyms (see [#261](https://github.com/xatkit-bot-platform/xatkit-runtime/issues/261))

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ private List<Pattern> createPatterns(IntentDefinition intentDefinition) {
for (String trainingSentence : intentDefinition.getTrainingSentences()) {
trainingSentence = escapeRegExpReservedCharacters(trainingSentence);
if (intentDefinition.getOutContexts().isEmpty()) {
patterns.add(Pattern.compile("^" + trainingSentence + "$"));
patterns.add(Pattern.compile("^(?i)" + trainingSentence + "$"));
} else {
String preparedTrainingSentence = trainingSentence;
for (Context context : intentDefinition.getOutContexts()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -153,7 +154,7 @@ public void registerIntentDefinitionNoOutContext() {
assertThat(provider.intentPatterns).as("Intent pattern map contains the registered intent definition")
.containsKeys(VALID_INTENT_DEFINITION_NO_OUT_CONTEXT);
assertThat(provider.intentPatterns.get(VALID_INTENT_DEFINITION_NO_OUT_CONTEXT).get(0).pattern()).as("Intent " +
"pattern map contains the correct pattern").isEqualTo("^this is a test$");
"pattern map contains the correct pattern").isEqualTo("^(?i)this is a test$");
}

@Test
Expand Down Expand Up @@ -204,6 +205,24 @@ public void getIntentValidIntentDefinitionWithOutContextMapping() {
provider.registerEntityDefinition(MAPPING_ENTITY);
provider.registerIntentDefinition(INTENT_MAPPING_OUT_CONTEXT);
RecognizedIntent recognizedIntent = provider.getIntent("this is a Person", new XatkitSession("sessionID"));
assertRecognizedIntentWithOutContextMappingIsValid(recognizedIntent, "this is a Person");

}

@Ignore
/*
* Should be enabled to test #261 (https://github.com/xatkit-bot-platform/xatkit-runtime/issues/261)
*/
@Test
public void getIntentValidIntentDefinitionWithOutContextMappingUpperCase() {
provider.registerEntityDefinition(MAPPING_ENTITY);
provider.registerIntentDefinition(INTENT_MAPPING_OUT_CONTEXT);
RecognizedIntent recognizedIntent = provider.getIntent("this is a Person".toUpperCase(), new XatkitSession(
"sessionID"));
assertRecognizedIntentWithOutContextMappingIsValid(recognizedIntent, "this is a Person".toUpperCase());
}

private void assertRecognizedIntentWithOutContextMappingIsValid(RecognizedIntent recognizedIntent, String input) {
assertThat(recognizedIntent).as("Not null recognized intent").isNotNull();
assertThat(recognizedIntent.getDefinition()).as("Valid intent definition").isEqualTo(INTENT_MAPPING_OUT_CONTEXT);
assertThat(recognizedIntent.getOutContextInstances()).as("Recognized intent contains one out context").hasSize(1);
Expand All @@ -213,7 +232,7 @@ public void getIntentValidIntentDefinitionWithOutContextMapping() {
ContextParameterValue value = contextInstance.getValues().get(0);
assertThat(value.getContextParameter()).as("Valid value context parameter").isEqualTo(MAPPING_CONTEXT.getParameters().get(0));
assertThat(value.getValue()).as("Valid value").isEqualTo("Person");
assertThat(recognizedIntent.getMatchedInput()).as("Correct matched input").isEqualTo("this is a Person");
assertThat(recognizedIntent.getMatchedInput()).as("Correct matched input").isEqualTo(input);
assertThat(recognizedIntent.getRecognitionConfidence()).as("Correct confidence level").isEqualTo(1);
}

Expand Down Expand Up @@ -257,6 +276,17 @@ public void getIntentValidIntentDefinitionNoOutContext() {
assertThat(recognizedIntent.getRecognitionConfidence()).as("Correct confidence level").isEqualTo(1);
}

@Test
public void getIntentValidIntentDefinitionNoOutContextUpperCase() {
provider.registerIntentDefinition(VALID_INTENT_DEFINITION_NO_OUT_CONTEXT);
RecognizedIntent recognizedIntent = provider.getIntent("this is a test".toUpperCase(), new XatkitSession(
"sessionID"));
assertThat(recognizedIntent).as("Not null recognized intent").isNotNull();
assertThat(recognizedIntent.getDefinition()).as("Correct matched input").isEqualTo(VALID_INTENT_DEFINITION_NO_OUT_CONTEXT);
assertThat(recognizedIntent.getMatchedInput()).as("Correct matched input").isEqualTo("this is a test".toUpperCase());
assertThat(recognizedIntent.getRecognitionConfidence()).as("Correct confidence level").isEqualTo(1);
}

@Test
public void getIntentValidIntentDefinitionWithReservedRegExpCharacters() {
IntentDefinition intentDefinition = ElementFactory.createIntentDefinitionNoOutContext();
Expand Down

0 comments on commit 7bbd21f

Please sign in to comment.