Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented phonemizer cache #199

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phonemizer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
from .phonemize import phonemize # pylint: disable=unused-import


__version__ = '3.3.0'
__version__ = '3.3.0.1'
"""Phonemizer version"""
21 changes: 21 additions & 0 deletions phonemizer/phonemize.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

Backend = Literal['espeak', 'espeak-mbrola', 'festival', 'segments']

_PHONEMIZER_CACHE = {}

def phonemize( # pylint: disable=too-many-arguments
text,
Expand Down Expand Up @@ -185,6 +186,7 @@ def phonemize( # pylint: disable=too-many-arguments
if the ``language`` is not supported by the ``backend``, if any incompatible options are used.

"""

# ensure we are using a compatible Python version
if sys.version_info < (3, 6): # pragma: nocover
logger.error(
Expand All @@ -201,6 +203,22 @@ def phonemize( # pylint: disable=too-many-arguments
if backend == 'espeak-mbrola' and separator.word:
logger.warning('espeak-mbrola backend cannot preserve word separation')

# cache handling: if this instance has been created in the past, everything should be A-OK
cache_key = (
backend,
language,
str(punctuation_marks),
preserve_punctuation,
with_stress,
tie,
language_switch,
words_mismatch
)

# return the backend from the cache if it exists
if cache_key in _PHONEMIZER_CACHE:
return _phonemize(_PHONEMIZER_CACHE[cache_key], text, separator, strip, njobs, prepend_text, preserve_empty_lines)

# initialize the phonemization backend
if backend == 'espeak':
phonemizer = BACKENDS[backend](
Expand All @@ -223,6 +241,9 @@ def phonemize( # pylint: disable=too-many-arguments
preserve_punctuation=preserve_punctuation,
logger=logger)

# after successfully initializing the backend, cache it
_PHONEMIZER_CACHE[cache_key] = phonemizer

# do the phonemization
return _phonemize(phonemizer, text, separator, strip, njobs, prepend_text, preserve_empty_lines)

Expand Down