{"id":1145,"date":"2008-06-21T16:58:58","date_gmt":"2008-06-21T08:58:58","guid":{"rendered":"https:\/\/pinyin.info\/news\/?p=1145"},"modified":"2008-06-21T14:18:49","modified_gmt":"2008-06-21T06:18:49","slug":"separating-pinyin-syllables-php-code","status":"publish","type":"post","link":"https:\/\/pinyin.info\/news\/2008\/separating-pinyin-syllables-php-code\/","title":{"rendered":"separating Pinyin syllables: PHP code"},"content":{"rendered":"<p>A few weeks ago I had someone write to ask if I had a script that can divide Pinyin texts into their individual syllables. It so happens that I do have something that does just that. Since I sent out that bit of code, I might as well make it available to everyone (<a href=\"http:\/\/www.gnu.org\/licenses\/gpl.html\">GNU GPL<\/a>, and links back to Pinyin.Info are always appreciated). <\/p>\n<p>It has lots of regular expressions, to make the code nice and compact. I&#8217;ve added comments for clarity. <\/p>\n<p><code style=\"color: blue;\">##############################<br \/>\n### SEPARATE THE SYLLABLES<br \/>\n##############################<br \/>\n\/\/ In the lines below, <span style=\"font-family: monospace;\">\\s<\/span> means <em>space<\/em><br \/>\n\/\/ This program assumes that <em>&uuml;<\/em> is written as <em>v<\/em><br \/>\n\/\/ The <span style=\"font-family: monospace;\">i<\/span> at the end of a line means <em>case insensitive<\/em><br \/>\n\/\/ <span style=\"font-family: monospace;\">\\W<\/span> is a single, non-word character (e.g., punctuation)<\/p>\n<p>$search = array (\"'([aeiouv])([^aeiounr\\W\\s])'i\",    \/\/ This line does most of the work<br \/>\n       \"'(\\w)([csz]h)'i\",        \/\/ double-consonant initials<br \/>\n       \"'(n)([^aeiouvg\\W\\s])'i\",     \/\/ cleans up most <em>n<\/em> compounds<br \/>\n       \"'([aeiuov])([^aeiou\\W\\s])([aeiuov])'i\", \/\/ assumes correct Pinyin (i.e., no missing <a href=\"https:\/\/pinyin.info\/romanization\/hanyu\/apostrophes.html\" style=\"text-decoration: underline;\" >apostrophes<\/a>)<br \/>\n       \"'([aeiouv])(n)(g)([aeiouv])'i\",    \/\/ assumes correct Pinyin, i.e. <em>changan<\/em> = <em>chan<\/em> + <em>gan<\/em><br \/>\n       \"'([gr])([^aeiou\\W\\s])'i\",  \/\/     fixes <em>-ng<\/em> and <em>-r<\/em> finals not followed by vowels<br \/>\n       \"'([^e\\W\\s])(r)'i\",    \/\/    <em>r<\/em> an initial, except in <em>er<\/em><br \/>\n       );<\/p>\n<p>$replace = array (\"\\\\1 \\\\2\",<br \/>\n                 \"\\\\1 \\\\2\",<br \/>\n                 \"\\\\1 \\\\2\",<br \/>\n         \"\\\\1 \\\\2\\\\3\",<br \/>\n         \"\\\\1\\\\2 \\\\3\\\\4\",<br \/>\n                 \"\\\\1 \\\\2\",<br \/>\n                 \"\\\\1 \\\\2\",<br \/>\n       );<\/p>\n<p>$usertext = preg_replace($search, $replace, $document);<\/p>\n<p>##############################<br \/>\n<\/code><\/p>\n<p>Since I&#8217;m always going on about the need for word parsing and <em>not<\/em> separating Pinyin into single syllables, some of you are probably wondering just why I of all people would have ever written such code. The answer is that it&#8217;s part of my <a href=\"https:\/\/pinyin.info\/tools\/spellcheck.html\">Pinyin spell-checker<\/a>, which is only a very basic utility in that it functions by checking for theoretically correct groups of syllables rather than real words (i.e., anything composed of correctly spelled groups of syllables, minus tone marks, will pass even if that word isn&#8217;t found in a dictionary). <\/p>\n<p>Suggestions for improvements are always welcome. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few weeks ago I had someone write to ask if I had a script that can divide Pinyin texts into their individual syllables. It so happens that I do have something that does just that. Since I sent out &hellip; <a href=\"https:\/\/pinyin.info\/news\/2008\/separating-pinyin-syllables-php-code\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1145","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/pinyin.info\/news\/wp-json\/wp\/v2\/posts\/1145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pinyin.info\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pinyin.info\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pinyin.info\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pinyin.info\/news\/wp-json\/wp\/v2\/comments?post=1145"}],"version-history":[{"count":0,"href":"https:\/\/pinyin.info\/news\/wp-json\/wp\/v2\/posts\/1145\/revisions"}],"wp:attachment":[{"href":"https:\/\/pinyin.info\/news\/wp-json\/wp\/v2\/media?parent=1145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pinyin.info\/news\/wp-json\/wp\/v2\/categories?post=1145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pinyin.info\/news\/wp-json\/wp\/v2\/tags?post=1145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}