Pre-release
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
- Fuzzy matching and confidence scoring with
rapidfuzz - A simple template structure that extends to any language or grammar rule
- Built-in integration with
simplematchfor fuzzy slot matching
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
rapidfuzzfor fast fuzzy matchingsimplematchfor lightweight template parsing
Metadata
Release files for keyword-template-matcher 0.1.8a2
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.8a2.tar.gz | 11.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| keyword_template_matcher-0.1.8a2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 20.6 kB
Release files / keyword_template_matcher-0.1.8a2.tar.gz
| Download URL | keyword_template_matcher-0.1.8a2.tar.gz |
|---|---|
| Size | 11.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9fe5b74558c26e16f2fe3cb7bae0798b2787a7857bc47d57250fc7a14f13e7d3
|
|
BLAKE2b-256 checksum How to use checksums |
031a52a1377ac77d451388bf1528e29235fd70960ac7c8a32d005cc10d4a5425
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / keyword_template_matcher-0.1.8a2-py3-none-any.whl
| Download URL | keyword_template_matcher-0.1.8a2-py3-none-any.whl |
|---|---|
| Size | 9.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b83b3be0ce608c85765b689525de8992352c9153807638c6f0aaa707feacc327
|
|
BLAKE2b-256 checksum How to use checksums |
b8a5b8b535079105670f51adb6b6f7b690fcc92fe9ef6ac94befe79bd010e355
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|