Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 2 files changed
  • 0 comments
  • 1 contributor

Showing 2 changed files with 48 additions and 0 deletions. Show diff stats Hide diff stats

  1. +2 0  .gitignore
  2. +46 0 num2pinyin.py
2  .gitignore
@@ -25,3 +25,5 @@ pip-log.txt
25 25
26 26 #Mr Developer
27 27 .mr.developer.cfg
  28 +
  29 +*~
46 num2pinyin.py
... ... @@ -0,0 +1,46 @@
  1 +#!/usr/local/bin/python
  2 +# -*- coding: utf-8 -*-
  3 +
  4 +# num2pinyin.py (c) 2012 Kasper Souren <kasper@guaka.org>
  5 +#
  6 +# This program is free software: you can redistribute it and/or modify
  7 +# it under the terms of the GNU General Public License as published by
  8 +# the Free Software Foundation, either version 3 of the License, or
  9 +# (at your option) any later version.
  10 +#
  11 +# This program is distributed in the hope that it will be useful,
  12 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 +# GNU General Public License for more details.
  15 +#
  16 +# You should have received a copy of the GNU General Public License
  17 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  18 +
  19 +
  20 +
  21 +def num2pinyin(s):
  22 + # Inspired by http://en.wikipedia.org/wiki/Pinyin
  23 + # "the tone mark should always be placed by the
  24 + # order--'a','o','e','i','u','ü', with the only exception being
  25 + # 'iu', where the tone mark is placed on the 'u' instead."
  26 + d = { 1: u'ā ō ē ī ū ǖ Ā Ē Ī Ō Ū Ǖ',
  27 + 2: u'á ó é í ú ǘ Á É Í Ó Ú Ǘ',
  28 + 3: u'ǎ ǒ ě ǐ ǔ ǚ Ǎ Ě Ǐ Ǒ Ǔ Ǚ',
  29 + 4: u'à ò è ì ù ǜ À È Ì Ò Ù Ǜ',
  30 + 5: u'a o e i u ü A E I O U Ü'}
  31 + d5 = d[5]
  32 +
  33 + def num2diacritic(phon):
  34 + tone = int(phon[-1])
  35 + phon = phon[0 : -1]
  36 + # to do: add exception for iu
  37 + vowels = filter(lambda c: c in d5, phon)[0]
  38 + # print phon, tone, vowels
  39 + return phon.replace(vowels, d[tone][d5.find(vowels)])
  40 +
  41 + return ' '.join(map(num2diacritic, s.split(' ')))
  42 +
  43 +
  44 +test = 'wo3 men5 qu4 xiang1 gang3'
  45 +
  46 +print num2pinyin(test)

No commit comments for this range

Something went wrong with that request. Please try again.