Pinyin-based wake word detection for Chinese speech recognition pipelines
Project description
pinyin-wakeword
Pinyin-based wake word detection for Chinese speech recognition pipelines
简体中文 | English
Uses pypinyin to convert Chinese text to pinyin syllables, enabling homophone-tolerant wake word matching. For example, configuring the wake word as "小月" will also match "晓悦", "小悦", etc.
Table of Contents
- Features
- Installation
- Quick Start
- Configuration
- Match Modes
- Short Mode
- Runtime Control
- Events
- Examples
- Development
- Contributing
- License
Features
- Homophone matching — Detects wake words by pinyin, not characters
- Sliding window — Finds wake words anywhere in the input text
- Short mode — Shortened matching for noisy/speaking contexts
- Multiple wake words — Monitor several wake words simultaneously
- Three match modes — Exact, fuzzy (similarity threshold), partial (prefix)
- Event-driven API —
feed(),check(),iter_events(), and callbacks - Lightweight — Only depends on
pypinyin
Installation
pip install pinyin-wakeword
Or install from source:
git clone https://github.com/akkoaya/pinyin-wakeword.git
cd pinyin-wakeword
pip install -e .
Quick Start
from pinyin_wakeword import PinyinWakeWord, WakeWordConfig, WakeWordEventType
detector = PinyinWakeWord(WakeWordConfig(wake_words="小月小月"))
# Simple boolean check
if detector.check("你好小月小月"):
print("Wake word detected!")
# Event-based
events = detector.feed("你好小月小月请问天气")
for event in events:
if event.type == WakeWordEventType.DETECTED:
print(f"Detected: {event.matched_text} at position {event.position}")
# Callback style
detector.on_detected = lambda e: print(f"Woke up: {e.matched_text}")
detector.feed("小月小月你好")
# Iterator over ASR stream
for event in detector.iter_events(asr_text_stream):
if event.type == WakeWordEventType.DETECTED:
start_listening()
Configuration
config = WakeWordConfig(
wake_words="小月小月", # Wake word(s), string or list
short_mode_length=2, # Characters used in short mode
match_mode="exact", # "exact", "fuzzy", or "partial"
min_text_length=1, # Minimum input text length
similarity_threshold=0.8, # Fuzzy mode threshold (0.0-1.0)
strip_punctuation=True, # Remove punctuation before matching
pinyin_style="normal", # "normal", "tone", or "tone_number"
)
| Parameter | Default | Description |
|---|---|---|
wake_words |
"" |
Wake word string or list of strings |
short_mode_length |
2 |
Number of characters for short mode |
match_mode |
"exact" |
Matching strategy |
min_text_length |
1 |
Minimum text length to process |
similarity_threshold |
0.8 |
Fuzzy mode similarity threshold |
strip_punctuation |
True |
Strip punctuation before matching |
pinyin_style |
"normal" |
Pinyin romanization style |
Match Modes
Exact (default)
Pinyin sequences must match exactly. "小月" matches "晓悦" (same pinyin) but not "小日".
detector = PinyinWakeWord(WakeWordConfig(wake_words="小月", match_mode="exact"))
Fuzzy
Allows partial syllable mismatch based on similarity threshold.
detector = PinyinWakeWord(WakeWordConfig(
wake_words="小月小月",
match_mode="fuzzy",
similarity_threshold=0.7, # 3/4 syllables = 75% >= 70%
))
detector.check("小月小日") # True (75% match)
Partial
Detects incomplete wake word utterances (prefix matching).
detector = PinyinWakeWord(WakeWordConfig(
wake_words="小月小月",
match_mode="partial",
))
events = detector.feed("小月") # Returns PARTIAL_MATCH event
Short Mode
Reduces the required wake word to the first N characters, useful when the system is already speaking or playing audio:
detector = PinyinWakeWord(WakeWordConfig(
wake_words="小月小月",
short_mode_length=2,
))
detector.check("小月") # False (need full "小月小月")
detector.short_mode = True
detector.check("小月") # True (only first 2 chars required)
Runtime Control
# Add/remove wake words dynamically
detector.add_wake_word("你好小智")
detector.remove_wake_word("小月小月")
# Toggle short mode
detector.short_mode = True
detector.short_mode = False
# Reset state
detector.reset()
Events
| Event Type | When | Key Fields |
|---|---|---|
DETECTED |
Wake word found | wake_word, matched_text, position, confidence, short_mode |
PARTIAL_MATCH |
Prefix match (partial mode) | matched_text |
NOT_DETECTED |
No match found | text |
Examples
See the examples/ directory for complete usage examples:
basic_detection.py— Basic wake word detectionwith_asr.py— Integration with ASR streaming
Development
git clone https://github.com/akkoaya/pinyin-wakeword.git
cd pinyin-wakeword
pip install -e ".[dev]"
pytest -v
Contributing
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create your feature branch (
git checkout -b feature/your-feature) - Commit your changes (
git commit -m 'Add some feature') - Push to the branch (
git push origin feature/your-feature) - Open a Pull Request
Please make sure to update tests as appropriate.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgements
- pypinyin — Chinese characters to pinyin conversion
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 pinyin_wakeword-0.1.0.tar.gz.
File metadata
- Download URL: pinyin_wakeword-0.1.0.tar.gz
- Upload date:
- Size: 15.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03cce7f22b2915b3832f3303e1a11bd1f46fc09aebbd5bc03440685247cc12bd
|
|
| MD5 |
bbbed44f8edff7d7728d8a17d6266078
|
|
| BLAKE2b-256 |
3321fc295d15f6a7e7481440f6f6ea952bad208307256a15a87095270c4f3312
|
Provenance
The following attestation bundles were made for pinyin_wakeword-0.1.0.tar.gz:
Publisher:
publish.yml on akkoaya/pinyin-wakeword
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pinyin_wakeword-0.1.0.tar.gz -
Subject digest:
03cce7f22b2915b3832f3303e1a11bd1f46fc09aebbd5bc03440685247cc12bd - Sigstore transparency entry: 1005000664
- Sigstore integration time:
-
Permalink:
akkoaya/pinyin-wakeword@d2dd6223732ce21a480b45d4815779fa7050de18 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/akkoaya
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d2dd6223732ce21a480b45d4815779fa7050de18 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file pinyin_wakeword-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pinyin_wakeword-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
729da7c60fd8df4f101f4697803efbd75c6af2461ec7132c4f5fe3a7e06e251e
|
|
| MD5 |
5baae08c860efc612a66b2baac436c54
|
|
| BLAKE2b-256 |
810ff8e178e688d565b5ef33a26bc53fe6daf26746742a04b701cfeadc23b6c5
|
Provenance
The following attestation bundles were made for pinyin_wakeword-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on akkoaya/pinyin-wakeword
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pinyin_wakeword-0.1.0-py3-none-any.whl -
Subject digest:
729da7c60fd8df4f101f4697803efbd75c6af2461ec7132c4f5fe3a7e06e251e - Sigstore transparency entry: 1005000674
- Sigstore integration time:
-
Permalink:
akkoaya/pinyin-wakeword@d2dd6223732ce21a480b45d4815779fa7050de18 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/akkoaya
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d2dd6223732ce21a480b45d4815779fa7050de18 -
Trigger Event:
workflow_dispatch
-
Statement type: