Giter Site home page Giter Site logo

pinyin's Introduction

pīnyīn (v3)

pīnyīn, 汉字拼音转换工具。


NPM version Build Status Coverage Status Language Grade: JavaScript NPM downloads

Web Site: 简体中文 | English | 한국어

README: 简体中文 | English | 한국어

转换中文字符为拼音。可以用于汉字注音、排序、检索。

注:这个版本同时支持在 Node 和 Web 浏览器环境运行,

Python 版请关注 mozillazg/python-pinyin


特性

  • 根据词组智能匹配最正确的拼音。
  • 支持多音字。
  • 简单的繁体支持。
  • 支持多种不同拼音风格。

安装

via npm:

npm install pinyin --save

用法

开发者:

import pinyin from "pinyin";

console.log(pinyin("中心"));    // [ [ 'zhōng' ], [ 'xīn' ] ]

console.log(pinyin("中心", {
  heteronym: true,              // 启用多音字模式
}));                            // [ [ 'zhōng', 'zhòng' ], [ 'xīn' ] ]

console.log(pinyin("中心", {
  heteronym: true,              // 启用多音字模式
  segment: true,                // 启用分词,以解决多音字问题。默认不开启,使用 true 开启使用 Intl.Segmenter 分词库。
}));                            // [ [ 'zhōng' ], [ 'xīn' ] ]

console.log(pinyin("中心", {
  segment: "@node-rs/jieba",    // 指定分词库,可以是 "Intl.Segmenter", "nodejieba"、"segmentit"、"@node-rs/jieba"。
}));                            // [ [ 'zhōng' ], [ 'xīn' ] ]

console.log(pinyin("我喜欢你", {
  segment: "segmentit",         // 启用分词
  group: true,                  // 启用词组
}));                            // [ [ 'wǒ' ], [ 'xǐhuān' ], [ 'nǐ' ] ]

console.log(pinyin("中心", {
  style: "initials",            // 设置拼音风格。
  heteronym: true,              // 即使有多音字,因为拼音风格选择,重复的也会合并。
}));                            // [ [ 'zh' ], [ 'x' ] ]

console.log(pinyin("华夫人", {
  mode: "surname",              // 姓名模式。
}));                            // [ ['huà'], ['fū'], ['rén'] ]

命令行:

$ pinyin 中心
zhōng xīn
$ pinyin -h

类型

IPinyinOptions

传入给 pinyin 方法的第二个参数的选项类型。

export interface IPinyinOptions {
  style?: IPinyinStyle; // 拼音输出形式
  mode?: IPinyinMode, // 拼音模式
  // 指定分词库。
  // 为了兼容老版本,可以使用 boolean 类型指定是否开启分词,默认开启。
  segment?: IPinyinSegment | boolean;
  // 是否返回多音字
  heteronym?: boolean;
  // 是否分组词组拼音
  group?: boolean;
  compact?: boolean;
}

IPinyinStyle

输出拼音格式。可以从直接使用以下字符串或数字,也兼容 v2 版本中 pinyin.STYLE_TONE 这样的形式。

export type IPinyinStyle =
  "normal" | "tone" | "tone2" | "to3ne" | "initials" | "first_letter" | "passport" | // 推荐使用小写,和输出的拼音一致
  "NORMAL" | "TONE" | "TONE2" | "TO3NE" | "INITIALS" | "FIRST_LETTER" | "PASSPORT" | // 方便老版本迁移
  0        | 1      | 2       | 5       | 3          | 4;               // 兼容老版本

IPinyinMode

拼音模式,默认普通模式,可以指定人名模式。

// - NORMAL: 普通模式
// - SURNAME: 姓氏模式,优先使用姓氏的拼音。
export type IPinyinMode =
  "normal" | "surname" |
  "NORMAL" | "SURNAME";

IPinyinSegment

分词方式。

  • 默认关闭 false
  • 也可以设置为 true 开启,Web 和 Node 版中均使用 "Intl.Segmenter" 分词。
  • 也可以声明以下字符串来指定分词算法。但目前 Web 版只支持 "Intl.Segmenter" 和 "segmentit" 分词。
