* // U+4E2D: zhōng,zhòng # 中 * * @throws Exception */ function parse_chars(string $path, callable $fn = null): Generator { $fn ??= fn ($p) => $p; foreach (file($path) as $line) { preg_match('/^U\+(?[0-9A-Z]+):\s+(?\S+)\s+#\s*(?\S+)/', $line, $matched); if ($matched && !empty($matched['pinyin'])) { yield $matched['char'] => $fn(explode(',', $matched['pinyin'])); } elseif (!str_starts_with($line, '#')) { throw new Exception("行解析错误:$line"); } } } /** * @example *
 * // 㞎㞎: bǎ ba # 注释
 * 
* * @throws Exception */ function parse_words(string $path, callable $fn = null): Generator { $fn ??= fn ($p) => $p; foreach (file($path) as $line) { preg_match('/^(?[^#\s]+):\s+(?[\p{L} ]+)#?/u', $line, $matched); if ($matched && !empty($matched['pinyin'])) { yield $matched['word'] => $fn(explode(' ', trim($matched['pinyin']))); } } }