PinyinBase.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.getPinyinInstance = void 0;
  7. var dict_zi_1 = __importDefault(require("./data/dict-zi"));
  8. var phrases_dict_1 = __importDefault(require("./data/phrases-dict"));
  9. var segment_web_1 = require("./segment-web");
  10. var format_1 = require("./format");
  11. var surname_1 = __importDefault(require("./data/surname"));
  12. var compound_surname_1 = __importDefault(require("./data/compound_surname"));
  13. var util_1 = require("./util");
  14. var constant_1 = require("./constant");
  15. var PinyinBase = (function () {
  16. function PinyinBase() {
  17. this.STYLE_TONE = constant_1.ENUM_PINYIN_STYLE.TONE;
  18. this.STYLE_TONE2 = constant_1.ENUM_PINYIN_STYLE.TONE2;
  19. this.STYLE_TO3NE = constant_1.ENUM_PINYIN_STYLE.TO3NE;
  20. this.STYLE_NORMAL = constant_1.ENUM_PINYIN_STYLE.NORMAL;
  21. this.STYLE_INITIALS = constant_1.ENUM_PINYIN_STYLE.INITIALS;
  22. this.STYLE_FIRST_LETTER = constant_1.ENUM_PINYIN_STYLE.FIRST_LETTER;
  23. this.STYLE_PASSPORT = constant_1.ENUM_PINYIN_STYLE.PASSPORT;
  24. this.MODE_NORMAL = constant_1.ENUM_PINYIN_MODE.NORMAL;
  25. this.MODE_SURNAME = constant_1.ENUM_PINYIN_MODE.SURNAME;
  26. }
  27. PinyinBase.prototype.pinyin = function (hans, options) {
  28. if (typeof hans !== "string") {
  29. return [];
  30. }
  31. var opt = (0, util_1.convertUserOptions)(options);
  32. var pys;
  33. if (opt.mode === constant_1.ENUM_PINYIN_MODE.SURNAME) {
  34. pys = this.surname_pinyin(hans, opt);
  35. }
  36. else {
  37. if (opt.segment) {
  38. pys = this.segment_pinyin(hans, opt);
  39. }
  40. else {
  41. pys = this.normal_pinyin(hans, opt);
  42. }
  43. }
  44. if (options === null || options === void 0 ? void 0 : options.compact) {
  45. pys = (0, util_1.compact)(pys);
  46. }
  47. return pys;
  48. };
  49. PinyinBase.prototype.normal_pinyin = function (hans, options) {
  50. var pys = [];
  51. var nohans = "";
  52. for (var i = 0, l = hans.length; i < l; i++) {
  53. var words = hans[i];
  54. var firstCharCode = words.charCodeAt(0);
  55. if (dict_zi_1.default[firstCharCode]) {
  56. if (nohans.length > 0) {
  57. pys.push([nohans]);
  58. nohans = "";
  59. }
  60. pys.push(this.single_pinyin(words, options));
  61. }
  62. else {
  63. nohans += words;
  64. }
  65. }
  66. if (nohans.length > 0) {
  67. pys.push([nohans]);
  68. nohans = "";
  69. }
  70. return pys;
  71. };
  72. PinyinBase.prototype.single_pinyin = function (han, options) {
  73. if (typeof han !== "string") {
  74. return [];
  75. }
  76. if (han.length !== 1) {
  77. return this.single_pinyin(han.charAt(0), options);
  78. }
  79. var hanCode = han.charCodeAt(0);
  80. if (!dict_zi_1.default[hanCode]) {
  81. return [han];
  82. }
  83. var pys = dict_zi_1.default[hanCode].split(",");
  84. if (!options.heteronym) {
  85. return [(0, format_1.toFixed)(pys[0], options.style)];
  86. }
  87. var py_cached = {};
  88. var pinyins = [];
  89. for (var i = 0, l = pys.length; i < l; i++) {
  90. var py = (0, format_1.toFixed)(pys[i], options.style);
  91. if ((0, util_1.hasKey)(py_cached, py)) {
  92. continue;
  93. }
  94. py_cached[py] = py;
  95. pinyins.push(py);
  96. }
  97. return pinyins;
  98. };
  99. PinyinBase.prototype.segment = function (hans, segmentType) {
  100. return (0, segment_web_1.segment)(hans, segmentType);
  101. };
  102. PinyinBase.prototype.segment_pinyin = function (hans, options) {
  103. var phrases = this.segment(hans, options.segment);
  104. var pys = [];
  105. var nohans = "";
  106. for (var i = 0, l = phrases.length; i < l; i++) {
  107. var words = phrases[i];
  108. var firstCharCode = words.charCodeAt(0);
  109. if (dict_zi_1.default[firstCharCode]) {
  110. if (nohans.length > 0) {
  111. pys.push([nohans]);
  112. nohans = "";
  113. }
  114. var newPys = words.length === 1
  115. ? this.normal_pinyin(words, options)
  116. : this.phrases_pinyin(words, options);
  117. if (options.group) {
  118. pys.push(this.groupPhrases(newPys));
  119. }
  120. else {
  121. pys = pys.concat(newPys);
  122. }
  123. }
  124. else {
  125. nohans += words;
  126. }
  127. }
  128. if (nohans.length > 0) {
  129. pys.push([nohans]);
  130. nohans = "";
  131. }
  132. return pys;
  133. };
  134. PinyinBase.prototype.phrases_pinyin = function (phrases, options) {
  135. var py = [];
  136. if ((0, util_1.hasKey)(phrases_dict_1.default, phrases)) {
  137. phrases_dict_1.default[phrases].forEach(function (item, idx) {
  138. py[idx] = [];
  139. if (options.heteronym) {
  140. item.forEach(function (py_item, py_index) {
  141. py[idx][py_index] = (0, format_1.toFixed)(py_item, options.style);
  142. });
  143. }
  144. else {
  145. py[idx][0] = (0, format_1.toFixed)(item[0], options.style);
  146. }
  147. });
  148. }
  149. else {
  150. for (var i = 0, l = phrases.length; i < l; i++) {
  151. py.push(this.single_pinyin(phrases[i], options));
  152. }
  153. }
  154. return py;
  155. };
  156. PinyinBase.prototype.groupPhrases = function (phrases) {
  157. if (phrases.length === 1) {
  158. return phrases[0];
  159. }
  160. var grouped = (0, util_1.combo)(phrases);
  161. return grouped;
  162. };
  163. PinyinBase.prototype.surname_pinyin = function (hans, options) {
  164. return this.compound_surname(hans, options);
  165. };
  166. PinyinBase.prototype.compound_surname = function (hans, options) {
  167. var len = hans.length;
  168. var prefixIndex = 0;
  169. var result = [];
  170. for (var i = 0; i < len; i++) {
  171. var twowords = hans.substring(i, i + 2);
  172. if ((0, util_1.hasKey)(compound_surname_1.default, twowords)) {
  173. if (prefixIndex <= i - 1) {
  174. result = result.concat(this.single_surname(hans.substring(prefixIndex, i), options));
  175. }
  176. var pys = compound_surname_1.default[twowords].map(function (item) {
  177. return item.map(function (ch) { return (0, format_1.toFixed)(ch, options.style); });
  178. });
  179. result = result.concat(pys);
  180. i = i + 2;
  181. prefixIndex = i;
  182. }
  183. }
  184. result = result.concat(this.single_surname(hans.substring(prefixIndex, len), options));
  185. return result;
  186. };
  187. PinyinBase.prototype.single_surname = function (hans, options) {
  188. var result = [];
  189. for (var i = 0, l = hans.length; i < l; i++) {
  190. var word = hans.charAt(i);
  191. if ((0, util_1.hasKey)(surname_1.default, word)) {
  192. var pys = surname_1.default[word].map(function (item) {
  193. return item.map(function (ch) { return (0, format_1.toFixed)(ch, options.style); });
  194. });
  195. result = result.concat(pys);
  196. }
  197. else {
  198. result.push(this.single_pinyin(word, options));
  199. }
  200. }
  201. return result;
  202. };
  203. PinyinBase.prototype.compare = function (hanA, hanB) {
  204. var pinyinA = this.pinyin(hanA);
  205. var pinyinB = this.pinyin(hanB);
  206. return String(pinyinA).localeCompare(String(pinyinB));
  207. };
  208. PinyinBase.prototype.compact = function (pys) {
  209. return (0, util_1.compact)(pys);
  210. };
  211. return PinyinBase;
  212. }());
  213. exports.default = PinyinBase;
  214. function getPinyinInstance(py) {
  215. var pinyin = py.pinyin.bind(py);
  216. pinyin.compare = py.compare.bind(py);
  217. pinyin.compact = py.compact.bind(py);
  218. pinyin.STYLE_TONE = constant_1.ENUM_PINYIN_STYLE.TONE;
  219. pinyin.STYLE_TONE2 = constant_1.ENUM_PINYIN_STYLE.TONE2;
  220. pinyin.STYLE_TO3NE = constant_1.ENUM_PINYIN_STYLE.TO3NE;
  221. pinyin.STYLE_NORMAL = constant_1.ENUM_PINYIN_STYLE.NORMAL;
  222. pinyin.STYLE_INITIALS = constant_1.ENUM_PINYIN_STYLE.INITIALS;
  223. pinyin.STYLE_FIRST_LETTER = constant_1.ENUM_PINYIN_STYLE.FIRST_LETTER;
  224. pinyin.STYLE_PASSPORT = constant_1.ENUM_PINYIN_STYLE.PASSPORT;
  225. pinyin.MODE_NORMAL = constant_1.ENUM_PINYIN_MODE.NORMAL;
  226. pinyin.MODE_SURNAME = constant_1.ENUM_PINYIN_MODE.SURNAME;
  227. return pinyin;
  228. }
  229. exports.getPinyinInstance = getPinyinInstance;
  230. //# sourceMappingURL=PinyinBase.js.map