export type IPinyinSegment = "Intl.Segmenter" | "nodejieba" | "segmentit" | "@node-rs/jieba";

API

方法 <Array> pinyin(words: string[, options: IPinyinOptions])

将传入的中文字符串 (words) 转换成拼音符号串。

options 是可选的,可以设定拼音风格,或打开多音字选项。

返回二维数组,第一维每个数组项位置对应每个中文字符串位置。 第二维是各个汉字的读音列表,多音字会有多个拼音项。

方法 Number compare(a, b)

按拼音排序的默认算法。

方法 string[][] compact(pinyinResult array[][])

将拼音多音字以各种可能的组合排列变换成紧凑形式。参考 options.compact

参数

<Boolean|IPinyinSegment> options.segment

是否启用分词模式,中文分词有助于极大的降低多音字问题。 但性能会极大的下降,内存也会使用更多。

  • 默认不启用分词。
  • 如果 segemnt = true,默认使用 Intl.Segmenter 分词。
  • 可以指定 "Intl.Segmenter"、"nodejieba"、"segmentit"、"@node-rs/jieba" 进行分词。

<Boolean> options.heteronym

是否启用多音字模式,默认关闭。

关闭多音字模式时,返回每个汉字第一个匹配的拼音。

启用多音字模式时,返回多音字的所有拼音列表。

<Boolean> options.group

按词组分组拼音,例如:

我喜欢你
wǒ xǐhuān nǐ

<IPinyinStyle> options.style

指定拼音风格。可以使用以下特定字符串或数值指定:

IPinyinStyle =
  "normal" | "tone" | "tone2" | "to3ne" | "initials" | "first_letter" | "passport" | // 推荐使用小写,和输出的拼音一致
  "NORMAL" | "TONE" | "TONE2" | "TO3NE" | "INITIALS" | "FIRST_LETTER" | "PASSPORT" | // 方便老版本迁移
  0        | 1      | 2       | 5       | 3          | 4;               // 兼容老版本

NORMAL, normal

普通风格,即不带声调。

如:pin yin

TONE, tone

声调风格,拼音声调在韵母第一个字母上。

注:这是默认的风格。

如:pīn yīn

TONE2, tone2

声调风格 2,即拼音声调以数字形式在各个拼音之后,用数字 [0-4] 进行表示。

如:pin1 yin1

TO3NE, to3ne

声调风格 3,即拼音声调以数字形式在注音字符之后,用数字 [0-4] 进行表示。

如:pi1n yi1n

INITIALS, initials

声母风格,只返回各个拼音的声母部分。对于没有声母的汉字,返回空白字符串。

如:** 的拼音 zh g

注:声母风格会区分 zhzchcshs

