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

Get back from_speach.py #108

Merged
merged 1 commit into from
Oct 25, 2024
Merged
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
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)