-
-
Save rainly/70834598e113f86cc76f6c6711085ed4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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