Guest User

Untitled

a guest
Jan 11th, 2025
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. from translate import Translator
  2. import re
  3. import codecs
  4. import glob
  5. import time
  6. import os
  7. import os.path
  8. #from nltk.tokenize import sent_tokenize
  9. #import nltk
  10.  
  11. result = []
  12.  
  13. def process_one_file(filename):
  14. f = open("dialog/"+filename+"",'r')
  15. #lines = f.readlines()
  16. data0 = f.read()
  17. data = data0
  18.  
  19. f = re.findall(r'{\d+}{.*?}{([\w\W]*?)}', data,re.MULTILINE)
  20. all_lines = set()
  21. for txt in f:
  22. all_lines.add(txt)
  23.  
  24. all_lines = list(all_lines)
  25. all_lines = sorted(all_lines, key=len, reverse=True)
  26.  
  27. for txt in all_lines:
  28. if len(txt)>500:
  29. continue
  30. translation = ""
  31. while True:
  32. translator = Translator(to_lang="ru")
  33. try:
  34. translation = translator.translate(txt)
  35. except:
  36. print("network error!")
  37. time.sleep(3)
  38. continue
  39.  
  40. if "MYMEMORY WARNING:" in translation:
  41. print("restart tor!")
  42. time.sleep(3)
  43. continue
  44.  
  45. print(translation)
  46. break
  47.  
  48. data = data.replace(txt,translation)
  49.  
  50. f = open("dialog_new/"+filename+"",'wb')
  51. data2 = bytes(data, "cp866", errors='ignore')
  52.  
  53. f.write(data2)
  54.  
  55. files = glob.glob("dialog/*.msg")
  56.  
  57. for filename in files:
  58. filename = filename.replace("\\","/")
  59. filename = filename.split("/")[1]
  60. print(filename)
  61. print("=============================================")
  62. if os.path.exists("dialog_new/"+filename):
  63. continue
  64. process_one_file(filename)
Advertisement
Add Comment
Please, Sign In to add comment