Skip to content

Commit 7bbd21f

Browse files
committed
RegEx intent recognition is now case insensitive (fix #259)
The training sentences are case insensitive, but not mapping entities (refs #261). To do this we need support for synonyms in the RegExIntentRecognitionProvider
1 parent fb21558 commit 7bbd21f

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ The changelog format is based on [Keep a Changelog](https://keepachangelog.com/e
2626
- 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.
2727
- 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.
2828
- `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.
29+
- `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))
2930

3031
### Removed
3132

core/src/main/java/com/xatkit/core/recognition/regex/RegExIntentRecognitionProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ private List<Pattern> createPatterns(IntentDefinition intentDefinition) {
301301
for (String trainingSentence : intentDefinition.getTrainingSentences()) {
302302
trainingSentence = escapeRegExpReservedCharacters(trainingSentence);
303303
if (intentDefinition.getOutContexts().isEmpty()) {
304-
patterns.add(Pattern.compile("^" + trainingSentence + "$"));
304+
patterns.add(Pattern.compile("^(?i)" + trainingSentence + "$"));
305305
} else {
306306
String preparedTrainingSentence = trainingSentence;
307307
for (Context context : intentDefinition.getOutContexts()) {

core/src/test/java/com/xatkit/core/recognition/regex/RegExIntentRecognitionProviderTest.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.junit.After;
2121
import org.junit.Before;
2222
import org.junit.BeforeClass;
23+
import org.junit.Ignore;
2324
import org.junit.Test;
2425

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

159160
@Test
@@ -204,6 +205,24 @@ public void getIntentValidIntentDefinitionWithOutContextMapping() {
204205
provider.registerEntityDefinition(MAPPING_ENTITY);
205206
provider.registerIntentDefinition(INTENT_MAPPING_OUT_CONTEXT);
206207
RecognizedIntent recognizedIntent = provider.getIntent("this is a Person", new XatkitSession("sessionID"));
208+
assertRecognizedIntentWithOutContextMappingIsValid(recognizedIntent, "this is a Person");
209+
210+
}
211+
212+
@Ignore
213+
/*
214+
* Should be enabled to test #261 (https://github.com/xatkit-bot-platform/xatkit-runtime/issues/261)
215+
*/
216+
@Test
217+
public void getIntentValidIntentDefinitionWithOutContextMappingUpperCase() {
218+
provider.registerEntityDefinition(MAPPING_ENTITY);
219+
provider.registerIntentDefinition(INTENT_MAPPING_OUT_CONTEXT);
220+
RecognizedIntent recognizedIntent = provider.getIntent("this is a Person".toUpperCase(), new XatkitSession(
221+
"sessionID"));
222+
assertRecognizedIntentWithOutContextMappingIsValid(recognizedIntent, "this is a Person".toUpperCase());
223+
}
224+
225+
private void assertRecognizedIntentWithOutContextMappingIsValid(RecognizedIntent recognizedIntent, String input) {
207226
assertThat(recognizedIntent).as("Not null recognized intent").isNotNull();
208227
assertThat(recognizedIntent.getDefinition()).as("Valid intent definition").isEqualTo(INTENT_MAPPING_OUT_CONTEXT);
209228
assertThat(recognizedIntent.getOutContextInstances()).as("Recognized intent contains one out context").hasSize(1);
@@ -213,7 +232,7 @@ public void getIntentValidIntentDefinitionWithOutContextMapping() {
213232
ContextParameterValue value = contextInstance.getValues().get(0);
214233
assertThat(value.getContextParameter()).as("Valid value context parameter").isEqualTo(MAPPING_CONTEXT.getParameters().get(0));
215234
assertThat(value.getValue()).as("Valid value").isEqualTo("Person");
216-
assertThat(recognizedIntent.getMatchedInput()).as("Correct matched input").isEqualTo("this is a Person");
235+
assertThat(recognizedIntent.getMatchedInput()).as("Correct matched input").isEqualTo(input);
217236
assertThat(recognizedIntent.getRecognitionConfidence()).as("Correct confidence level").isEqualTo(1);
218237
}
219238

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

279+
@Test
280+
public void getIntentValidIntentDefinitionNoOutContextUpperCase() {
281+
provider.registerIntentDefinition(VALID_INTENT_DEFINITION_NO_OUT_CONTEXT);
282+
RecognizedIntent recognizedIntent = provider.getIntent("this is a test".toUpperCase(), new XatkitSession(
283+
"sessionID"));
284+
assertThat(recognizedIntent).as("Not null recognized intent").isNotNull();
285+
assertThat(recognizedIntent.getDefinition()).as("Correct matched input").isEqualTo(VALID_INTENT_DEFINITION_NO_OUT_CONTEXT);
286+
assertThat(recognizedIntent.getMatchedInput()).as("Correct matched input").isEqualTo("this is a test".toUpperCase());
287+
assertThat(recognizedIntent.getRecognitionConfidence()).as("Correct confidence level").isEqualTo(1);
288+
}
289+
260290
@Test
261291
public void getIntentValidIntentDefinitionWithReservedRegExpCharacters() {
262292
IntentDefinition intentDefinition = ElementFactory.createIntentDefinitionNoOutContext();

0 commit comments

Comments
 (0)