注意:部分汉字没有声母,如 饿 等,另外 y, w, yu 都不是声母, 这些汉字的拼音声母风格会返回 ""。请仔细考虑你的需求是否应该使用首字母风格。 详情请参考 [为什么没有 y, w, yu 几个声母](#为什么没有 -y-w-yu- 几个声母)

FIRST_LETTER, first_letter

首字母风格,只返回拼音的首字母部分。

如:p y

PASSPORT, passport

护照风格。转换成大写形式,并且 ü 会以 YU 形式输出。

国家移民管理局门户网站于2021年9月29日发布了《关于内地居民拼音姓名中字母“ü”在出入境证件中打印规则的公告》(以下简称公告),根据《**人名汉语拼音字母拼写规则》和《关于机读旅行证件的相关国际通用规范》, 内地居民申办出入境证件,出入境证件上打印的持证人拼音姓名中,Lü(吕等字)、Nü(女等字)中的字母“ü”应当转换为“YU”,LüE(略等字)、NüE(虐等字)中的字母“ü”应当转换为“U”。

options.mode

拼音模式,默认 "NORMAL" 普通模式。 如果你明确的在姓名场景下,可以使用 "SURNAME" 让姓氏使用更准确的拼音。

  • NORMAL:普通模式,自动识别读音。
  • SURNAME:姓名模式,对于明确的姓名场景,可以更准确的识别姓氏的读音。

options.compact

是否返回紧凑模式,默认 false,按标准格式返回。 如果设置为 true,则将多音字可能的各种组合排列后返回。例如:

pinyin("你好吗", { compact:false });
> [[nǐ], [hǎo,hào], [ma,má,mǎ]]

pinyin("你好吗", { compact:true });
> [
>   [nǐ,hǎo,ma], [nǐ,hǎo,má], [nǐ,hǎo,mǎ],
>   [nǐ,hào,ma], [nǐ,hào,má], [nǐ,hào,mǎ],
> ]

你也可以必要时使用 compact() 函数处理 pinyin(han, {compact:false}) 返回的结果。

Test

npm test

Q&A

关于 Web 版如何使用

首先,我建议大家应该优先考虑在服务端一次性转换拼音并将结果持久化,避免在客户端每次转换损耗性能和体验。

如果你坚持在客户端使用,你可以考虑使用 Webpack + Babel 来转换成低端浏览器的可执行代码。

实在不想折腾,可以试试 https://github.com/hotoo/pinyin/tree/gh-pages/dist

为什么没有 y, w, yu 几个声母?

声母风格(INITIALS)下,“雨”、“我”、“圆”等汉字返回空字符串,因为根据《汉语拼音方案》, ywü (yu) 都不是声母,在某些特定韵母无声母时,才加上 yw,而 ü 也有其特定规则。

如果你觉得这个给你带来了麻烦,那么也请小心一些无声母的汉字(如“啊”、“饿”、“按”、“昂”等)。 这时候你也许需要的是首字母风格(FIRST_LETTER)。

如何实现按拼音排序?

pinyin 模块提供了默认的排序方案:

const pinyin = require('pinyin');

const data = '我要排序'.split('');
const sortedData = data.sort(pinyin.compare);

如果默认的比较方法不能满足你的需求,你可以自定义 pinyinCompare 方法:

const pinyin = require('pinyin');

const data = '我要排序'.split('');

// 建议将汉字的拼音持久化存储起来。
const pinyinData = data.map(han => ({
  han: han,
  pinyin: pinyin(han)[0][0], // 可以自行选择不同的生成拼音方案和风格。
}));
const sortedData = pinyinData.sort((a, b) => {
  return a.pinyin.localeCompare(b.pinyin);
}).map(d => d.han);

node 版和 web 版有什么异同?

pinyin 目前可以同时运行在 Node 服务器端和 Web 浏览器端。 API 和使用方式完成一致。

但 Web 版较 Node 版稍简单,拼音库只有常用字部分,没有使用分词算法, 并且考虑了网络传输对词库进行了压缩处理。

由于分词和繁体中文的特性,部分情况下的结果也不尽相同。

特性 Web 版 Node 版
拼音库 常用字库。压缩、合并 完整字库。不压缩、合并
分词 没有分词 使用分词算法,多音字拼音更准确。
拼音频度排序 有根据拼音使用频度优先级排序。 同 Web 版。
繁体中文 没有繁体中文支持。 有简单的繁简汉字转换。

由于这些区别,测试不同运行环境的用例也不尽相同。

捐赠

如果这个模块有帮助到您,请 Star 这个仓库。

你也可以选择使用支付宝或微信给我捐赠:

Alipay:hotoo.cn@gmail.com, WeChat:hotoome

许可证

MIT

pinyin's People

Contributors

alephpi avatar aqiongbei avatar baijunyao avatar belinchung avatar bitdeli-chef avatar chloe88 avatar chrisyip avatar dayudodo avatar doyoonkim12345 avatar elvisqi avatar fanofxiaofeng avatar fourteendp avatar fuyuan9 avatar hczhcz avatar hotoo avatar imeoer avatar intijk avatar isayme avatar lancercomet avatar mozillazg avatar simonleeee avatar starsugar avatar ultravires avatar yanyiwu avatar zhuangya avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pinyin's Issues

注音、笔顺、部首、结构、异体字、五笔、仓颉等数据

之前自己搜集了汉字包括拼音和注音、笔顺、部首、结构分析、异体字、五笔码、仓颉码等等方面的数据,虽然多出来的部分已经不在拼音的范畴内,传上来看能不能用得上,做扩展插件或者加强版之类的。

非常感谢这个项目!

http://pan.baidu.com/s/1dFcM5iD
2017.03.30 更新了一次,修正了一些数据中的错误。

如果不需要的话请管理员删除。

「变调」“不”字的标音问题

基本信息

只有一个“不”字或“不”字后面紧接着的字为非第四声时,读音为四声bù;
例如:不(bù)法(fǎ)之徒
但是如果后面的字也是四声,需要变调,变成bú。
例如:不(bú)是;不(bú)对

但从标音角度讲,变调不应作为一种标音规则,不(bù)才是正确的标音。

合并同音字拼音库

将拼音库中同音字进行合并。多音字多个拼音算一个拼音进行合并,避免多音字的多个拼音优先级不同,无法将更常用的拼音放在前面进行匹配。

每个汉字的拼音独立存储,334KB.
完全同音的汉字进行合并存储,115KB.
所有同音字合并存储(多音字的各个读音拆分合并),81KB.

无声调模式的一个小BUG

当使用无声调的几种转换模式的时候
[āáǎà] 这几个字符会直接在结果中输出,并不会转化为a
我使用的是压缩过的字典,并工作在NodeJS环境下
这是否是一个BUG呢

npm install pinyin出问题

npm WARN engine [email protected]: wanted: {"node":"0.10.x"} (current: {"node":"0.12.0","npm":"2.5.1"})
-
> [email protected] install /home/rccoder/code/HIT-International/node_modules/pinyin/node_modules/nodejieba
> node-gyp rebuild

child_process: customFds option is deprecated, use stdio instead.
Usage: gyp_main.py [options ...] [build_file ...]

gyp_main.py: error: no such option: --no-parallel
gyp ERR! configure error 
gyp ERR! stack Error: `gyp` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onCpExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:343:16)
gyp ERR! stack     at ChildProcess.emit (events.js:110:17)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (child_process.js:1067:12)
gyp ERR! System Linux 3.19.0-18-generic
gyp ERR! command "node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/rccoder/code/HIT-International/node_modules/pinyin/node_modules/nodejieba
gyp ERR! node -v v0.12.0
gyp ERR! node-gyp -v v1.0.2
gyp ERR! not ok 
npm ERR! Linux 3.19.0-18-generic
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "pinyin"
npm ERR! node v0.12.0
npm ERR! npm  v2.5.1
npm ERR! code ELIFECYCLE

