Skip to main content

Pinyin-based wake word detection for Chinese speech recognition pipelines

Project description

pinyin-wakeword

Pinyin-based wake word detection for Chinese speech recognition pipelines

PyPI version Python versions License Issues

简体中文 | 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

  • 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 APIfeed(), 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:

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:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/your-feature)
  3. Commit your changes (git commit -m 'Add some feature')
  4. Push to the branch (git push origin feature/your-feature)
  5. 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


Download files

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

Source Distribution

pinyin_wakeword-0.1.0.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

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

pinyin_wakeword-0.1.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

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

Hashes for pinyin_wakeword-0.1.0.tar.gz
Algorithm Hash digest
SHA256 03cce7f22b2915b3832f3303e1a11bd1f46fc09aebbd5bc03440685247cc12bd
MD5 bbbed44f8edff7d7728d8a17d6266078
BLAKE2b-256 3321fc295d15f6a7e7481440f6f6ea952bad208307256a15a87095270c4f3312

See more details on using hashes here.

Provenance

The following attestation bundles were made for pinyin_wakeword-0.1.0.tar.gz:

Publisher: publish.yml on akkoaya/pinyin-wakeword

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pinyin_wakeword-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pinyin_wakeword-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 729da7c60fd8df4f101f4697803efbd75c6af2461ec7132c4f5fe3a7e06e251e
MD5 5baae08c860efc612a66b2baac436c54
BLAKE2b-256 810ff8e178e688d565b5ef33a26bc53fe6daf26746742a04b701cfeadc23b6c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for pinyin_wakeword-0.1.0-py3-none-any.whl:

Publisher: publish.yml on akkoaya/pinyin-wakeword

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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