From 2a7faf5a4822db1eb622bf305bc06addd9d795e5 Mon Sep 17 00:00:00 2001 From: tsukumi Date: Thu, 1 Aug 2024 18:41:00 +0900 Subject: [PATCH] =?UTF-8?q?Refactor:=20NJDFeature=20=E5=9E=8B=E3=81=AE?= =?UTF-8?q?=E5=AE=9A=E7=BE=A9=E3=82=92=20types.py=20=E3=81=AB=E7=A7=BB?= =?UTF-8?q?=E5=8B=95=E3=81=97=E3=80=81if=20TYPE=5FCHECKING:=20=E3=81=9B?= =?UTF-8?q?=E3=81=9A=E3=81=A8=E3=82=82=20NJDFeature=20=E3=82=92=E5=9E=8B?= =?UTF-8?q?=E3=83=92=E3=83=B3=E3=83=88=E3=81=AB=E4=BD=BF=E3=81=88=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E6=94=B9=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 従来は openjtalk.pyx の型定義ファイルである openjtalk.pyi に記述されていたため型としては実態がなく、呼び出すには from __future__ import annotations に加え if TYPE_CHECKING: の中で import する必要があり非常に面倒だった --- pyopenjtalk/__init__.py | 8 ++------ pyopenjtalk/openjtalk.pyi | 27 +++----------------------- pyopenjtalk/types.py | 18 +++++++++++++++++ pyopenjtalk/utils.py | 8 ++------ pyopenjtalk/yomi_model/nani_predict.py | 8 ++------ 5 files changed, 27 insertions(+), 42 deletions(-) create mode 100644 pyopenjtalk/types.py diff --git a/pyopenjtalk/__init__.py b/pyopenjtalk/__init__.py index a5dc75c..7467887 100644 --- a/pyopenjtalk/__init__.py +++ b/pyopenjtalk/__init__.py @@ -1,12 +1,10 @@ -from __future__ import annotations - import atexit import os import sys from contextlib import ExitStack from os.path import exists from pathlib import Path -from typing import TYPE_CHECKING, Any, List, Tuple, Union +from typing import Any, List, Tuple, Union import numpy as np import numpy.typing as npt @@ -25,11 +23,9 @@ from .openjtalk import OpenJTalk from .openjtalk import build_mecab_dictionary as _build_mecab_dictionary from .openjtalk import mecab_dict_index as _mecab_dict_index +from .types import NJDFeature from .utils import merge_njd_marine_features, modify_kanji_yomi, modify_masu_acc, retreat_acc_nuc -if TYPE_CHECKING: - from .openjtalk import NJDFeature - _file_manager = ExitStack() atexit.register(_file_manager.close) diff --git a/pyopenjtalk/openjtalk.pyi b/pyopenjtalk/openjtalk.pyi index 3f4d7fd..39329c0 100644 --- a/pyopenjtalk/openjtalk.pyi +++ b/pyopenjtalk/openjtalk.pyi @@ -1,29 +1,8 @@ # flake8: noqa -import sys -from typing import Dict, Iterable, List - -if sys.version_info >= (3, 8): - from typing import TypedDict - - class NJDFeature(TypedDict): - string: str - pos: str - pos_group1: str - pos_group2: str - pos_group3: str - ctype: str - cform: str - orig: str - read: str - pron: str - acc: int - mora_size: int - chain_rule: str - chain_flag: int - -else: - NJDFeature = Dict[str, str | int] +from typing import Iterable, List + +from .types import NJDFeature class OpenJTalk: def __init__(self, dn_mecab: bytes = b"/usr/local/dic", userdic: bytes = b"") -> None: diff --git a/pyopenjtalk/types.py b/pyopenjtalk/types.py new file mode 100644 index 0000000..00e4631 --- /dev/null +++ b/pyopenjtalk/types.py @@ -0,0 +1,18 @@ +from typing import TypedDict + + +class NJDFeature(TypedDict): + string: str + pos: str + pos_group1: str + pos_group2: str + pos_group3: str + ctype: str + cform: str + orig: str + read: str + pron: str + acc: int + mora_size: int + chain_rule: str + chain_flag: int diff --git a/pyopenjtalk/utils.py b/pyopenjtalk/utils.py index 7225cff..5829e79 100644 --- a/pyopenjtalk/utils.py +++ b/pyopenjtalk/utils.py @@ -1,14 +1,10 @@ -from __future__ import annotations - -from typing import TYPE_CHECKING, Any, Dict, List +from typing import Any, Dict, List from sudachipy import dictionary, tokenizer +from .types import NJDFeature from .yomi_model.nani_predict import predict -if TYPE_CHECKING: - from .openjtalk import NJDFeature - def merge_njd_marine_features( njd_features: List[NJDFeature], marine_results: Dict[str, Any] diff --git a/pyopenjtalk/yomi_model/nani_predict.py b/pyopenjtalk/yomi_model/nani_predict.py index e31be99..5d57b34 100644 --- a/pyopenjtalk/yomi_model/nani_predict.py +++ b/pyopenjtalk/yomi_model/nani_predict.py @@ -1,14 +1,10 @@ -from __future__ import annotations - import os # import pickle -from typing import TYPE_CHECKING, List, Union +from typing import List, Union # import pandas as pd - -if TYPE_CHECKING: - from ..openjtalk import NJDFeature +from ..types import NJDFeature X_COLS = ["pos", "pos_group1", "pos_group2", "pron", "ctype", "cform"] model_dir = os.path.dirname(__file__)