npm ERR! [email protected] install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] install script 'node-gyp rebuild'.
npm ERR! This is most likely a problem with the nodejieba package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild
npm ERR! You can get their info via:
npm ERR!     npm owner ls nodejieba
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/rccoder/code/HIT-International/npm-debug.log

“酒坊”在设置option为STYLE_FIRST_LETTER时,返回JIǔFāNG

在词组字典中找到记录后,也加上toFixed处理

function phrases_pinyin(phrases, options){
    options = extend(DEFAULT_OPTIONS, options);
  if(PHRASES_DICT.hasOwnProperty(phrases)){
      pinyins = PHRASES_DICT[phrases];
      for (var i in pinyins) {
          pinyins[i] = toFixed(pinyins[i], options.style);
      }
    return pinyins;
  }
  var py = [];
  for(var i=0,l=phrases.length; i<l; i++){
    py.push(single_pinyin(phrases[i], options));
  }
  return py;
}

npm 2.3 版本压缩包哈希有问题.. 建议重新上传..

下载的时候发现哈希值不对...

npm http GET https://registry.npmjs.org/pinyin
npm http 304 https://registry.npmjs.org/pinyin
npm http GET https://registry.npmjs.org/pinyin/-/pinyin-2.3.0.tgz
npm http 200 https://registry.npmjs.org/pinyin/-/pinyin-2.3.0.tgz
npm ERR! Error: shasum check failed for C:\Users\ADMINI~1\AppData\Local\Temp\npm-10888-PvyKzWjp\1400
305010327-0.3144137696363032\tmp.tgz
npm ERR! Expected: c33d179d6edebee2a82c06f8c7e2750f0ec080df
npm ERR! Actual:   26ddf7356672b9aa8810f88a6e977eca85d39906
npm ERR! From:     https://registry.npmjs.org/pinyin/-/pinyin-2.3.0.tgz

