kongdeqiang
2026-03-13 28ccfbfc51068a663a80764e14074df5edf2b5ba
fun_text_processing/text_normalization/de/verbalizers/verbalize_final.py
@@ -1,17 +1,3 @@
# Copyright NeMo (https://github.com/NVIDIA/NeMo). All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import pynini
@@ -31,7 +17,7 @@
class VerbalizeFinalFst(GraphFst):
    """
    Finite state transducer that verbalizes an entire sentence
    Args:
        deterministic: if True will provide a single transduction option,
            for False multiple options (used for audio-based normalization)
@@ -39,16 +25,20 @@
        overwrite_cache: set to True to overwrite .far files
    """
    def __init__(self, deterministic: bool = True, cache_dir: str = None, overwrite_cache: bool = False):
    def __init__(
        self, deterministic: bool = True, cache_dir: str = None, overwrite_cache: bool = False
    ):
        super().__init__(name="verbalize_final", kind="verbalize", deterministic=deterministic)
        far_file = None
        if cache_dir is not None and cache_dir != "None":
            os.makedirs(cache_dir, exist_ok=True)
            far_file = os.path.join(cache_dir, f"de_tn_{deterministic}_deterministic_verbalizer.far")
            far_file = os.path.join(
                cache_dir, f"de_tn_{deterministic}_deterministic_verbalizer.far"
            )
        if not overwrite_cache and far_file and os.path.exists(far_file):
            self.fst = pynini.Far(far_file, mode="r")["verbalize"]
            logging.info(f'VerbalizeFinalFst graph was restored from {far_file}.')
            logging.info(f"VerbalizeFinalFst graph was restored from {far_file}.")
        else:
            verbalize = VerbalizeFst(deterministic=deterministic).fst
            word = WordFst(deterministic=deterministic).fst