dicta_api.py 861 B

12345678910111213141516171819202122232425262728293031323334353637
  1. from os import environ
  2. from requests import post
  3. from scriptshifter.exceptions import BREAK
  4. from scriptshifter.tools import capitalize
  5. EP = environ.get("TXL_DICTA_EP")
  6. DEFAULT_GENRE = "rabbinic"
  7. def s2r_post_config(ctx):
  8. """
  9. Romanize Hebrew text using the Dicta API service.
  10. """
  11. ctx.warnings = []
  12. rsp = post(
  13. EP,
  14. json={
  15. "data": ctx.src,
  16. "genre": ctx.options.get("genre", DEFAULT_GENRE)
  17. })
  18. rsp.raise_for_status()
  19. rom = rsp.json().get("transliteration")
  20. if rom:
  21. if ctx.options["capitalize"] == "all":
  22. rom = capitalize(rom)
  23. elif ctx.options["capitalize"] == "first":
  24. rom = rom[0].upper() + rom[1:]
  25. else:
  26. ctx.warnings.append("Upstream service returned empty result.")
  27. ctx.dest = rom
  28. return BREAK