Python: How to prepend the string 'ub' to every pronounced vowel in a string?

前端 未结 3 1737
-上瘾入骨i
-上瘾入骨i 2020-12-06 12:52

Example: Speak -> Spubeak, more info here

Don\'t give me a solution, but point me in the right direction or tell which which python library I could

3条回答
  •  遥遥无期
    2020-12-06 13:44

    You can use regular expressions for substitutions. See re.sub.

    Example:

    >>> import re
    >>> re.sub(r'(e)', r'ub\1', 'speak')
    'spubeak'
    

    You will need to read the documentation for regex groups and so on. You will also need to figure out how to match different vowels instead of just the one in the example.

    For some great ideas (and code) for using regular expressions in Python for a pronunciation dictionary, take a look at this link, which is one of the design pages for the Cainteoir project: http://rhdunn.github.com/cainteoir/rules.html

    Cainteoir's text-to-speech rule engine design (which is not fully implemented yet) uses regular expressions. See also Pronunciation Dictionaries and Regexes, another article by the Cainteoir author.

提交回复
热议问题