Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Ask DeepWiki

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})
  • 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 simplematch for 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}

Contributions

Open an issue or submit a pull request to report a bug or propose an improvement.

Acknowledgements

Metadata

Release files for keyword-template-matcher 0.1.10a1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for keyword-template-matcher 0.1.10a1
File Size Uploaded
keyword_template_matcher-0.1.10a1.tar.gz 12.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for keyword-template-matcher 0.1.10a1
File Interpreter ABI Platform
keyword_template_matcher-0.1.10a1-py3-none-any.whl Python 3 none any Details

Total release size: 22.1 kB

Release files / keyword_template_matcher-0.1.10a1.tar.gz

Download URL keyword_template_matcher-0.1.10a1.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
0cfb3ab7412b683302b202ee7cb91ae898b29391f092a2b94bad9a5b6895f5a6
BLAKE2b-256 checksum
How to use checksums
1740cad6ed2dea97d0ec4603cfe098d8f1464416cd34992b15840a17b8bb72b8
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.10a1-py3-none-any.whl

Download URL keyword_template_matcher-0.1.10a1-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b560bb675412d787be9268ec5f86dd8853a205819e796b1b72e65e8455534293
BLAKE2b-256 checksum
How to use checksums
cc936e6a7b11b1139dfdaf7dbc6df9fbc05affae692a70838231faa0b2138c11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page