0套转换失败

当包含"数字+套"字时 前者无法正确转换为缩写和拼音
例如:
0套价 -> 0套j 而不是 0tj

localeCompare 研究及相关算法尝试

建立在 localeCompare() 以中文拼音顺序排序的基础上(如果这个可靠性不能保证,崩塌)。

  1. 排序后,每个相同的拼音序列只保存第一个字及其拼音作为关键字,得到有序的关键字序列
  2. 二分法,将目标汉字 A 与排序后的关键字序列 B 进行 localeCompare() 比较,找到 A 在 B 序列中的位置,并由此得到 A 的拼音。

缺陷

  • localeCompare() 可靠性问题,及 locale 参数支持程度。
  • 无法支持多音字。

参考

npm shasum 错误

http://registry.npmjs.org/pinyin/2.3.1 上声明的是 "shasum": "5e2937083d2ab706331ed68a07ce9e4c691b8963" , 但下载下来实际是

$ shasum pinyin-2.3.1.tgz 03ec304233ddbc76b94bf1c2e11c3f328881d224 pinyin-2.3.1.tgz
$ npm install pinyin
npm http GET https://registry.npmjs.org/pinyin
npm http 200 https://registry.npmjs.org/pinyin
npm http GET https://registry.npmjs.org/pinyin/-/pinyin-2.3.1.tgz
npm http 200 https://registry.npmjs.org/pinyin/-/pinyin-2.3.1.tgz
npm ERR! Error: shasum check failed for /var/folders/5h/x4h0t8rs6w300g_wnzbx47km0000gn/T/npm-20912-4Ei4eW63/1401180705236-0.6836859425529838/tmp.tgz
npm ERR! Expected: 5e2937083d2ab706331ed68a07ce9e4c691b8963
npm ERR! Actual:   03ec304233ddbc76b94bf1c2e11c3f328881d224
npm ERR!     at /Users/mk2/node_modules/npm/node_modules/sha/index.js:38:8
npm ERR!     at ReadStream.<anonymous> (/Users/mk2/node_modules/npm/node_modules/sha/index.js:85:7)
npm ERR!     at ReadStream.EventEmitter.emit (events.js:126:20)
npm ERR!     at _stream_readable.js:895:16
npm ERR!     at process._tickCallback (node.js:339:11)
npm ERR! If you need help, you may report this *entire* log,
npm ERR! including the npm and node versions, at:
npm ERR!     <http://github.com/npm/npm/issues>

npm ERR! System Darwin 13.2.0
npm ERR! command "node" "/Users/mk2/node_modules/.bin/npm" "install" "pinyin"
npm ERR! cwd /private/tmp
npm ERR! node -v v0.11.12
npm ERR! npm -v 1.4.6
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /private/tmp/npm-debug.log
npm ERR! not ok code 0

是否尝试使用Redis或Mongodb?

测试了下虽然性能不错,但对于经常要查询拼音的应用,最好还是后端有个DB异步查询。是否可以加入Redis或Mongodb做为字典存储的功能?

error.log is not gitignored or npmignored

This file is large 8029.931 kb but
https://github.com/hotoo/pinyin/blob/master/tools/dict/error.log

