Ejemplo n.º 1
0
def main():
    """Phonemize a text from command-line arguments"""
    args = parse_args()

    if args.version:
        print(version.version())
        return

    # configure logging according to --verbose/--quiet options
    verbosity = 'normal'
    if args.verbose:
        verbosity = 'verbose'
    elif args.quiet:
        verbosity = 'quiet'
    log = logger.get_logger(verbosity=verbosity)

    # configure input as a readable stream
    streamin = args.input
    if isinstance(streamin, str):
        streamin = codecs.open(streamin, 'r', encoding='utf8')
    log.debug('reading from %s', streamin.name)

    # configure output as a writable stream
    streamout = args.output
    if isinstance(streamout, str):
        streamout = codecs.open(streamout, 'w', 'utf8')
    log.debug('writing to %s', streamout.name)

    # configure the separator for phonemes, syllables and words.
    sep = separator.Separator(phone=args.phone_separator,
                              syllable=args.syllable_separator,
                              word=args.word_separator)
    log.debug('separator is %s', sep)

    # load the input text (python2 optionnally needs an extra decode)
    text = streamin.read()
    try:
        text = text.decode('utf8')
    except (AttributeError, UnicodeEncodeError):
        pass

    # phonemize the input text
    out = phonemize.phonemize(text,
                              language=args.language,
                              backend=args.backend,
                              separator=sep,
                              strip=args.strip,
                              with_stress=args.with_stress,
                              use_sampa=args.sampa,
                              language_switch=args.language_switch,
                              njobs=args.njobs,
                              logger=log)

    if len(out):
        streamout.write(out + '\n')
Ejemplo n.º 2
0
def main():
    """Phonemize a text from command-line arguments"""
    args = parse_args()

    if args.version:
        print(version.version())
        return

    # configure logging according to --verbose option. Init a logger to output
    # on stderr. By default log only warnings, if verbose log all messages, if
    # quiet do not log anything.
    logger = logging.getLogger()
    logger.handlers = []
    handler = logging.StreamHandler(sys.stderr)
    logger.setLevel(logging.WARNING)
    if args.verbose:
        logger.setLevel(logging.DEBUG)
    if args.quiet:
        handler = logging.NullHandler()
    handler.setFormatter(logging.Formatter('[%(levelname)s] %(message)s'))
    logger.addHandler(handler)

    # configure input as a readable stream
    streamin = args.input
    if isinstance(streamin, str):
        streamin = codecs.open(streamin, 'r', encoding='utf8')
    logger.debug('reading from %s', streamin.name)

    # configure output as a writable stream
    streamout = args.output
    if isinstance(streamout, str):
        streamout = codecs.open(streamout, 'w', 'utf8')
    logger.debug('writing to %s', streamout.name)

    # configure the separator for phonemes, syllables and words.
    sep = separator.Separator(
        phone=args.phone_separator,
        syllable=args.syllable_separator,
        word=args.word_separator)
    logger.debug('separator is %s', sep)

    # load the input text (python2 optionnally needs an extra decode)
    text = streamin.read()
    try:
        text = text.decode('utf8')
    except (AttributeError, UnicodeEncodeError):
        pass

    # phonemize the input text
    out = phonemize.phonemize(
        text,
        language=args.language,
        backend=args.backend,
        separator=sep,
        strip=args.strip,
        with_stress=args.with_stress,
        use_sampa=args.sampa,
        language_switch=args.language_switch,
        njobs=args.njobs,
        logger=logger)

    if len(out):
        streamout.write(out + '\n')
