示例#1
0
def test_path_good():
    try:
        binary = distutils.spawn.find_executable('festival')
        FestivalBackend.set_festival_path(binary)

        test_im()

    # restore the festival path to default
    finally:
        FestivalBackend.set_festival_path(None)
示例#2
0
def test_path_bad():
    try:
        # corrupt the default espeak path, try to use python executable instead
        binary = distutils.spawn.find_executable('python')
        FestivalBackend.set_festival_path(binary)

        with pytest.raises(RuntimeError):
            FestivalBackend('en-us').phonemize('hello')
        with pytest.raises(RuntimeError):
            FestivalBackend.version()

        with pytest.raises(ValueError):
            FestivalBackend.set_festival_path(__file__)

    # restore the festival path to default
    finally:
        FestivalBackend.set_festival_path(None)
示例#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')
示例#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_path:
        EspeakBackend.set_espeak_path(args.espeak_path)
    if args.festival_path:
        FestivalBackend.set_festival_path(args.festival_path)

    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,
                              preserve_punctuation=args.preserve_punctuation,
                              punctuation_marks=args.punctuation_marks,
                              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')