Skip to main content

A lightweight Python utility for template expansion and matching with slots and fuzzy matching.

Project description

🧩 TemplateMatcher

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

Match Confidence

The match() method returns the best matches sorted by fuzzy confidence (rapidfuzz.distance.DamerauLevenshtein.normalized_similarity).


🧪 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

keyword-template-matcher-0.0.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

keyword_template_matcher-0.0.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file keyword-template-matcher-0.0.1.tar.gz.

File metadata

File hashes

Hashes for keyword-template-matcher-0.0.1.tar.gz
Algorithm Hash digest
SHA256 ee9099486e46f249dc142620deb92abce3c9a60bd6d9aa53e89822f5ee565173
MD5 c91184dbc22e5398ebee8f5ce963f783
BLAKE2b-256 f6ad2e085d6ebb19f2fb1e01f68d9a942def3124ac4108b84bd49cde8b019639

See more details on using hashes here.

File details

Details for the file keyword_template_matcher-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for keyword_template_matcher-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fea4c9d145bcef658d5b050c37940a371a55a0766da2b94fcc0abfc7bee9f510
MD5 7c29031ab9a551a6dab9c04bb0d0506f
BLAKE2b-256 0c9e7e0b8c97ac72d96e62ed47ebefadc3da50bf386ca23851cdb1e80b978641

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page