Skip to content

Commit

Permalink
adding guard (#3655)
Browse files Browse the repository at this point in the history
Signed-off-by: Yang Zhang <[email protected]>
  • Loading branch information
yzhang123 authored Feb 14, 2022
1 parent 6a517f0 commit fbbfb08
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 (
Expand Down
16 changes: 16 additions & 0 deletions nemo_text_processing/text_normalization/data_loader_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion nemo_text_processing/text_normalization/en/graph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion nemo_text_processing/text_normalization/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fbbfb08

Please sign in to comment.