Last login: Mon Feb 17 11:56:33 on ttys004
[hotoo@hotoo node-pinyin]$make dict-node
404, Not Matched. 0x3404 㐄 http://www.zdic.net/z/e/js/3404.htm
404, Not Matched. 0x3403 㐃 http://www.zdic.net/z/e/js/3403.htm
404, Not Matched. 0x3415 㐕 http://www.zdic.net/z/e/js/3415.htm
404, Not Matched. 0x341b 㐛 http://www.zdic.net/z/e/js/341b.htm
404, Not Matched. 0x3420 㐠 http://www.zdic.net/z/e/js/3420.htm
404, Not Matched. 0x3427 㐧 http://www.zdic.net/z/e/js/3427.htm
404, Not Matched. 0x342a 㐪 http://www.zdic.net/z/e/js/342a.htm
404, Not Matched. 0x342e 㐮 http://www.zdic.net/z/e/js/342e.htm
404, Not Matched. 0x342f 㐯 http://www.zdic.net/z/e/js/342f.htm
404, Not Matched. 0x3434 㐴 http://www.zdic.net/z/e/js/3434.htm
404, Not Matched. 0x3435 㐵 http://www.zdic.net/z/e/js/3435.htm
404, Not Matched. 0x3436 㐶 http://www.zdic.net/z/e/js/3436.htm
404, Not Matched. 0x343d 㐽 http://www.zdic.net/z/e/js/343d.htm
404, Not Matched. 0x343f 㐿 http://www.zdic.net/z/e/js/343f.htm
404, Not Matched. 0x3441 㑁 http://www.zdic.net/z/e/js/3441.htm
404, Not Matched. 0x3446 㑆 http://www.zdic.net/z/e/js/3446.htm
404, Not Matched. 0x344c 㑌 http://www.zdic.net/z/e/js/344c.htm
404, Not Matched. 0x3450 㑐 http://www.zdic.net/z/e/js/3450.htm

At least it's not ok to be put into npm module. I think it's better to remove it from the repo:
http://dalibornasevic.com/posts/2-permanently-remove-files-and-folders-from-git-repo

计划扩充 Web 版拼音库

Web 版目前是根据国家教育部发布的 《现代汉语常用字表》 收录的 2500个常用字,和 1000个次常用字表来做的字典,这导致很多不在常用和次常用字表中,但仍然挺常用的字无法进行转换。

例如 , #29 中提到的 , ,以及 #26

考虑到实用性,计划扩充 Web 版的拼音库:

  • 增加常用地名用字。
  • 增加常用姓氏用字。

如果有需要,可以回复本 issue,或者提交新的 issue 来讨论其他的非常用字是否默认添加到拼音库中。

另外你也可以自行处理这些非常用字。

参考

nodejs里内存占用巨大

qq20140825-1
如图,在nodeJS里引入拼音库后node进程内存使用200M,去掉后变成100M,heap里显示占内存(图片里显示占用60M)的主要是一些常用的成语、pinyin里的node_modules里的object,请问有没有什么可以优化的办法?

有不依赖Seajs的版本吗?

有不依赖Seajs的版本或者编译选项嗎?
或者node版本做成像别的库那种兼容web和node的方式也成.现在node版本直接外部依赖segment,所以也不太容易手工改成web版本.

建议提供一个不包含分词的npm包

现在依赖的node-jieba需要node-gyp,在Windows环境中非常麻烦。实际上很多应用可能并不需要分词,是否可以考虑提供一个不包含分词的npm包?比如名为pinyin-lite之类的。

非常感谢!

github page被打包到npm里 -> _site目录

total 88
drwxr-xr-x  17 fdhuang  staff   578B Nov 19 13:36 .
drwxr-xr-x   4 fdhuang  staff   136B Nov 19 13:36 ..
-rw-r--r--   1 fdhuang  staff   5.0K Aug 23 08:29 .eslintrc
-rw-r--r--   1 fdhuang  staff    56B Oct 22  2014 .npmignore
-rw-r--r--   1 fdhuang  staff   2.4K Aug 25 09:33 CHANGELOG.md
-rw-r--r--   1 fdhuang  staff   1.9K Aug 25 09:36 Makefile
-rw-r--r--   1 fdhuang  staff   4.4K Aug  4 08:42 README-us_EN.md
-rw-r--r--   1 fdhuang  staff   5.0K Aug  4 08:42 README.md
drwxr-xr-x  17 fdhuang  staff   578B Nov 19 13:35 _site
drwxr-xr-x   3 fdhuang  staff   102B Aug 24 10:12 bin
drwxr-xr-x   6 fdhuang  staff   204B Nov 19 13:35 coverage
drwxr-xr-x   3 fdhuang  staff   102B Jun  9 07:44 dist
drwxr-xr-x   3 fdhuang  staff   102B Aug 24 10:12 examples
-rw-r--r--   1 fdhuang  staff    43B Aug 24 10:12 index.js
drwxr-xr-x   4 fdhuang  staff   136B Nov 19 13:36 node_modules
-rw-r--r--   1 fdhuang  staff   1.9K Nov 19 13:36 package.json
drwxr-xr-x   9 fdhuang  staff   306B Nov 19 13:36 src

