mattrguerra/transcription-pipelinemainPrivate on GitHub, published here in full

test_sentences.py

Language Python1.3 KB36 lines

tests/test_sentences.py

  1. """split_sentences: known cases, offsets, and the golden file.
  2. tests/fixtures/sentences-golden.json holds what the original JavaScript
  3. splitter returned for each input, generated once from that code. The Python
  4. port must reproduce it exactly.
  5. """
  6. import json
  7. from pathlib import Path
  8. from transcription_pipeline.sentences import split_sentences
  9. GOLDEN = Path(__file__).resolve().parent / "fixtures" / "sentences-golden.json"
  10. def test_known_cases():
  11. assert [s["text"] for s in split_sentences("Hello there. How are you? I'm fine!")] == \
  12. ["Hello there.", "How are you?", "I'm fine!"]
  13. assert len(split_sentences("Um... yeah I think so.")) == 1
  14. assert len(split_sentences("Growth was 3.5 percent.")) == 1
  15. assert [s["text"] for s in split_sentences('He said "stop." Then he left.')] == \
  16. ['He said "stop."', "Then he left."]
  17. def test_offsets_point_into_the_text():
  18. text = "Dr. Smith arrived. He sat down. Then... nothing."
  19. for s in split_sentences(text):
  20. assert text[s["offset"]:s["offset"] + len(s["text"])] == s["text"]
  21. def test_matches_the_golden_file():
  22. cases = json.loads(GOLDEN.read_text(encoding="utf-8"))
  23. assert cases
  24. for case in cases:
  25. got = split_sentences(case["input"])
  26. assert got == case["sentences"], f"mismatch on {case['input']!r}"