Skip to content

Commit

Permalink
Get back from_speach.py
Browse files Browse the repository at this point in the history
  • Loading branch information
insolor committed Oct 25, 2024
1 parent 6592951 commit 9aa6b32
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions df_translation_toolkit/create_pot/from_speech.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import sys
from collections.abc import Iterable, Iterator, Sequence
from pathlib import Path
from typing import TextIO

import typer

from df_translation_toolkit.utils.po_utils import TranslationItem, save_pot


def extract_from_speech_file(file: TextIO, source_path: str) -> Iterator[TranslationItem]:
for i, line in enumerate(file, 1):
text = line.rstrip("\n")
if text:
yield TranslationItem(text=text, source_file=source_path, line_number=i)


def extract_translatables(files: Iterable[Path]) -> Iterator[TranslationItem]:
for file_path in files:
if file_path.is_file():
print("File:", file_path.name, file=sys.stderr)
with file_path.open() as file:
yield from extract_from_speech_file(file, file_path.name)


def create_pot_file(pot_file: typer.FileBinaryWrite, files: Sequence[Path]) -> None:
save_pot(
pot_file,
extract_translatables(files),
)


def main(
path: Path,
pot_file: typer.FileBinaryWrite,
) -> None:
files = (file for file in path.glob("*.txt") if file.is_file())
create_pot_file(pot_file, sorted(files))


if __name__ == "__main__":
typer.run(main)

0 comments on commit 9aa6b32

Please sign in to comment.