|
| 1 | +/*! |
| 2 | + * pinyin-separate v1.0.6 (March 18th 2019) |
| 3 | + * Separates a string containing pinyin notation (with diacritics) into an array of pinyin syllables, even if there are no spaces in between. |
| 4 | + * |
| 5 | + * https://github.com/Connum/npm-pinyin-separate#readme |
| 6 | + * |
| 7 | + * @author Connum <[email protected]> |
| 8 | + * @license MIT |
| 9 | + */ |
| 10 | +(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.pinyinSeparate = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +// @ts-check |
| 14 | + |
| 15 | +var vowels = 'aāáǎăàeēéěĕèiīíǐĭìoōóǒŏòuūúǔŭùüǖǘǚǚü̆ǜvv̄v́v̆v̌v̀'; |
| 16 | +var tones = 'āáǎăàēéěĕèīíǐĭìōóǒŏòūúǔŭùǖǘǚǚü̆ǜv̄v́v̆v̌v̀'; |
| 17 | +function separate(pinyin) { |
| 18 | + return pinyin.replace(/'/g, ' ') // single quote used for separation |
| 19 | + .replace(new RegExp('([' + vowels + '])([^' + vowels + 'nr])', 'gi'), '$1 $2') // This line does most of the work |
| 20 | + .replace(new RegExp('(\\w)([csz]h)', 'gi'), '$1 $2') // double-consonant initials |
| 21 | + .replace(new RegExp('([' + vowels + ']{2}(ng? )?)([^\\snr])', 'gi'), '$1 $3') // double-vowel finals |
| 22 | + .replace(new RegExp('([' + vowels + ']{2})(n[' + vowels + '])', 'gi'), '$1 $2') // double-vowel followed by n initial |
| 23 | + .replace(new RegExp('(n)([^' + vowels + 'vg])', 'gi'), '$1 $2') // cleans up most n compounds |
| 24 | + .replace(new RegExp('([' + vowels + 'v])([^' + vowels + '\\w\\s])([' + vowels + 'v])', 'gi'), '$1 $2$3') // assumes correct Pinyin (i.e., no missing apostrophes) |
| 25 | + .replace(new RegExp('([' + vowels + 'v])(n)(g)([' + vowels + 'v])', 'gi'), '$1$2 $3$4') // assumes correct Pinyin, i.e. changan = chan + gan |
| 26 | + .replace(new RegExp('([gr])([^' + vowels + '])', 'gi'), '$1 $2') // fixes -ng and -r finals not followed by vowels |
| 27 | + .replace(new RegExp('([^eēéěĕè\\w\\s])(r)', 'gi'), '$1 $2') // r an initial, except in er |
| 28 | + .replace(new RegExp('([^\\w\\s])([eēéěĕè]r)', 'gi'), '$1 $2') // er |
| 29 | + .replace(/\s{2,}/g, ' ') // remove double-spaces |
| 30 | + ; |
| 31 | +} |
| 32 | + |
| 33 | +module.exports = function separatePinyinInSyllables(pinyin, separateBySpaces) { |
| 34 | + if (!pinyin) { |
| 35 | + return []; |
| 36 | + } |
| 37 | + |
| 38 | + if (separateBySpaces) { |
| 39 | + return pinyin.split(String.fromCharCode(160)); |
| 40 | + } |
| 41 | + |
| 42 | + var pinyinSeparated = separate(pinyin).split(' '); |
| 43 | + var newPinyin = []; |
| 44 | + |
| 45 | + pinyinSeparated.forEach(function (p) { |
| 46 | + var totalTones = 1; |
| 47 | + var pregMatch = p.match(new RegExp('([' + tones + '])', 'g')); |
| 48 | + if (pregMatch) { |
| 49 | + totalTones = pregMatch.length; |
| 50 | + } |
| 51 | + |
| 52 | + if (p.length > 4 || totalTones > 1) { |
| 53 | + separate(p).split(' ').forEach(function (newP) { |
| 54 | + pregMatch = newP.match(new RegExp('([' + tones + '])', 'g')); |
| 55 | + if (pregMatch) { |
| 56 | + totalTones = pregMatch.length; |
| 57 | + } |
| 58 | + |
| 59 | + if (newP.length > 4 || totalTones > 1) { |
| 60 | + separate(newP).split(' ').forEach(function (newP2) { |
| 61 | + newPinyin.push(newP2.trim()); |
| 62 | + }); |
| 63 | + } else { |
| 64 | + newPinyin.push(newP.trim()); |
| 65 | + } |
| 66 | + }); |
| 67 | + } else { |
| 68 | + newPinyin.push(p.trim()); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + return newPinyin; |
| 73 | +}; |
| 74 | +},{}],2:[function(require,module,exports){ |
| 75 | +'use strict'; |
| 76 | + |
| 77 | +Object.defineProperty(exports, "__esModule", { |
| 78 | + value: true |
| 79 | +}); |
| 80 | + |
| 81 | +var _separatePinyinInSyllables = require('./helpers/separate-pinyin-in-syllables'); |
| 82 | + |
| 83 | +var _separatePinyinInSyllables2 = _interopRequireDefault(_separatePinyinInSyllables); |
| 84 | + |
| 85 | +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
| 86 | + |
| 87 | +var defaultOptions = { |
| 88 | + byNbsp: false |
| 89 | +}; |
| 90 | + |
| 91 | +var pinyinSeparate = function pinyinSeparate(pinyIn) { |
| 92 | + var optionsArg = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultOptions; |
| 93 | + |
| 94 | + var options = optionsArg; |
| 95 | + if (options !== defaultOptions) { |
| 96 | + options = Object.assign({}, defaultOptions, options); |
| 97 | + } |
| 98 | + |
| 99 | + return (0, _separatePinyinInSyllables2.default)(pinyIn, options.byNbsp); |
| 100 | +}; |
| 101 | + |
| 102 | +exports.default = pinyinSeparate; |
| 103 | + |
| 104 | +// export { pinyinSeparate }; |
| 105 | + |
| 106 | +module.exports = exports.default; |
| 107 | +},{"./helpers/separate-pinyin-in-syllables":1}]},{},[2])(2) |
| 108 | +}); |
0 commit comments