Commit b41a9f30 by Jonathan Thomas

Case insensitive regex (FLEE vs flee), improved friendship unit tests.

parent 99eeea35
Pipeline #12457 passed with stages
in 2 minutes 16 seconds
...@@ -14,7 +14,7 @@ All notable changes to **CreatureChat** are documented in this file. The format ...@@ -14,7 +14,7 @@ All notable changes to **CreatureChat** are documented in this file. The format
### Changed ### Changed
- **Huge improvements** to **chat prompt** for more *balanced* dialog and *predictable* behaviors - **Huge improvements** to **chat prompt** for more *balanced* dialog and *predictable* behaviors
- Improved **Behavior regex** to include both `<BEHAVIOR arg>` and `*BEHAVIOR arg*` syntax, and ignore unknown behaviors. - Improved **Behavior regex** to include both `<BEHAVIOR arg>` and `*BEHAVIOR arg*` syntax, and ignore unknown behaviors.
- Expanded regex to support args with `+` sign (i.e. `<FRIENDSHIP +1>`) - Expanded regex to support args with `+` sign (i.e. `<FRIENDSHIP +1>`) and case-insensitive
- Improved **message cleaning** to remove any remaining `**` and `<>` after parsing behaviors - Improved **message cleaning** to remove any remaining `**` and `<>` after parsing behaviors
- Privacy Policy updated - Privacy Policy updated
......
...@@ -19,7 +19,7 @@ public class MessageParser { ...@@ -19,7 +19,7 @@ public class MessageParser {
LOGGER.info("Parsing message: {}", input); LOGGER.info("Parsing message: {}", input);
StringBuilder cleanedMessage = new StringBuilder(); StringBuilder cleanedMessage = new StringBuilder();
List<Behavior> behaviors = new ArrayList<>(); List<Behavior> behaviors = new ArrayList<>();
Pattern pattern = Pattern.compile("[<*](FOLLOW|FLEE|ATTACK|FRIENDSHIP|UNFOLLOW)(?:\\s+([+-]?\\d+))?[>*]"); Pattern pattern = Pattern.compile("[<*](FOLLOW|FLEE|ATTACK|FRIENDSHIP|UNFOLLOW)(?:\\s+([+-]?\\d+))?[>*]", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input); Matcher matcher = pattern.matcher(input);
while (matcher.find()) { while (matcher.find()) {
......
...@@ -51,7 +51,7 @@ public class BehaviorTests { ...@@ -51,7 +51,7 @@ public class BehaviorTests {
"DIEEE!"); "DIEEE!");
List<String> friendshipUpMessages = Arrays.asList( List<String> friendshipUpMessages = Arrays.asList(
"Hi friend! I am so happy to see you again!", "Hi friend! I am so happy to see you again!",
"How is my best friend doing?", "Looking forward to hanging out with you.",
"<gives 1 golden apple>"); "<gives 1 golden apple>");
List<String> friendshipDownMessages = Arrays.asList( List<String> friendshipDownMessages = Arrays.asList(
"<attacked you directly with Stone Axe>", "<attacked you directly with Stone Axe>",
...@@ -97,49 +97,53 @@ public class BehaviorTests { ...@@ -97,49 +97,53 @@ public class BehaviorTests {
@Test @Test
public void followBrave() { public void followBrave() {
for (String message : followMessages) { for (String message : followMessages) {
testPromptForBehavior(bravePath, message, "FOLLOW"); testPromptForBehavior(bravePath, List.of(message), "FOLLOW");
} }
} }
@Test @Test
public void followNervous() { public void followNervous() {
for (String message : followMessages) { for (String message : followMessages) {
testPromptForBehavior(nervousPath, message, "FOLLOW"); testPromptForBehavior(nervousPath, List.of(message), "FOLLOW");
} }
} }
@Test @Test
public void attackBrave() { public void attackBrave() {
for (String message : attackMessages) { for (String message : attackMessages) {
testPromptForBehavior(bravePath, message, "ATTACK"); testPromptForBehavior(bravePath, List.of(message), "ATTACK");
} }
} }
@Test @Test
public void attackNervous() { public void attackNervous() {
for (String message : attackMessages) { for (String message : attackMessages) {
testPromptForBehavior(nervousPath, message, "FLEE"); testPromptForBehavior(nervousPath, List.of(message), "FLEE");
} }
} }
@Test @Test
public void friendshipUpNervous() { public void friendshipUpNervous() {
for (String message : friendshipUpMessages) { ParsedMessage result = testPromptForBehavior(nervousPath, friendshipUpMessages, "FRIENDSHIP");
ParsedMessage result = testPromptForBehavior(nervousPath, message, "FRIENDSHIP");
assertTrue(result.getBehaviors().stream().anyMatch(b -> "FRIENDSHIP".equals(b.getName()) && b.getArgument() > 0)); assertTrue(result.getBehaviors().stream().anyMatch(b -> "FRIENDSHIP".equals(b.getName()) && b.getArgument() > 0));
} }
@Test
public void friendshipUpBrave() {
ParsedMessage result = testPromptForBehavior(bravePath, friendshipUpMessages, "FRIENDSHIP");
assertTrue(result.getBehaviors().stream().anyMatch(b -> "FRIENDSHIP".equals(b.getName()) && b.getArgument() > 0));
} }
@Test @Test
public void friendshipDownNervous() { public void friendshipDownNervous() {
for (String message : friendshipDownMessages) { for (String message : friendshipDownMessages) {
ParsedMessage result = testPromptForBehavior(nervousPath, message, "FRIENDSHIP"); ParsedMessage result = testPromptForBehavior(nervousPath, List.of(message), "FRIENDSHIP");
assertTrue(result.getBehaviors().stream().anyMatch(b -> "FRIENDSHIP".equals(b.getName()) && b.getArgument() < 0)); assertTrue(result.getBehaviors().stream().anyMatch(b -> "FRIENDSHIP".equals(b.getName()) && b.getArgument() < 0));
} }
} }
public ParsedMessage testPromptForBehavior(Path chatDataPath, String message, String behavior) { public ParsedMessage testPromptForBehavior(Path chatDataPath, List<String> messages, String behavior) {
LOGGER.info("Testing '" + chatDataPath.getFileName() + "' with '" + message + "' and expecting behavior: " + behavior); LOGGER.info("Testing '" + chatDataPath.getFileName() + "' with '" + messages.toString() + "' and expecting behavior: " + behavior);
try { try {
// Load entity chat data // Load entity chat data
...@@ -151,7 +155,9 @@ public class BehaviorTests { ...@@ -151,7 +155,9 @@ public class BehaviorTests {
assertNotNull(contextData); assertNotNull(contextData);
// Add test message // Add test message
for (String message : messages) {
entityTestData.addMessage(message, ChatDataManager.ChatSender.USER); entityTestData.addMessage(message, ChatDataManager.ChatSender.USER);
}
// Get prompt // Get prompt
Path promptPath = Paths.get(PROMPT_PATH, "system-chat"); Path promptPath = Paths.get(PROMPT_PATH, "system-chat");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment