Skip to content

Instantly share code, notes, and snippets.

@pierophp
Last active August 16, 2016 18:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save pierophp/bb84754e5de43b4f406aaf0bda8a007e to your computer and use it in GitHub Desktop.
var separatePinyinInSyllables = function (pinyin) {
var vowels = 'aāáǎàeēéěèiīíǐìoōóǒòuūúǔù';
return pinyin
.replace(new RegExp('([' + vowels + '])([^' + vowels + 'nr\w\s])'), '$1 $2') // This line does most of the work
.replace(new RegExp('(\w)([csz]h)'), '$1 $2') // double-consonant initials
.replace(new RegExp('(n)([^' + vowels + 'vg\w\s])'), '$1 $2') // cleans up most n compounds
.replace(new RegExp('([' + vowels + 'v])([^' + vowels + '\w\s])([' + vowels + 'v])'), '$1 $2$3') // assumes correct Pinyin (i.e., no missing apostrophes)
.replace(new RegExp('([' + vowels + 'v])(n)(g)([' + vowels + 'v])'), '$1$2 $3$4') // assumes correct Pinyin, i.e. changan = chan + gan
.replace(new RegExp('([gr])([^' + vowels + '])'), '$1 $2') // fixes -ng and -r finals not followed by vowels
.replace(new RegExp('([^e\w\s])(r)'), '$1 $2'); // r an initial, except in er
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment