From d1f20b2fffc2bb26087b7e9aeb160bf6373f00d7 Mon Sep 17 00:00:00 2001 From: sabonerune <102559104+sabonerune@users.noreply.github.com> Date: Thu, 17 Apr 2025 22:20:45 +0900 Subject: [PATCH] ENH: Add support for unsetting a dictionary once set. --- pyopenjtalk/__init__.py | 9 ++++++--- tests/test_openjtalk.py | 16 ++++++++++------ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/pyopenjtalk/__init__.py b/pyopenjtalk/__init__.py index 656c508..127f026 100644 --- a/pyopenjtalk/__init__.py +++ b/pyopenjtalk/__init__.py @@ -274,12 +274,15 @@ def update_global_jtalk_with_user_dict(path): Note that this will change the global state of the openjtalk module. Args: - path (str): path to user dictionary + path (str | None): path to user dictionary. + If None, Update global openjtalk instance without the user dictionary. """ + if path is None: + path = "" + elif not exists(path): + raise FileNotFoundError("no such file or directory: %s" % path) global _global_jtalk with _global_jtalk(): - if not exists(path): - raise FileNotFoundError("no such file or directory: %s" % path) _global_jtalk = _global_instance_manager( instance=OpenJTalk( dn_mecab=OPEN_JTALK_DICT_DIR, userdic=path.encode("utf-8") diff --git a/tests/test_openjtalk.py b/tests/test_openjtalk.py index 5a27466..459f37c 100644 --- a/tests/test_openjtalk.py +++ b/tests/test_openjtalk.py @@ -89,10 +89,11 @@ def test_g2p_phone(): def test_userdic(): - for text, expected in [ + parameter = [ ("nnmn", "n a n a m i N"), ("GNU", "g u n u u"), - ]: + ] + for text, expected in parameter: p = pyopenjtalk.g2p(text) assert p != expected @@ -106,13 +107,16 @@ def test_userdic(): pyopenjtalk.mecab_dict_index(f.name, user_dic) pyopenjtalk.update_global_jtalk_with_user_dict(user_dic) - for text, expected in [ - ("nnmn", "n a n a m i N"), - ("GNU", "g u n u u"), - ]: + for text, expected in parameter: p = pyopenjtalk.g2p(text) assert p == expected + pyopenjtalk.update_global_jtalk_with_user_dict(None) + + for text, expected in parameter: + p = pyopenjtalk.g2p(text) + assert p != expected + def test_multithreading(): ojt = pyopenjtalk.openjtalk.OpenJTalk(pyopenjtalk.OPEN_JTALK_DICT_DIR)