Implement a keyword-based tool selector:
register_tool(tool_name, keywords): Associate keywords with a toolselect_tool(query): Count keyword matches in query for each tool- Return tool with highest match count, or None if no matches
- Case-insensitive matching
Constraints:
- Keywords can appear multiple times (count all)
- Empty query returns None
- Tie-breaking: first registered tool wins
- Partial word matching is OK (e.g., 'calc' matches 'calculate')
Examples
Example 1:
Input:
selector = KeywordToolSelector(); selector.register_tool('weather', ['weather', 'temperature', 'forecast']); selector.select_tool('What is the weather today?')Output:
'weather'Explanation: Query contains 'weather' keyword, matches weather tool
Example 2:
Input:
selector = KeywordToolSelector(); selector.register_tool('a', ['x']); selector.register_tool('b', ['x']); selector.select_tool('x')Output:
'a'Explanation: Tie between a and b, a was registered first
Starter Code
class KeywordToolSelector:
"""
Simple keyword-based tool selection for agents.
Maps keywords to tool names for quick selection.
"""
def __init__(self):
self.keyword_map = {}
def register_tool(self, tool_name, keywords):
"""Register tool with associated keywords (list)"""
# Your implementation here
pass
def select_tool(self, query):
"""
Select best tool based on keyword matching.
Returns tool_name or None if no match.
In case of tie, return first registered.
"""
# Your implementation here
passPython3
ReadyLines: 1Characters: 0
Ready