python中的连字符(撇号和异常)

2024-04-26 20:57:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用pyhyphen得到单词的音节。如果我用的是英语词典,我认为撇号的处理是正确的

import hyphen
h = hyphen.Hyphenator('en_US')
h.syllables(u"Hammond's")

它只是包含在一个音节里

^{pr2}$

但是如果我用德语字典也这么做的话

h = hyphen.Hyphenator('de_CH')
h.syllables(u"Hammond's")
h.syllables(u"Bismarck'sche")

撇号被看作是它自己的音节:

[u'Ham', u'mond', u"'s"]
[u'Bis', u'marck', u"'", u'sche']

我在想,如果可以为某些字符定义异常(而不是中断)呢?好像在LaTex中是可能的。在

我想到的解决办法就是在音节中寻找一个前导撇号,然后与前一个撇号连接起来:

syl = [u'Bis', u'marck', u"'", u'sche']
syls2 = []
for syl in syls:
    if syl.startswith("'"):
        if not syls2:
            syls2.append(syl)
        else:
            syls2[-1]+=syl
    else:
        syls2.append(syl)

[u'Bis', u"marck'", u'sche']

但这并不是一个好的或通用的解决方案,我感兴趣的是如何定义单词的连字符规则,在那里它做得不正确。在


Tags: if定义字符单词音节hyphenatorappendbis