This release is a pre-release and may not be stable for production use.
KeywordTemplateMatcher
KeywordTemplateMatcher is a Python library for natural language templates. A template can hold slots, optional phrases, and alternatives. The library expands a template into every sentence it describes, and matches an utterance back to the slot values it filled. Use it to prototype 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 from a supplied dictionary
- Exact structural matching with
simplematch, ranked by literal token count - A simple template structure that extends to any language or grammar rule
- Built-in integration with
simplematchfor slot extraction
Installation
pip install keyword-template-matcher
Usage
1. Expand 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. Match a template
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})]"
]
These templates expand to sentences like:
- call me {name}
- hello, my name is {name}
- tell me a {joke_type} joke
- play {query}
- play {query} in {device_name}
Related projects
- OpenVoiceOS/simplematch - the lightweight template parser this library builds on
Contributions
Open an issue or submit a pull request to report a bug or propose an improvement.
Acknowledgements
simplematchfor lightweight template parsing
Metadata
Release files for keyword-template-matcher 0.1.11a1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| keyword_template_matcher-0.1.11a1.tar.gz | 13.2 kB | Without provenance | Details for keyword_template_matcher-0.1.11a1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| keyword_template_matcher-0.1.11a1-py3-none-any.whl | Python 3 | none | any | Details for keyword_template_matcher-0.1.11a1-py3-none-any.whl |
Total release size: 22.8 kB
Release files / keyword_template_matcher-0.1.11a1.tar.gz
| Download URL | keyword_template_matcher-0.1.11a1.tar.gz |
|---|---|
| Size | 13.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c4892b4f9a9ac790ce0ae10ca9686f6fdce6cdace79cfab0c7432ce593d34bbc
|
|
BLAKE2b-256 checksum How to use checksums |
4d0727e42fd937a6fa7d8d2920da139078d4c7e46905f1f7a2c827cc4180a338
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / keyword_template_matcher-0.1.11a1-py3-none-any.whl
| Download URL | keyword_template_matcher-0.1.11a1-py3-none-any.whl |
|---|---|
| Size | 9.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c6f5324c65fa297c6e2c1c74903771c8fd1eee93ebf66eee62381c6a7fe0cd38
|
|
BLAKE2b-256 checksum How to use checksums |
dea826f62bbcecdff6b12fb8b0a3df4240f51ceb1adaa953de91f027107bc265
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for keyword-template-matcher 0.1.11a1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.