<p>As indicated in the comments, <em>correct</em> pīnyīn is unambiguously convertible into syllables, because of the apostrophe rule. However, expect people complaining about their wrong pīnyīn not being parsed correctly.</p>
<p>You have to split in front of</p>
<ol>
<li>every normal consonant and &quot;yw&quot;, <code>&quot;bpmfdtlkjqxzcsryw&quot;</code></li>
<li>every <code>&quot;h&quot;</code> not preceeded by <code>&quot;zcs&quot;</code></li>
<li>every <code>&quot;'&quot;</code>, deleting the <code>&quot;'&quot;</code></li>
<li>every <code>&quot;n&quot;</code> or <code>&quot;g&quot;</code> followed by a vowel. (This is the worst part)</li>
</ol>
<p>This is all doable by regex.</p>
<pre><code>import re

def split_pinyin(unsplit: str) -&gt; str:
   &quot;&quot;&quot;Split pinyin into syllables.&quot;&quot;&quot;
   normal = &quot;[bpmfdtlkjqxzcsryw]&quot;
   h = r&quot;(?&lt;![zcs])h&quot;
   ng = r&quot;[ng][aeiouüáāàǎéèēěíìīǐóòōǒúùūǔǘǜǖǚ]&quot;
   unsplit = unsplit.replace(&quot;'&quot;, &quot; &quot;)
   pattern = r&quot;(?&lt;=\w)(&quot; + &quot;|&quot;.join((normal, h, ng)) + &quot;)&quot;
   return re.sub(pattern, r&quot; \1&quot;, unsplit)

for test in &quot;&quot;&quot;tàngsuǒ
débùchángshī
fēngtǔrénqíng
huǎngrán dà wù
jiànjí'ānxiāng
ānníng, xiāng'ài, guàngài&quot;&quot;&quot;.split(&quot;\n&quot;):
    print(split_pinyin(test))
</code></pre>
<p><strong>Edit:</strong> The first version forgot about &quot;y&quot;, &quot;w&quot;.</p>

CC BY-SA 4.0
2024-11-26T16:18:05.787Z
Great answer! Great Python! Well done! Love your regex!
Pedroski
CC BY-SA 4.0
2024-11-26T19:24:43.517Z
@Pedroski Just in case you already copied it: I forgot about y,w.
Dodezv
CC BY-SA 4.0
2024-11-26T19:49:58.397Z
Thank you! I did copy! I will add y and w! I am wrestling with a cv2 thing to find dices in an image and get the number on each die. I thought it would be easy, but turns out more complicated! Thanks again!
Pedroski
CC BY-SA 4.0
2024-11-26T10:23:55.950Z
2024-11-26T19:23:44.393Z
2024-11-26T19:23:44.393Z
Dodezv
<p>因为你没有提供你的python代码，我也没法看你的代码，所以在这里我给出一个编程方面的思路：</p>
<p>首先把数组a全部初始化为0，然后以逆序的方式读取所有的字符到一个数组a中，比如 tàngsuǒ，读取后数组中a[0]是ǒ，a[1]是u，a[2]是s。。。a[6]是t。</p>
<p>再初始化一个数组b，内容全部是0，然后开始复制数组a的元素到数组b，复制过程中判断数组a，如果是元音，比如a[2]是s，则把s复制到b[2]后，再在后面加一个空格到b[3]中。。。，当然几个特殊的发音也需要处理，读取数组a中最后一个元素后不加空格。</p>
<p>最后再把数组b以逆序的方式，就是以b[7]，b[6]。。。b[0]的顺序，打印出来，就得到你想要的结果了。</p>

CC BY-SA 4.0
2024-11-26T05:22:43.360Z
2024-11-26T05:22:43.360Z
Tom
<p>The objective of the project: Splitting a given string containing pinyin into its syllables.</p>
<p>I would do it with the following steps. There could be exceptions, therefore testing is very important.</p>
<p>(1) Separate all characters.</p>
<p>(2) Combine adjacent vowels (ai, ei, ao, ou, ia, ie, iu, ua, uo, ui, ve, iao). *1</p>
<p>(3) combine adjacent consonants (ng, ch, sh, zh). *2</p>
<p>(4) combine finals with n and ng.</p>
<p>(5) combine initials and finals to form the syllables. *3</p>
<p>*1 I am not quite sure about er in (二）and (一会儿).</p>
<p>*2 combine n and g only if g is followed by a consonant or a separate symbol. The official separate symbol is ('), such as in (xi'an).</p>
<p>*3 Some syllables do not have an initial but only a final.</p>

CC BY-SA 4.0
2023-02-27T16:34:23.413Z
2023-02-27T20:20:17.263Z
2023-02-27T20:20:17.263Z
PdotWang
<h1>The question</h1>
<p>I'd like to have a function in Python for splitting a given string containing pinyin into its syllables. The following two code blocks show how the function is intended to work.</p>
<p>Input</p>
<pre><code>tàngsuǒ
débùchángshī
fēngtǔrénqíng
huǎngrán dà wù
</code></pre>
<p>Output</p>
<pre><code>tàng suǒ
fēng tǔ rén qíng
dé bù cháng shī
huǎng rán dà wù
</code></pre>
<h1>Additional notes</h1>
<p>The function should support pinyin of all Chinese characters. I emphasize this because I have noticed that some software that input pinyin can't type the syllables shown in the code block below.</p>
<p>Input</p>
<pre><code>shài
hūlüè
ǹg
ang
hng
</code></pre>
<p>Output</p>
<pre><code>shài
hū lüè
ǹg
ang
hng
</code></pre>
<p>For the record, I'm considering the pinyin that is taught in the <a href="https://www.purpleculture.net/hsk-standard-course-bs-719/" rel="nofollow noreferrer">HSK Standard Course book series</a> (i.e. pinyin of Standard Mandarin).</p>

CC BY-SA 4.0
2023-02-27T04:10:25.643Z
I don't know enough code to help on that side.  However your biggest issue is not a code one, but ambiguous pinyin.  for example mìngā could be mìng ā or mìn gā-- huànshi could be huàn shi or hu àn shi (usually written hu'àn for clarity but not always).  also basic auto pinyin issues such as wrong tone or pronunciation in the source should be considered.
zagrycha
CC BY-SA 4.0
2023-02-27T05:28:42.587Z
I agree largely with what zagrycha said, this is a coding issue and can't be easily done. You must consider the frequency of which each word is used, and the nasal tones can be confusing to your program. You can consider using a dictionary (tedious, and sadly doesn't distinguish nasal tones, but maybe solvable with an if statement because nasal with ng usually doesn't follow with another vowel). BUT largely this is not a Chinese problem but a coding problem
Wolfking
CC BY-SA 4.0
2023-02-27T06:22:56.463Z
I think you'd do okay by breaking after non-extensible pinyin finals (and just give a warning for the ambiguous cases).  Nevertheless, there's a plethora of boundary cases like capital letters (e.g. 俄罗斯 in pinyin), punctuation, numbers, English words, errors in the input, and peculiar characters like [欸](https://baike.baidu.com/item/%E6%AC%B8/2235416) and [圕](https://baike.baidu.com/item/%E5%9C%95/10177623).
Becky 李蓓
CC BY-SA 4.0
2023-02-27T20:14:39.247Z
According to Pinyin Rules, an apostrophe (隔音符号，单引号）must be inserted before a vowel (a. e. o) in case it starts a new syllable in the middel of a word, such as in (xi'an). In the word tian'anmen, you still need the (') symbol, even if there is no word tia-nan-men. (no the sound of tia).
PdotWang
CC BY-SA 4.0
2023-03-04T09:13:34.703Z
Commenting rather than answering because this isn't in Python, but [this project](https://github.com/pepebecker/pinyin-split) in TypeScript seems to be decently well-tested, and uses a list of common syllables. I agree that ambiguity would make this brittle, but maybe converting this to Python, possibly adding more common syllables, and sorting them by commonality, would get you pretty close.
Zac Anger
CC BY-SA 4.0
2024-11-26T05:05:26.877Z
Where is your python code? Show me the code please.
Tom
<pinyin>
Python function for splitting pinyin into syllables
# Python function for splitting pinyin into syllables

(Asked by: username_0 on 2023-02-27)

The question
I'd like to have a function in Python for splitting a given string containing pinyin into its syllables. The following two code blocks show how the function is intended to work.
Input
tàngsuǒ
débùchángshī
fēngtǔrénqíng
huǎngrán dà wù

Output
tàng suǒ
fēng tǔ rén qíng
dé bù cháng shī
huǎng rán dà wù

Additional notes
The function should support pinyin of all Chinese characters. I emphasize this because I have noticed that some software that input pinyin can't type the syllables shown in the code block below.
Input
shài
hūlüè
ǹg
ang
hng

Output
shài
hū lüè
ǹg
ang
hng

For the record, I'm considering the pinyin that is taught in the HSK Standard Course book series (i.e. pinyin of Standard Mandarin).

**Tags:** pinyin

> **Comments:**
> **username_1**: I don't know enough code to help on that side.  However your biggest issue is not a code one, but ambiguous pinyin.  for example mìngā could be mìng ā or mìn gā-- huànshi could be huàn shi or hu àn shi (usually written hu'àn for clarity but not always).  also basic auto pinyin issues such as wrong tone or pronunciation in the source should be considered.  
> **username_2**: I agree largely with what username_1 said, this is a coding issue and can't be easily done. You must consider the frequency of which each word is used, and the nasal tones can be confusing to your program. You can consider using a dictionary (tedious, and sadly doesn't distinguish nasal tones, but maybe solvable with an if statement because nasal with ng usually doesn't follow with another vowel). BUT largely this is not a Chinese problem but a coding problem  
> **username_3**: I think you'd do okay by breaking after non-extensible pinyin finals (and just give a warning for the ambiguous cases).  Nevertheless, there's a plethora of boundary cases like capital letters (e.g. 俄罗斯 in pinyin), punctuation, numbers, English words, errors in the input, and peculiar characters like [欸](https://baike.baidu.com/item/%E6%AC%B8/2235416) and [圕](https://baike.baidu.com/item/%E5%9C%95/10177623).  
> **username_4**: According to Pinyin Rules, an apostrophe (隔音符号，单引号）must be inserted before a vowel (a. e. o) in case it starts a new syllable in the middel of a word, such as in (xi'an). In the word tian'anmen, you still need the (') symbol, even if there is no word tia-nan-men. (no the sound of tia).  
> **username_5**: Commenting rather than answering because this isn't in Python, but [this project](https://github.com/pepebecker/pinyin-split) in TypeScript seems to be decently well-tested, and uses a list of common syllables. I agree that ambiguity would make this brittle, but maybe converting this to Python, possibly adding more common syllables, and sorting them by commonality, would get you pretty close.  
> **username_6**: Where is your python code? Show me the code please.  

---

## 3 answers

### Answer by username_7 on 2024-11-26. Score: 1

*Error converting HTML to text*

<p>As indicated in the comments, <em>correct</em> pīnyīn is unambiguously convertible into syllables, because of the apostrophe rule. However, expect people complaining about their wrong pīnyīn not being parsed correctly.</p>
<p>You have to split in front of</p>
<ol>
<li>every normal consonant and "yw", <code>"bpmfdtlkjqxzcsryw"</code></li>
<li>every <code>"h"</code> not preceeded by <code>"zcs"</code></li>
<li>every <code>"'"</code>, deleting the <code>"'"</code></li>
<li>every <code>"n"</code> or <code>"g"</code> followed by a vowel. (This is the worst part)</li>
</ol>
<p>This is all doable by regex.</p>
<pre><code>import re

def split_pinyin(unsplit: str) -> str:
   """Split pinyin into syllables."""
   normal = "[bpmfdtlkjqxzcsryw]"
   h = r"(?<![zcs])h"
   ng = r"[ng][aeiouüáāàǎéèēěíìīǐóòōǒúùūǔǘǜǖǚ]"
   unsplit = unsplit.replace("'", " ")
   pattern = r"(?<=\w)(" + "|".join((normal, h, ng)) + ")"
   return re.sub(pattern, r" \1", unsplit)

for test in """tàngsuǒ
débùchángshī
fēngtǔrénqíng
huǎngrán dà wù
jiànjí'ānxiāng
ānníng, xiāng'ài, guàngài""".split("\n"):
    print(split_pinyin(test))
</code></pre>
<p><strong>Edit:</strong> The first version forgot about "y", "w".</p>


> **Comments:**
> **username_8**: Great answer! Great Python! Well done! Love your regex!  
> **username_7**: @username_8 Just in case you already copied it: I forgot about y,w.  
> **username_8**: Thank you! I did copy! I will add y and w! I am wrestling with a cv2 thing to find dices in an image and get the number on each die. I thought it would be easy, but turns out more complicated! Thanks again!  

---

### Answer by username_6 on 2024-11-26. Score: 0

因为你没有提供你的python代码，我也没法看你的代码，所以在这里我给出一个编程方面的思路：

首先把数组a全部初始化为0，然后以逆序的方式读取所有的字符到一个数组a中，比如 tàngsuǒ，读取后数组中a[0]是ǒ，a[1]是u，a[2]是s。。。a[6]是t。

再初始化一个数组b，内容全部是0，然后开始复制数组a的元素到数组b，复制过程中判断数组a，如果是元音，比如a[2]是s，则把s复制到b[2]后，再在后面加一个空格到b[3]中。。。，当然几个特殊的发音也需要处理，读取数组a中最后一个元素后不加空格。

最后再把数组b以逆序的方式，就是以b[7]，b[6]。。。b[0]的顺序，打印出来，就得到你想要的结果了。


---

### Answer by username_4 on 2023-02-27. Score: 0

The objective of the project: Splitting a given string containing pinyin into its syllables.

I would do it with the following steps. There could be exceptions, therefore testing is very important.

(1) Separate all characters.

(2) Combine adjacent vowels (ai, ei, ao, ou, ia, ie, iu, ua, uo, ui, ve, iao). \*1

(3) combine adjacent consonants (ng, ch, sh, zh). \*2

(4) combine finals with n and ng.

(5) combine initials and finals to form the syllables. \*3

\*1 I am not quite sure about er in (二）and (一会儿).

\*2 combine n and g only if g is followed by a consonant or a separate symbol. The official separate symbol is ('), such as in (xi'an).

\*3 Some syllables do not have an initial but only a final.


---

