From d0cda924583c23a36f4230560f7a39a488201bfb Mon Sep 17 00:00:00 2001 From: letiantian Date: Sat, 1 Oct 2016 20:01:45 +0800 Subject: [PATCH] v0.1.3: custom pinyin dict --- ChineseTone/chinesetone.py | 11 +++++++++++ example/example06.py | 15 +++++++++++++++ setup.py | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 example/example06.py diff --git a/ChineseTone/chinesetone.py b/ChineseTone/chinesetone.py index 4810f73..f4200b9 100644 --- a/ChineseTone/chinesetone.py +++ b/ChineseTone/chinesetone.py @@ -285,6 +285,17 @@ def __middle(s, pinyinFormat, replace): result += __middle(item, pinyinFormat, replace) return result + @staticmethod + def addWordPinyin(w, pinyinList): + ''' 添加自定义的单词的拼音,若已有则替换 ''' + PinyinHelper.loadTable() + PinyinHelper.WORD_PINYIN_TABLE[w] = pinyinList + + @staticmethod + def addCharPinyin(c, pinyinList): + ''' 添加自定义的字符的拼音,若已有则替换 ''' + PinyinHelper.loadTable() + PinyinHelper.PINYIN_TABLE[c] = pinyinList @staticmethod def hasMultiPinyin(c): diff --git a/example/example06.py b/example/example06.py new file mode 100644 index 0000000..70e89b1 --- /dev/null +++ b/example/example06.py @@ -0,0 +1,15 @@ +# coding: utf-8 + +from __future__ import (print_function, unicode_literals) + +from ChineseTone import * + +print(','.join(PinyinHelper.convertToPinyinFromSentence('金馆长啊', pinyinFormat=PinyinFormat.WITHOUT_TONE))) +PinyinHelper.addWordPinyin('金馆长', ['jin', 'guan', 'zhang']) # 建议实际情况下拼音中加入声调 +print(','.join(PinyinHelper.convertToPinyinFromSentence('金馆长啊', pinyinFormat=PinyinFormat.WITHOUT_TONE))) + +print(','.join(PinyinHelper.convertToPinyinFromSentence('价值40$', pinyinFormat=PinyinFormat.WITHOUT_TONE))) +PinyinHelper.addCharPinyin('4', ['si']) # 考虑多音字,所以用list +PinyinHelper.addCharPinyin('0', ['ling']) +PinyinHelper.addWordPinyin('$', ['mei', 'yuan']) # 这个用法奇怪些 +print(','.join(PinyinHelper.convertToPinyinFromSentence('价值40$', pinyinFormat=PinyinFormat.WITHOUT_TONE))) diff --git a/setup.py b/setup.py index 04b674e..b81538b 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ setup( name='ChineseTone', - version='0.1.2', + version='0.1.3', description='汉字转换为拼音,支持多音字', long_description=LONGDOC, author='Letian Sun',