-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
62 lines (54 loc) · 2.05 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from typing import List
import MeCab
def read_srt(filename: str) -> str:
res = ''
with open(filename, 'r', encoding='utf-8') as f:
for line in f:
if line == '\n':
continue
if '0' <= line[0] <= '9':
continue
res += line
return res
def split_words(text: str) -> List[str]:
tagger = MeCab.Tagger()
parsed = tagger.parse(text)
res = []
for line in parsed.split('\n'):
row = line.split('\t')
if len(row) < 8:
break
if '助詞' in row[4] or '助動詞' in row[4] or '感動詞' in row[4] or '記号' in row[4] or '空白' == row[4]:
continue
if '-' in row[3]:
continue
res.append(row[3])
return res
def make_cards(short_name: str, srt_path: str):
from WordDict import WordDict
wordDict = WordDict()
cards = {}
for ep in range(1, 13):
print(f'processing EP{ep}')
filename = srt_path.format(ep)
text = read_srt(filename).replace('\u3000', ' ')
for line in text.split('\n'):
for word in split_words(line):
if word in cards:
if f'E{ep:02}' in cards[word][4]:
continue
cards[word][4] += f' {short_name}E{ep:02}'
continue
res = wordDict.find(word)
if len(res) > 0:
cards[word] = res+[line] # 汉字,假名,声调,翻译,标签,原句
cards[word][4] += f' {short_name}E{ep:02}'
else:
cards[word] = [word, '-', '-', '-', '-', line]
wordDict.save_exdict()
with open(f'anki/{short_name}.txt', 'w', encoding='utf-8') as f:
for word, v in cards.items():
f.write('\t'.join(v)+'\n')
make_cards(
'点兔S03', 'subtitle/Gochuumon wa Usagi Desuka_ Bloom (01-12, JP only)/Gochuumon wa Usagi Desuka.S03E{:02}.ja.srt')
# make_cards('魔女之旅', 'subtitle/Wandering Witch_The Journey of Elaina/S01E{:02}.ja.srt')