Skip to content

Commit

Permalink
Merge 809b7d5 into 974350d
Browse files Browse the repository at this point in the history
  • Loading branch information
daya0576 committed Jan 11, 2018
2 parents 974350d + 809b7d5 commit 8057d8d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
9 changes: 9 additions & 0 deletions docs/faq.rst
Expand Up @@ -8,6 +8,15 @@ FAQ
设置环境变量 ``PYPINYIN_NO_PHRASES=true`` 即可


如何禁用默认的“拼音库”copy 操作
++++++++++++++++++++++++++++++++

设置环境变量 ``PYPINYIN_NO_DICT_COPY=true`` 即可.


副作用: 用户的自定义拼音库出现问题时, 无法回退到自带的拼音库.


``INITIALS`` 声母风格下,以 ``y``, ``w``, ``yu`` 开头的汉字返回空字符串
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Expand Down
10 changes: 8 additions & 2 deletions pypinyin/constants.py
Expand Up @@ -16,10 +16,16 @@
PHRASES_DICT = {}
else:
from pypinyin import phrases_dict
PHRASES_DICT = phrases_dict.phrases_dict.copy()
PHRASES_DICT = phrases_dict.phrases_dict

# 单字拼音库
PINYIN_DICT = pinyin_dict.pinyin_dict.copy()
PINYIN_DICT = pinyin_dict.pinyin_dict

# 利用环境变量控制不做copy操作(无自定义拼音库的情况), 以减少内存使用
if not os.environ.get('PYPINYIN_NO_DICT_COPY'):
PINYIN_DICT = PINYIN_DICT.copy()
PHRASES_DICT = PHRASES_DICT.copy()

# 匹配使用数字标识声调的字符的正则表达式
RE_TONE2 = re.compile(r'([aeoiuvnm])([1-4])$')

Expand Down
14 changes: 14 additions & 0 deletions tests/_test_env.py
Expand Up @@ -2,7 +2,9 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from importlib import reload
import os

os.environ['PYPINYIN_NO_PHRASES'] = 'true'

import pypinyin.core # noqa
Expand All @@ -11,3 +13,15 @@
def test_env():
assert pypinyin.core.PHRASES_DICT == {}
assert pypinyin.core.seg('北京') == ['北京']


def test_no_copy():
""" 禁用 copy 操作 """
reload(pypinyin.constants)
assert pypinyin.core.PINYIN_DICT is not pypinyin.pinyin_dict.pinyin_dict

os.environ['PYPINYIN_NO_DICT_COPY'] = 'true'
reload(pypinyin.constants)
assert pypinyin.constants.PINYIN_DICT is pypinyin.pinyin_dict.pinyin_dict


0 comments on commit 8057d8d

Please sign in to comment.