$ show zh-lx/pinyin-pro
zh-lx/pinyin-pro is a high-performance JavaScript/TypeScript library for converting Chinese characters (Hanzi) to Pinyin. It is used both in browsers and Node.js environments, supporting a wide range of output formats (tone marks, tone numbers, initials, finals, etc.) and advanced features like surname detection, tone sandhi, polyphonic character handling, word segmentation, and fuzzy text matching. The library is organized as a monorepo with three packages: the core `pinyin-pro` library, a `@pinyin-pro/data` package containing large curated dictionaries (modern, complete, traditional, buddhist), and documentation sites in Chinese and English. Architecturally, the core employs a middleware pipeline to process input text through sequential transformations (non-Chinese handling, pattern extraction, tone type conversion, etc.). It uses an Aho-Corasick automaton for efficient phrase matching against its multi-tier dictionaries (single characters to multi-word phrases). Multiple tokenization strategies (reverse max match, max probability, min tokenization) are implemented for word segmentation. The library has been heavily optimized for performance, achieving ~99.8% accuracy in benchmarks and competitive speed with native Rust-based alternatives. Key entry points are the `pinyin()` function for basic conversion, and additional functions for matching (`match`), segmentation (`segment`), HTML ruby annotation (`html`), polyphonic listing (`polyphonic`), custom dictionary management (`customPinyin`, `addDict`), and format conversion (`convert`). External dependencies are minimal: the library runs entirely on its built-in dictionaries and does not require any runtime dependencies. The codebase is well-tested (vitest) with a dedicated CI pipeline (GitHub Actions) for quality assurance, bundle size comparison, and automated release.
The LLM identified 3 files worth highlighting for reuse:
Implements an Aho-Corasick automaton for efficient multi-pattern matching. This is a self-contained, generic text-search engine that can be extracted and reused for any pattern-matching problem (e.g., keyword detection, dictionary lookup) with minimal modification. It demonstrates clean automaton construction and state traversal, separate from the pinyin data.
Implements a reverse maximum matching algorithm for word segmentation. This is a standalone algorithm implementation that can be extracted for any tokenization task requiring longest-match-first segmentation. The code is clear, with no tight coupling to pinyin-specific logic, making it reusable in other NLP or text-processing projects.
Defines a chainable middleware pipeline for the `pinyin` function. Each middleware handles a specific transformation (non-Chinese characters, pattern extraction, tone type, etc.). This pattern cleanly separates concerns and allows easy extension or reordering of processing steps. Although slightly coupled to the pinyin data model, the middleware pattern itself is a portable architectural pattern applicable to any sequential processing pipeline.