joxeankoret

Untitled

Aug 13th, 2024
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. """
  4. Written by Joxean Koret in 2024
  5. Public Domain
  6. """
  7.  
  8. import os
  9. import sys
  10.  
  11. import argostranslate.package
  12. import argostranslate.translate
  13.  
  14. try:
  15.   import readline
  16. except ImportError:
  17.   pass
  18.  
  19. #-------------------------------------------------------------------------------
  20. def translate(from_code : str, to_code : str, text : str) -> str:
  21.   # Download and install Argos Translate package if required
  22.   argostranslate.package.update_package_index()
  23.   available_packages = argostranslate.package.get_available_packages()
  24.  
  25.   try:
  26.     package_to_install = next(
  27.       filter(
  28.           lambda x: x.from_code == from_code and x.to_code == to_code, available_packages
  29.       )
  30.     )
  31.     argostranslate.package.install_from_path(package_to_install.download())
  32.   except StopIteration:
  33.     pass
  34.   except:
  35.     print("Error:", sys.exc_info()[1])
  36.  
  37.   # Translate
  38.   translatedText = argostranslate.translate.translate(text, from_code, to_code)
  39.   return translatedText
  40.  
  41. #-------------------------------------------------------------------------------
  42. def show_help():
  43.   print("""Options:
  44. .quit/.exit       Exit
  45. .from <lang>      Set the language to translate from.
  46. .to   <lang>      Set the language to translate to.
  47. .list             List the pair of supported languages to translate to/from.
  48.  
  49. Anything else will be accumulated in a buffer until a single '.' character is typed in a line.
  50. Example:
  51.  
  52. (es-fr) Hola!
  53. >> QuΓ© tal?
  54. >> .
  55. 'Hola!\nQuΓ© tal?\n'
  56. Bonjour !
  57. Γ‡a va ?
  58.  
  59. """)
  60.  
  61. #-------------------------------------------------------------------------------
  62. def main():
  63.   from_code = "es"
  64.   to_code = "fr"
  65.   text = ""
  66.  
  67.   buf = ""
  68.   prompt = f"({from_code}-{to_code}) "
  69.   while 1:
  70.     try:
  71.       if buf == "":
  72.         prompt = f"({from_code}-{to_code}) "
  73.       else:
  74.         prompt = ">> "
  75.       command = input(prompt)
  76.     except EOFError:
  77.       break
  78.     except KeyboardInterrupt:
  79.       break
  80.  
  81.     command = command.strip(" ")
  82.     if command == "":
  83.       continue
  84.     elif command == "?":
  85.       show_help()
  86.       continue
  87.     elif len(command) > 1 and command.startswith("."):
  88.       args = command[1:].split(" ")
  89.       cmd = args[0].lower()
  90.       if cmd in ["quit", "exit"]:
  91.         break
  92.       elif cmd == "from":
  93.         if len(args) == 1:
  94.           print("Usage: .from <lang_code>")
  95.         else:
  96.           from_code = args[1]
  97.       elif cmd == "to":
  98.         if len(args) == 1:
  99.           print("Usage: .to <lang_code>")
  100.         else:
  101.           to_code = args[1]
  102.       elif cmd == "list":
  103.         pkgs = argostranslate.package.get_available_packages()
  104.         print("\n".join(map(str, pkgs)))
  105.       else:
  106.         print(f"Unknown command '{command}'")
  107.         show_help()
  108.       continue
  109.     elif command.startswith("!"):
  110.       os.system(command[1:])
  111.       continue
  112.  
  113.     if command != ".":
  114.       buf += command + "\n"
  115.       continue
  116.  
  117.     ret = translate(from_code, to_code, buf)
  118.     print(ret)
  119.     buf = ""
  120.  
  121. if __name__ == "__main__":
  122.   main()
  123.  
Advertisement
Add Comment
Please, Sign In to add comment