feat: case insensitive keywords

This commit is contained in:
2026-02-02 12:35:13 +01:00
parent 2e717ec277
commit b3caff0f90
2 changed files with 24 additions and 3 deletions

View File

@@ -107,7 +107,7 @@ class AgentSpeakGenerator:
check if a keyword is a substring of the user's message.
The generated rule has the form:
keyword_said(Keyword) :- user_said(Message) & .substring(Keyword, Message, Pos) & Pos >= 0
keyword_said(Keyword) :- user_said(Message) & .substring_case_insensitive(Keyword, Message, Pos) & Pos >= 0
This enables the system to trigger behaviors based on keyword detection.
"""
@@ -119,7 +119,7 @@ class AgentSpeakGenerator:
AstRule(
AstLiteral("keyword_said", [keyword]),
AstLiteral("user_said", [message])
& AstLiteral(".substring", [keyword, message, position])
& AstLiteral(".substring_case_insensitive", [keyword, message, position])
& (position >= 0),
)
)

View File

@@ -342,6 +342,28 @@ class BDICoreAgent(BaseAgent):
the function expects (which will be located in `term.args`).
"""
@self.actions.add(".substring_case_insensitive", 3)
@agentspeak.optimizer.function_like
def _substring(agent, term, intention):
"""
Find out if a string is a substring of another (case insensitive). Copied mostly from
the agentspeak library method .substring.
"""
needle = agentspeak.asl_str(agentspeak.grounded(term.args[0], intention.scope)).lower()
haystack = agentspeak.asl_str(agentspeak.grounded(term.args[1], intention.scope)).lower()
choicepoint = object()
pos = haystack.find(needle)
while pos != -1:
intention.stack.append(choicepoint)
if agentspeak.unify(term.args[2], pos, intention.scope, intention.stack):
yield
agentspeak.reroll(intention.scope, intention.stack, choicepoint)
pos = haystack.find(needle, pos + 1)
@self.actions.add(".reply", 2)
def _reply(agent, term, intention):
"""
@@ -467,7 +489,6 @@ class BDICoreAgent(BaseAgent):
body=str(trigger_name),
)
# TODO: check with Pim
self.add_behavior(self.send(msg))
yield