Add tests and update README
This commit is contained in:
parent
056f93e355
commit
072ca8b0ed
8 changed files with 202 additions and 23 deletions
|
|
@ -1 +1 @@
|
|||
1.0
|
||||
1.0.0
|
||||
|
|
@ -1,10 +1,23 @@
|
|||
"""Uses ctypes and libespeak-ng to get IPA phonemes from text"""
|
||||
import ctypes
|
||||
import re
|
||||
import typing
|
||||
from pathlib import Path
|
||||
|
||||
_DIR = Path(__file__).parent
|
||||
__version__ = (_DIR / "VERSION").read_text().strip()
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
class Phonemizer:
|
||||
"""Use ctypes and libespeak-ng to get IPA phonemes from text"""
|
||||
"""
|
||||
Use ctypes and libespeak-ng to get IPA phonemes from text.
|
||||
Not thread safe.
|
||||
|
||||
Requires libc.so.6
|
||||
Tries to use libespeak-ng.so or libespeak-ng.so.1
|
||||
"""
|
||||
|
||||
SEEK_SET = 0
|
||||
|
||||
|
|
@ -44,7 +57,23 @@ class Phonemizer:
|
|||
keep_language_flags: bool = False,
|
||||
no_stress: bool = False,
|
||||
) -> str:
|
||||
"""Return IPA string for text"""
|
||||
"""
|
||||
Return IPA string for text.
|
||||
Not thread safe.
|
||||
|
||||
Args:
|
||||
text: Text to phonemize
|
||||
voice: optional voice (uses self.default_voice if None)
|
||||
keep_clause_breakers: True if punctuation symbols should be kept
|
||||
phoneme_separator: Separator character between phonemes
|
||||
word_separator: Separator string between words (default: space)
|
||||
punctuation_separator: Separator string between before punctuation (keep_clause_breakers=True)
|
||||
keep_language_flags: True if language switching flags should be kept
|
||||
no_stress: True if stress characters should be removed
|
||||
|
||||
Returns:
|
||||
ipa - string of IPA phonemes
|
||||
"""
|
||||
self._maybe_init()
|
||||
|
||||
voice = voice or self.default_voice
|
||||
|
|
@ -97,13 +126,20 @@ class Phonemizer:
|
|||
Phonemizer.LANG_SWITCH_FLAG.sub("", line) for line in phoneme_lines
|
||||
]
|
||||
|
||||
if word_separator != " ":
|
||||
# Split/re-join words
|
||||
for line_idx in range(len(phoneme_lines)):
|
||||
phoneme_lines[line_idx] = word_separator.join(
|
||||
phoneme_lines[line_idx].split()
|
||||
)
|
||||
|
||||
# Re-insert clause breakers
|
||||
if missing_breakers:
|
||||
# pylint: disable=consider-using-enumerate
|
||||
for line_idx in range(len(phoneme_lines)):
|
||||
if line_idx < len(missing_breakers):
|
||||
phoneme_lines[line_idx] += (
|
||||
word_separator + missing_breakers[line_idx]
|
||||
punctuation_separator + missing_breakers[line_idx]
|
||||
)
|
||||
|
||||
phonemes_str = word_separator.join(line.strip() for line in phoneme_lines)
|
||||
|
|
|
|||
|
|
@ -1,14 +1,20 @@
|
|||
"""Command-line interface to espeak_phonemizer"""
|
||||
import argparse
|
||||
import csv
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
from . import Phonemizer
|
||||
|
||||
_LOGGER = logging.getLogger("espeak_phonemizer")
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(prog="espeak_phonemizer")
|
||||
parser.add_argument("-v", "--voice", required=True, help="eSpeak voice to use")
|
||||
parser.add_argument("-v", "--voice", help="eSpeak voice to use")
|
||||
parser.add_argument(
|
||||
"-p", "--phoneme-separator", help="Separator character between phonemes"
|
||||
)
|
||||
|
|
@ -48,8 +54,30 @@ def main():
|
|||
parser.add_argument(
|
||||
"--csv-delimiter", default="|", help="Delimiter in CSV input and output"
|
||||
)
|
||||
parser.add_argument("--version", action="store_true", help="Print version and exit")
|
||||
parser.add_argument(
|
||||
"--debug", action="store_true", help="Print DEBUG messages to the console"
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.debug:
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
else:
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
|
||||
if args.version:
|
||||
# Print version and exit
|
||||
from . import __version__
|
||||
|
||||
print(__version__)
|
||||
sys.exit(0)
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
|
||||
assert args.voice, "Missing -v/--voice"
|
||||
|
||||
if args.word_separator:
|
||||
assert (
|
||||
args.phoneme_separator.strip()
|
||||
|
|
@ -82,7 +110,7 @@ def main():
|
|||
phoneme_separator=args.phoneme_separator,
|
||||
keep_language_flags=args.keep_language_flags,
|
||||
no_stress=args.no_stress,
|
||||
punctuation_separator=args.phoneme_separator,
|
||||
punctuation_separator=args.phoneme_separator or "",
|
||||
)
|
||||
|
||||
if args.word_separator:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue