From fbbfb089447bea92ca080cd60e116b1984de438d Mon Sep 17 00:00:00 2001 From: Yang Zhang Date: Mon, 14 Feb 2022 15:05:14 -0800 Subject: [PATCH] adding guard (#3655) Signed-off-by: Yang Zhang --- .../inverse_normalize.py | 4 ++++ .../text_normalization/data_loader_utils.py | 16 ++++++++++++++++ .../text_normalization/en/graph_utils.py | 2 +- .../text_normalization/normalize.py | 5 ++++- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/nemo_text_processing/inverse_text_normalization/inverse_normalize.py b/nemo_text_processing/inverse_text_normalization/inverse_normalize.py index f1e69085cf7d..fa0a3e29d35f 100644 --- a/nemo_text_processing/inverse_text_normalization/inverse_normalize.py +++ b/nemo_text_processing/inverse_text_normalization/inverse_normalize.py @@ -16,6 +16,7 @@ from time import perf_counter from typing import List +from nemo_text_processing.text_normalization.data_loader_utils import check_installation, get_installation_msg from nemo_text_processing.text_normalization.normalize import Normalizer from nemo_text_processing.text_normalization.token_parser import TokenParser @@ -32,6 +33,9 @@ class InverseNormalizer(Normalizer): """ def __init__(self, lang: str = 'en', cache_dir: str = None, overwrite_cache: bool = False): + + if not check_installation(): + raise ImportError(get_installation_msg()) if lang == 'en': from nemo_text_processing.inverse_text_normalization.en.taggers.tokenize_and_classify import ClassifyFst from nemo_text_processing.inverse_text_normalization.en.verbalizers.verbalize_final import ( diff --git a/nemo_text_processing/text_normalization/data_loader_utils.py b/nemo_text_processing/text_normalization/data_loader_utils.py index 5253a5745dae..887ca75cc5eb 100644 --- a/nemo_text_processing/text_normalization/data_loader_utils.py +++ b/nemo_text_processing/text_normalization/data_loader_utils.py @@ -238,3 +238,19 @@ def pre_process(text: str) -> str: # remove extra space text = re.sub(r' +', ' ', text) return text + + +def check_installation(): + try: + import pynini + + PYNINI_AVAILABLE = True + + except (ModuleNotFoundError, ImportError): + PYNINI_AVAILABLE = False + return PYNINI_AVAILABLE + + +def get_installation_msg(): + msg = "`pynini` is not installed ! \n Please run the `nemo_text_processing/setup.sh` script prior to usage of this toolkit." + return msg diff --git a/nemo_text_processing/text_normalization/en/graph_utils.py b/nemo_text_processing/text_normalization/en/graph_utils.py index e99922e1350d..26039e59b47a 100644 --- a/nemo_text_processing/text_normalization/en/graph_utils.py +++ b/nemo_text_processing/text_normalization/en/graph_utils.py @@ -116,7 +116,7 @@ PYNINI_AVAILABLE = False -def generator_main(file_name: str, graphs: Dict[str, pynini.FstLike]): +def generator_main(file_name: str, graphs: Dict[str, 'pynini.FstLike']): """ Exports graph as OpenFst finite state archive (FAR) file with given file name and rule name. diff --git a/nemo_text_processing/text_normalization/normalize.py b/nemo_text_processing/text_normalization/normalize.py index 8459b3472665..0d5715e53fcb 100644 --- a/nemo_text_processing/text_normalization/normalize.py +++ b/nemo_text_processing/text_normalization/normalize.py @@ -20,7 +20,7 @@ from math import factorial from typing import Dict, List, Union -from nemo_text_processing.text_normalization.data_loader_utils import pre_process +from nemo_text_processing.text_normalization.data_loader_utils import get_installation_msg, pre_process from nemo_text_processing.text_normalization.token_parser import PRESERVE_ORDER_KEY, TokenParser from tqdm import tqdm @@ -68,6 +68,9 @@ def __init__( ): assert input_case in ["lower_cased", "cased"] + if not PYNINI_AVAILABLE: + raise ImportError(get_installation_msg()) + if lang == 'en' and deterministic: from nemo_text_processing.text_normalization.en.taggers.tokenize_and_classify import ClassifyFst from nemo_text_processing.text_normalization.en.verbalizers.verbalize_final import VerbalizeFinalFst