From 7c3ad45a019116ed0948e396f2ec1361054b019b Mon Sep 17 00:00:00 2001 From: Bikatr7 Date: Fri, 7 Jun 2024 01:33:59 -0600 Subject: [PATCH] move to 1.6.2, prevent spacy model from being loaded when it may not need to be or extra times --- pyproject.toml | 2 +- src/kairyou/kairyou.py | 20 ++++++++++++-------- src/kairyou/version.py | 2 +- tests/passing.py | 2 ++ 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index cbd8d63..6e26263 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ dependencies = [ ] name = "kairyou" -version = "v1.6.1" +version = "v1.6.2" authors = [ { name="Bikatr7", email="Bikatr7@proton.me" }, ] diff --git a/src/kairyou/kairyou.py b/src/kairyou/kairyou.py index 10b5ffb..309f0ed 100644 --- a/src/kairyou/kairyou.py +++ b/src/kairyou/kairyou.py @@ -18,7 +18,6 @@ # -------------------start-of-Kairyou--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - class Kairyou: """ @@ -53,13 +52,8 @@ class Kairyou: #----------------------------/ - ## The spacy NER model used for enhanced replacement checking. - try: - _ner = spacy.load("ja_core_news_lg") + _ner:spacy.language.Language | None = None - except Exception: - raise SpacyModelNotFound - ##-------------------start-of-_reset_globals()--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- @staticmethod @@ -110,6 +104,16 @@ def preprocess(text_to_preprocess:str, replacement_json:typing.Union[dict,str], """ + + ## The spacy NER model used for enhanced replacement checking. + try: + + if(Kairyou._ner is None): + Kairyou._ner = spacy.load("ja_core_news_lg") + + except Exception: + raise SpacyModelNotFound + ## If the replacement json is blank, skip the preprocessing. if(replacement_json == _kudasai_blank_json or replacement_json == _fukuin_blank_json): return text_to_preprocess, "Skipped", "" @@ -498,7 +502,7 @@ def _perform_enhanced_replace(jap:str, replacement:str) -> int: while (i < len(_jap_lines)): if (jap in _jap_lines[i]): - _sentence = Kairyou._ner(_jap_lines[i]) + _sentence = Kairyou._ner(_jap_lines[i]) # type:ignore for _entity in _sentence.ents: if (_entity.text == jap and _entity.label_ == "PERSON"): diff --git a/src/kairyou/version.py b/src/kairyou/version.py index 4f6e19e..e7f64d7 100644 --- a/src/kairyou/version.py +++ b/src/kairyou/version.py @@ -2,4 +2,4 @@ ## Use of this source code is governed by an GNU Lesser General Public License v2.1 ## license that can be found in the LICENSE file. -VERSION = "1.6.1" \ No newline at end of file +VERSION = "1.6.2" \ No newline at end of file diff --git a/tests/passing.py b/tests/passing.py index 4739a45..c9daa2f 100644 --- a/tests/passing.py +++ b/tests/passing.py @@ -51,6 +51,8 @@ def main(): if(KatakanaUtil.is_partially_english("ใƒ†ใ‚นใƒˆ")): raise ValueError("Test failed") + + print("All tests passed") if(__name__ == "__main__"):