Last active
August 16, 2016 18:04
-
-
Save pierophp/bb84754e5de43b4f406aaf0bda8a007e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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