转换速度很慢

nodejs下,转换四个汉字比如“新建文档”需要1秒左右,有提高的空间吗
code: wordsGroups = pinyin(hanzi, { heteronym: true, style: pinyin.STYLE_NORMAL })

chao2 yang2不能获得“朝阳”的汉字匹配

因为pinyin/src/phrases-dict.js中有这一航
···
"朝阳": [["zhāo"], ["yáng"]],
···

所以chao2 yang2不能找到“朝阳”,只有zhao yang才能找到“朝阳”。

**有好几个城市都有“朝阳区”,习惯上这些区都被叫做chao2 yang2区,而不是zhao1 yang2区。

删掉这一行能解决这个问题。

算法优化尝试

@TooBug 有很好的一个 PR #26 ,这个 PR 其实早已经无法合并进来了,但是我一直没舍得关掉,因为有很好的参考价值。

  • 二进制字典。
  • 二分法查找。

我计划近期也尝试更新使用这两者,另外其他的一些技术(比如偏移量)也会进行尝试。

如果尝试的结果满足甚至超出预期,会合并且升级版本到 3.0.x

目前 @TooBug/pinyin 是每个汉字转换都会 fs.openSync 二进制字典,我计划只打开一次,必要的话直接装载到内存(不知道和现在直接装载整个 JSON 字典差异有多大)。

谢谢 @TooBug,并 Close #26.

Electron 下报错。

screen shot 2015-11-19 at 08 52 23

错误代码:

if(isNode){
  // 拼音词库,node 版无需使用压缩合并的拼音库。
  PINYIN_DICT = module["require"]("./dict-zi");
}

fix broken syntax errors in phrases-dict.js and dict-ci.js

> pinyin = require('pinyin');
{ [Function: pinyin]
  STYLE_NORMAL: 0,
  STYLE_TONE: 1,
  STYLE_TONE2: 2,
  STYLE_INITIALS: 3,
  STYLE_FIRST_LETTER: 4 }
> pinyin('经济'); // prints invalid pinyin below
[ [ 'jīngj&igrave;' ] ]
>


## here's a grep of invalid pinyin lookups
Darwin 2 kaizhu@kais-MacBook-Air:/tmp/node_modules/pinyin $ shGrep grave
./src/phrases-dict.js:7196:"节烈": [["ji&eacute;li&egrave;"]],
./src/phrases-dict.js:7517:"经济": [["jīngj&igrave;"]],
./src/phrases-dict.js:10697:"强韧": [["qi&aacute;ngr&egrave;n"]],
./src/phrases-dict.js:12421:"说话": [["shuōhu&agrave;"]],
./src/phrases-dict.js:15890:"怨毒": [["yu&agrave;nd&uacute;"]],
./src/phrases-dict.js:17052:"撰著": [["zhu&agrave;nzh&ugrave;"]],
./tools/dict/dict-ci.js:8648:"额定": ["&eacute;d&igrave;ng"],
./tools/dict/dict-ci.js:17387:"节烈": ["ji&eacute;li&egrave;"],
./tools/dict/dict-ci.js:18056:"经济": ["jīngj&igrave;"],
./tools/dict/dict-ci.js:18652:"跼促": ["j&uacute;c&ugrave;"],
./tools/dict/dict-ci.js:21645:"凌驾": ["l&iacute;ngji&agrave;"],
./tools/dict/dict-ci.js:24657:"涅槃": ["ni&egrave;p&aacute;n"],
./tools/dict/dict-ci.js:25698:"嘌呤": ["pi&agrave;ol&iacute;ng"],
./tools/dict/dict-ci.js:27018:"强韧": ["qi&aacute;ngr&egrave;n"],
./tools/dict/dict-ci.js:28866:"三废": ["sān-f&egrave;i"],
./tools/dict/dict-ci.js:32093:"说话": ["shuōhu&agrave;"],
./tools/dict/dict-ci.js:35466:"未定": ["w&egrave;id&igrave;ng"],
./tools/dict/dict-ci.js:42335:"怨毒": ["yu&agrave;nd&uacute;"],
./tools/dict/dict-ci.js:44660:"众望": ["zh&ograve;ngw&agrave;ng"],
./tools/dict/dict-ci.js:45315:"撰著": ["zhu&agrave;nzh&ugrave;"],
./tools/dict/dict-ci.js:47525:"报话机": ["b&agrave;ohu&agrave;jī"],
./tools/dict/dict-ci.js:62077:"警惕性": ["jǐngt&igrave;x&igrave;ng"],
Darwin 2 kaizhu@kais-MacBook-Air:/tmp/node_modules/pinyin $ 

