"""
    ..
        ---------------------------------------------------------------------
         ___   __    __    __    ___
        /     |  \  |  \  |  \  /              the automatic
        \__   |__/  |__/  |___| \__             annotation and
           \  |     |     |   |    \             analysis
        ___/  |     |     |   | ___/              of speech

        http://www.sppas.org/

        Use of this software is governed by the GNU Public License, version 3.

        SPPAS is free software: you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation, either version 3 of the License, or
        (at your option) any later version.

        SPPAS is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with SPPAS. If not, see <http://www.gnu.org/licenses/>.

        This banner notice must not be removed.

        ---------------------------------------------------------------------

    src.models.acm.htkscripts.py
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

"""
import os
import os.path
import logging

# ---------------------------------------------------------------------------


class sppasHtkScripts(object):
    """HTK-ASCII scripts reader/writer.

    :organization: Laboratoire Parole et Langage, Aix-en-Provence, France
    :license:      GPL, v3
    :copyright:    Copyright (C) 2011-2018  Brigitte Bigi
    :author:       Brigitte Bigi
    :contact:      develop@sppas.org

    This class is able to write all scripts of the VoxForge tutorial.
    They are used to train acoustic models thanks to the HTK toolbox.

    For details, refer to: http://www.voxforge.org/

    """

    def __init__(self):
        """Create a sppasHtkScripts instance."""
        self.configfile = ""
        self.globalfile = ""
        self.mkphones0file = ""
        self.mkphones1file = ""
        self.mktrifile = ""
        self.maketriphonesfile = ""
        self.silfile = ""

    # -----------------------------------------------------------------------

    def write_all(self, dirname):
        """Write all scripts at once.

        Write scripts with their default name, in the given directory.

        :param dirname: (str) a directory name (existing or to be created).

        """
        if os.path.exists(dirname) is False:
            os.mkdir(dirname)

        self.write_global_ded(os.path.join(dirname, "global.ded"))
        self.write_mkphones0_led(os.path.join(dirname, "mkphones0.led"))
        self.write_mkphones1_led(os.path.join(dirname, "mkphones1.led"))
        self.write_mktri_led(os.path.join(dirname, "mktri.led"))
        self.write_maketriphones_ded(os.path.join(dirname, "maketriphones.ded"))
        self.write_sil_hed(os.path.join(dirname, "sil.hed"))

    # -----------------------------------------------------------------------

    def write_global_ded(self, filename):
        """Write the htk script `global.ded`.

        :param filename: (str) Name of the script file.

        """
        logging.info('Write script file: {!s:s}'.format(filename))
        with open(filename, "w") as fp:
            fp.write("AS sp\n")
            fp.write("RS cmu\n")
            fp.write("MP sil sil sp\n")
            fp.write("\n")
            fp.close()

        self.globalfile = filename

    # -----------------------------------------------------------------------

    def write_mkphones0_led(self, filename):
        """Write the htk script `mkphones0.led`.

        :param filename: (str) Name of the script file.

        """
        logging.info('Write script file: {!s:s}'.format(filename))
        with open(filename, "w") as fp:
            fp.write("EX\n")
            fp.write("IS sil sil\n")
            fp.write("DE sp\n")
            fp.write("\n")
            fp.close()

        self.mkphones0file = filename

    # -----------------------------------------------------------------------

    def write_mkphones1_led(self, filename):
        """Write the htk script `mkphones1.led`.

        :param filename: (str) Name of the script file.

        """
        logging.info('Write script file: {!s:s}'.format(filename))
        with open(filename, "w") as fp:
            fp.write("EX\n")
            fp.write("IS sil sil\n")
            fp.write("\n")
            fp.close()

        self.mkphones1file = filename

    # -----------------------------------------------------------------------

    def write_mktri_led(self, filename):
        """Write the htk script `mktri.led`.

        :param filename: (str) Name of the script file.

        """
        logging.info('Write script file: {!s:s}'.format(filename))
        with open(filename, "w") as fp:
            fp.write("WB sp\n")
            fp.write("WB sil\n")
            fp.write("TC\n")
            fp.write("\n")
            fp.close()

        self.mktrifile = filename

    # -----------------------------------------------------------------------

    def write_maketriphones_ded(self, filename):
        """Write the htk script `maketriphones.ded`.

        :param filename: (str) Name of the script file.

        """
        logging.info('Write script file: {!s:s}'.format(filename))
        with open(filename, "w") as fp:
            fp.write("AS sp\n")
            fp.write("MP sil sil sp\n")
            fp.write("TC\n")
            fp.write("\n")
            fp.close()

        self.maketriphonesfile = filename

    # -----------------------------------------------------------------------

    def write_sil_hed(self, filename):
        """Write the htk script `sil.hed`.

        :param filename: (str) Name of the script file.

        """
        logging.info('Write script file: {!s:s}'.format(filename))
        with open(filename, "w") as fp:
            fp.write("AT 2 4 0.2 {sil.transP}\n")
            fp.write("AT 4 2 0.2 {sil.transP}\n")
            fp.write("AT 1 3 0.3 {sp.transP}\n")
            fp.write("TI silst {sil.state[3],sp.state[2]}\n")
            fp.write("\n")
            fp.close()

        self.silfile = filename

[((78, 11, 78, 34), 'os.path.exists', 'os.path.exists', ({(78, 26, 78, 33): 'dirname'}, {}), '(dirname)', False, 'import os\n'), ((79, 12, 79, 29), 'os.mkdir', 'os.mkdir', ({(79, 21, 79, 28): 'dirname'}, {}), '(dirname)', False, 'import os\n'), ((81, 30, 81, 65), 'os.path.join', 'os.path.join', ({(81, 43, 81, 50): 'dirname', (81, 52, 81, 64): '"""global.ded"""'}, {}), "(dirname, 'global.ded')", False, 'import os\n'), ((82, 33, 82, 71), 'os.path.join', 'os.path.join', ({(82, 46, 82, 53): 'dirname', (82, 55, 82, 70): '"""mkphones0.led"""'}, {}), "(dirname, 'mkphones0.led')", False, 'import os\n'), ((83, 33, 83, 71), 'os.path.join', 'os.path.join', ({(83, 46, 83, 53): 'dirname', (83, 55, 83, 70): '"""mkphones1.led"""'}, {}), "(dirname, 'mkphones1.led')", False, 'import os\n'), ((84, 29, 84, 63), 'os.path.join', 'os.path.join', ({(84, 42, 84, 49): 'dirname', (84, 51, 84, 62): '"""mktri.led"""'}, {}), "(dirname, 'mktri.led')", False, 'import os\n'), ((85, 37, 85, 79), 'os.path.join', 'os.path.join', ({(85, 50, 85, 57): 'dirname', (85, 59, 85, 78): '"""maketriphones.ded"""'}, {}), "(dirname, 'maketriphones.ded')", False, 'import os\n'), ((86, 27, 86, 59), 'os.path.join', 'os.path.join', ({(86, 40, 86, 47): 'dirname', (86, 49, 86, 58): '"""sil.hed"""'}, {}), "(dirname, 'sil.hed')", False, 'import os\n')]