Ejemplo n.º 3
0
def main():
    """Phonemize a text from command-line arguments"""
    args = parse_args()

    # setup a custom path to espeak and festival if required (this must be done
    # before generating the version message)
    if args.espeak_path:
        EspeakBackend.set_espeak_path(args.espeak_path)
    if args.festival_path:
        FestivalBackend.set_festival_path(args.festival_path)

    # display version information and exit
    if args.version:
        print(version.version())
        return

    # list supported languages and exit
    if args.list_languages:
        backends = (['festival', 'segments', 'espeak', 'espeak-mbrola']
                    if not args.backend else [args.backend])
        for backend in backends:
            print(f'supported languages for {backend} are:\n' +
                  '\n'.join(f'\t{k}\t->\t{v}' for k, v in sorted(
                      BACKENDS_MAP[backend].supported_languages().items())))
        return

    # set default backend as espeak if not specified
    args.backend = args.backend or 'espeak'

    # configure logging according to --verbose/--quiet options
    verbosity = 'normal'
    if args.verbose:
        verbosity = 'verbose'
    elif args.quiet:
        verbosity = 'quiet'
    log = logger.get_logger(verbosity=verbosity)

    # configure input as a readable stream
    streamin = args.input
    if isinstance(streamin, str):
        streamin = codecs.open(streamin, 'r', encoding='utf8')
    log.debug('reading from %s', streamin.name)

    # configure output as a writable stream
    streamout = args.output
    if isinstance(streamout, str):
        streamout = codecs.open(streamout, 'w', 'utf8')
    log.debug('writing to %s', streamout.name)

    # configure the separator for phonemes, syllables and words.
    if args.backend == 'espeak-mbrola':
        log.debug('using espeak-mbrola backend: ignoring word separator')
        sep = separator.Separator(phone=args.phone_separator,
                                  syllable=None,
                                  word=None)
    else:
        sep = separator.Separator(phone=args.phone_separator,
                                  syllable=args.syllable_separator,
                                  word=args.word_separator)
    log.debug('separator is %s', sep)

    text = [line.strip() for line in streamin]

    # phonemize the input text
    out = phonemize(text,
                    language=args.language,
                    backend=args.backend,
                    separator=sep,
                    strip=args.strip,
                    preserve_punctuation=args.preserve_punctuation,
                    punctuation_marks=args.punctuation_marks,
                    with_stress=args.with_stress,
                    language_switch=args.language_switch,
                    njobs=args.njobs,
                    logger=log)

    if out:
        streamout.write('\n'.join(out) + '\n')
Ejemplo n.º 4
0
def main():
    """Phonemize a text from command-line arguments"""
    args = parse_args()

    # setup a custom path to espeak and festival if required (this must be done
    # before generating the version message)
    if args.espeak_library:
        BACKENDS['espeak'].set_library(args.espeak_library)
    if args.festival_executable:
        BACKENDS['festival'].set_executable(args.festival_executable)

    # display version information and exit
    if args.version:
        print(version.version())
        return

    # list supported languages and exit
    if args.list_languages:
        print(list_languages(args.backend))
        return

    # set default backend as espeak if not specified
    args.backend = args.backend or 'espeak'

    # configure logging according to --verbose/--quiet options
    log = get_logger(args.verbose, args.quiet)

    # configure input:output as a readable/writable streams
    streamin = setup_stream(args.input, 'r')
    log.debug('reading from %s', streamin.name)
    streamout = setup_stream(args.output, 'w')
    log.debug('writing to %s', streamout.name)

    # configure the separator for phonemes, syllables and words.
    if args.backend == 'espeak-mbrola':
        log.debug('using espeak-mbrola backend: ignoring word separator')
        sep = separator.Separator(phone=args.phone_separator,
                                  syllable=None,
                                  word=None)
    else:
        sep = separator.Separator(phone=args.phone_separator,
                                  syllable=args.syllable_separator,
                                  word=args.word_separator)
    log.debug('separator is %s', sep)

    if args.prepend_text:
        input_output_separator = sep.input_output_separator(args.prepend_text)
        log.debug('prepend input text to output, separator is "%s"',
                  input_output_separator)
    else:
        input_output_separator = False

    # phonemize the input text
    out = phonemize(streamin.readlines(),
                    language=args.language,
                    backend=args.backend,
                    separator=sep,
                    strip=args.strip,
                    prepend_text=args.prepend_text,
                    preserve_punctuation=args.preserve_punctuation,
                    punctuation_marks=args.punctuation_marks,
                    with_stress=args.with_stress,
                    tie=args.tie,
                    language_switch=args.language_switch,
                    words_mismatch=args.words_mismatch,
                    njobs=args.njobs,
                    logger=log)

    if out and input_output_separator:
        streamout.write(
            os.linesep.join(f'{line[0]} {input_output_separator} {line[1]}'
                            for line in out) + os.linesep)
    elif out:
        streamout.write(os.linesep.join(out) + os.linesep)