Skip to content

Instantly share code, notes, and snippets.

@goldengrape
Created December 16, 2019 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save goldengrape/31c8a4a3079cc989095a541ad246021f to your computer and use it in GitHub Desktop.
Save goldengrape/31c8a4a3079cc989095a541ad246021f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# coding: utf-8
import pysubs2
import re
from stardict import DictCsv
# depend on https://github.com/skywind3000/ECDICT/
dict_filename="ecdict.csv"
sub_filename="01sub.srt"
check_list=["cet4", "cet6", 'toelf',"gre",'ielts']
sdict=DictCsv(dict_filename)
subs = pysubs2.load(sub_filename, encoding="utf-8")
for line in subs:
s=line.text
words=re.findall(r"\w+", s)
for w in words:
try:
t=sdict.query(w)
if any( c in t['tag'] for c in check_list):
try:
trans=re.findall(r'[\u4e00-\u9fff]+',t['translation'])[0]
trans=w+"("+trans+")"
s=s[0:s.find(w)]+trans+s[s.find(w)+len(w):]
except:
pass
except:
pass
line.text = s
subs.save("my_subtitles_edited.srt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment