🧩 KeywordTemplateMatcher
A lightweight Python utility for expanding and matching natural language templates with slots, optional phrases, and alternatives. It supports fuzzy matching and slot extraction, making it ideal for prototyping NLU systems, voice assistants, or rule-based query matching.
🔧 Features
- ✅ Template expansion with:
- Optional phrases (
[optional]) - Alternatives (
(choice1|choice2)) - Slots (
{slot_name})
- Optional phrases (
- ✅ Slot substitution with provided dictionaries
- ✅ Fuzzy matching with confidence scoring using
rapidfuzz - ✅ Simple template structure, extensible for any language/grammar rules
- ✅ Built-in integration with
simplematchfor fuzzy slot matching
📦 Installation
pip install keyword-template-matcher
🚀 Usage
1. Expanding a Template with Slots
from kw_template_matcher import expand_slots
template = "change [the ]brightness to {brightness_level} and color to {color_name}"
slots = {
"brightness_level": ["low", "medium", "high"],
"color_name": ["red", "green", "blue"]
}
for sentence in expand_slots(template, slots):
print(sentence)
Output:
change the brightness to low and color to red
change brightness to high and color to blue
... (all combinations)
2. Template Matching
from kw_template_matcher import TemplateMatcher
matcher = TemplateMatcher()
matcher.add_templates([
"[hello,] (call me|my name is) {name}",
"tell me a [{joke_type}] joke"
])
query = "hello, my name is Alice"
results = matcher.match(query)
for match in results:
print(match)
🧠 How It Works
Template Syntax
| Syntax | Description |
|---|---|
{slot} |
Placeholder to be replaced with values |
[optional] |
Optional word or phrase |
(a|b|c) |
Alternatives - only one is chosen |
🧪 Test Example Templates
templates = [
"[hello,] (call me|my name is) {name}",
"tell me a [{joke_type}] joke",
"play {query} [in ({device_name}|{skill_name}|{zone_name})]"
]
Produces expansions like:
- call me {name}
- hello, my name is {name}
- tell me a {joke_type} joke
- play {query}
- play {query} in {device_name}
🤝 Contributions
PRs, issues, and suggestions welcome! Feel free to open an issue or submit a pull request if you'd like to improve the matcher.
💬 Acknowledgements
rapidfuzzfor fast fuzzy matchingsimplematchfor lightweight template parsing
Metadata
Release files for keyword-template-matcher 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| keyword-template-matcher-0.1.1.tar.gz | 6.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| keyword_template_matcher-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.8 kB
Release files / keyword-template-matcher-0.1.1.tar.gz
| Download URL | keyword-template-matcher-0.1.1.tar.gz |
|---|---|
| Size | 6.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b6bd91a85f1011150286a447ece313ebdf25762b076e5157eb38e08b7e47f50d
|
|
BLAKE2b-256 checksum How to use checksums |
79d8a3ce7b3ed4edc28dd51f9188b8012aebf6b75894a17f81e350bee3605c85
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.23
|
Release files / keyword_template_matcher-0.1.1-py3-none-any.whl
| Download URL | keyword_template_matcher-0.1.1-py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a8c7154c45620a03e9a368e9e43c042de5358047200766f9655b2b657160fe53
|
|
BLAKE2b-256 checksum How to use checksums |
a205619e5bc02f9be94eed0de603ea5a5af78d32c9fad07575c1fcc3b8068d4b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.23
|