npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

pinyin-match-hanzi

v0.1.1

Published

A tool library for matching pinyin with Chinese character candidate words.

Downloads

217

Readme

Pinyin Match Hanzi - 拼音匹配汉字工具库

English Documentation

安装

npm install pinyin-match-hanzi

使用方法

基本使用

import { 
  getPinyinCandidates, 
  getBestCandidates, 
  convertPinyinToChinese, 
  segmentAndConvert,
  segmentPinyin,
  split
} from 'pinyin-match-hanzi'

// 获取拼音对应的候选词
const candidates = getPinyinCandidates('nihao');
console.log(candidates); // [{ w: '你好', f: 1234, matchLength: 4 }, ...]

// 获取最佳候选词
const best = getBestCandidates('woshizhongguoren', 3);
console.log(best); // 返回前3个最佳候选词

// 转换拼音为中文
const chinese = convertPinyinToChinese('woaini');
console.log(chinese); // '我爱你'

// 分词转换
const converted = segmentAndConvert('wo ai shanghai');
console.log(converted); // 尝试转换混合拼音文本

// 分词拼音 - 将连续的拼音分解成带单引号分隔的拼音
const segmented = segmentPinyin('pinyinshurufa');
console.log(segmented); // 'pin'yin'shu'ru'fa' 或类似的分词结果

// 原始分词功能 - 获取详细的分词结果
const splitResult = split('pinyinshurufa');
console.log(splitResult); // 返回包含分词数组和校正结果的对象

API 说明

getPinyinCandidates(pinyin: string)

根据拼音获取所有可能的候选词。

getBestCandidates(pinyin: string, count: number = 1)

获取指定数量的最佳候选词。

convertPinyinToChinese(pinyin: string, index: number = 0)

将拼音转换为中文,index 参数指定选择第几个候选词(从0开始)。

segmentAndConvert(text: string)

将混合的拼音和文字进行分词并转换。

segmentPinyin(pinyin: string)

将连续的拼音字符串分解成带单引号分隔的拼音,便于阅读和理解。

split(text: string, options?: { useCorrector: boolean })

将拼音进行分词处理,返回分词结果和校正后的拼音数组。此函数是内部拼音分词算法的直接暴露。

特点

  • 纯粹的拼音到中文转换功能
  • 基于内置词库的离线转换
  • 高效的DAWG结构支持
  • 支持多种构建格式(ESM、UMD、CJS、IIFE)

注意事项

  • 所有转换都在客户端完成,无需网络请求
  • 适用于需要在应用程序中集成拼音转换功能的场景