dicta_api.py 977 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from os import environ
  2. from requests import post
  3. from scriptshifter.exceptions import BREAK, UpstreamError
  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. try:
  19. rsp.raise_for_status()
  20. except Exception:
  21. raise UpstreamError("Error received from Dicta service.")
  22. rom = rsp.json().get("transliteration")
  23. if rom:
  24. if ctx.options["capitalize"] == "all":
  25. rom = capitalize(rom)
  26. elif ctx.options["capitalize"] == "first":
  27. rom = rom[0].upper() + rom[1:]
  28. else:
  29. ctx.warnings.append("Upstream service returned empty result.")
  30. ctx.dest = rom
  31. return BREAK