去除非多音字词组的拼音库

有一个优化点,词组的拼音库,可以只保留『有多音字的词组』,没有多音字的词组以普通的拼音转换即可。
不过工作量会比较大,希望大家能帮助找出有多音字的词组。 :)
#19 #20

内存问题

您好,在使用您的pinyin包时遇到了一些问题,您的包好像特别消耗内存,也可能是我们使用不对导致了内存的泄露,也可能是其他原因。下面是截图,前四个都是pinyin包的内存快照,我就是想问,您的包是否会导致内存泄露。
内存快照

设置style失效 ?

image

image

为何我设置style都没有,我想不带音标的,仅首字母的,返回的结果都是一样的

没有实例页面

好像没有实例页面。我的项目不是用nodejs写,该怎么引用,求指教

在Centos 6.5+iojs 2.2.1 环境下npm install pinyin出现错误,还请帮助看看

 [root@pan miniyunjs]# npm install pinyin 
npm WARN engine [email protected]: wanted: {"node":"0.10.x"} (current: > {"node":"2.2.1","npm":"2.11.0"})
 |
[email protected] install /home/data/miniyunjs/node_modules/pinyin/node_modules/nodejieba
node-gyp rebuild

make: Entering directory `/home/data/miniyunjs/node_modules/pinyin/node_modules/nodejieba/build'
  CXX(target) Release/obj.target/segment/src/segment.o
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:66,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/char_traits.h:41,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/string:42,
                 from ../node_modules/nan/nan.h:37,
                 from ../src/utils.h:6,
                 from ../src/mix_segment.h:4,
                 from ../src/segment.cpp:1:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(_U1&&, _U2&&) [with _U1 = size_t&, _U2 = long int, _T1 = long unsigned int, _T2 = const CppJieba::DictUnit*]’:
../src/CppJieba/Trie.hpp:101:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_pair.h:90: 错误:从类型‘long int’到类型‘const CppJieba::DictUnit*’的转换无效
make: *** [Release/obj.target/segment/src/segment.o] 错误 1
make: Leaving directory `/home/data/miniyunjs/node_modules/pinyin/node_modules/nodejieba/build'
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/home/data/iojs-v2.2.1-linux-x64/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:269:23)
gyp ERR! stack     at emitTwo (events.js:87:13)
gyp ERR! stack     at ChildProcess.emit (events.js:172:7)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:199:12)
gyp ERR! System Linux 2.6.32-504.16.2.el6.x86_64
gyp ERR! command "/home/data/iojs-v2.2.1-linux-x64/bin/iojs" "/home/data/iojs-v2.2.1-linux-x64/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/data/miniyunjs/node_modules/pinyin/node_modules/nodejieba
gyp ERR! node -v v2.2.1
gyp ERR! node-gyp -v v1.0.3
gyp ERR! not ok 
npm ERR! Linux 2.6.32-504.16.2.el6.x86_64
npm ERR! argv "/home/data/iojs-v2.2.1-linux-x64/bin/iojs" "/usr/local/bin/npm" "install" "pinyin"
npm ERR! node v2.2.1
npm ERR! npm  v2.11.0
npm ERR! code ELIFECYCLE

npm ERR! [email protected] install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] install script 'node-gyp rebuild'.
npm ERR! This is most likely a problem with the nodejieba package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild
npm ERR! You can get their info via:
npm ERR!     npm owner ls nodejieba
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/data/miniyunjs/npm-debug.log

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.