PinyinBase.js 9.6 KB

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