Skip to content

Commit

Permalink
refactor: give the API convert functions self-documenting names
Browse files Browse the repository at this point in the history
%s/convert_to_readalong/convert_prealigned_text_to_readalong/g
%s/convert_to_offline_html/convert_prealigned_text_to_offline_html/g
  • Loading branch information
joanise committed Dec 17, 2024
1 parent d47c24b commit 25f2dad
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
17 changes: 11 additions & 6 deletions readalongs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ class like pathlib.Path. Warning: don't just use "/some/path/config.json"
Additional API function:
convert_to_readalong(sentences: Sequence[Sequence[Token]], language: Sequence[str]) -> str:
convert_prealigned_text_to_readalong():
convert a list of sentences into a readalong XML string ready to print to file.
Just like align and make_xml, this function expects a black line (empty list) to
make a paragraph break, and two consecutive blank lines to make a page break.
Unlike the other functions here, this function is not a wrapper around the CLI and
it just returns the string, non status.
it just returns the string, with no status.
convert_prealigned_text_to_offline_html():
same as convert_prealigned_text_to_readalong, but also creates an offline HTML file.
See their respective docstrings for more details.
"""

import io
Expand Down Expand Up @@ -218,7 +223,7 @@ def __init__(
self.is_word = is_word if is_word is not None else bool(time is not None)


def convert_to_readalong(
def convert_prealigned_text_to_readalong(
sentences: Sequence[Sequence[Token]],
language: Sequence[str] = ("und",),
) -> str:
Expand Down Expand Up @@ -267,15 +272,15 @@ def convert_to_readalong(
return xml_text + "\n"


def convert_to_offline_html(
def convert_prealigned_text_to_offline_html(
sentences: Sequence[Sequence[Token]],
audio_file_name: Union[str, os.PathLike],
language: Sequence[str] = ("und",),
title: str = DEFAULT_TITLE,
header: str = DEFAULT_HEADER,
subheader: str = DEFAULT_SUBHEADER,
) -> Tuple[str, str]:
"""Convert a list of sentences/paragraphs/pages of tokens, with corresponding autdio,
"""Convert a list of sentences/paragraphs/pages of tokens, with corresponding audio,
into a readalong Offline HTML
Args:
Expand All @@ -295,7 +300,7 @@ def convert_to_offline_html(
- the readalong XML file contents, ready to print to .readalong
"""

readalong_xml = convert_to_readalong(sentences, language)
readalong_xml = convert_prealigned_text_to_readalong(sentences, language)
try:
readalong_file = tempfile.NamedTemporaryFile(
"w", encoding="utf8", delete=False, suffix=".readalong"
Expand Down
4 changes: 2 additions & 2 deletions test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_deprecated_prepare(self):

def test_convert_to_readalong(self):

readalong = api.convert_to_readalong(self.sentences_to_convert)
readalong = api.convert_prealigned_text_to_readalong(self.sentences_to_convert)
# print(readalong)

# Make the reference by calling align with the same text and adjusting
Expand Down Expand Up @@ -155,7 +155,7 @@ def test_convert_to_readalong(self):
self.assertEqual(readalong, align_result)

def test_convert_to_offline_html(self):
html, _ = api.convert_to_offline_html(
html, _ = api.convert_prealigned_text_to_offline_html(
self.sentences_to_convert,
str(self.data_dir / "noise.mp3"),
subheader="by Jove!",
Expand Down

0 comments on commit 25f2dad

Please sign in to comment.