A lightweight Python utility for template expansion and matching with slots and fuzzy matching.
Project description
🧩 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file keyword-template-matcher-0.1.0a1.tar.gz.
File metadata
- Download URL: keyword-template-matcher-0.1.0a1.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98bf668705f72af684b78b39d61a04b9b6b6d28f3b78d7c126407dc1167c5f2b
|
|
| MD5 |
b92efaebbac7fe96fa41b7bf169ed74d
|
|
| BLAKE2b-256 |
a727ed33fe791673d8816229672b3df8acc717a438bd1b794fe0f337b7b6569a
|
File details
Details for the file keyword_template_matcher-0.1.0a1-py3-none-any.whl.
File metadata
- Download URL: keyword_template_matcher-0.1.0a1-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f166ab02be6b28437c5e161e664c1bddb29f7c560b79cc4b2f4336b1c6746a6
|
|
| MD5 |
7153ef9f8e179242515311c5e4201ba6
|
|
| BLAKE2b-256 |
2b0977ef40138745e8e65109cb6cac69b533ac613b586c7f1deeb79159afea15
|