-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrayleigh.py
64 lines (47 loc) · 1.83 KB
/
rayleigh.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
63
64
import os
import re
import subprocess
from bs4 import BeautifulSoup as bs
AOZORA_PATH = './aozorabunko/cards/'
MAIN_TXT_PATH = './main_texts/'
TSV_PATH = './tsv/'
def process_aozora_card(card_path):
for root, _, files in os.walk(card_path):
for file in files:
if file.endswith('.html'):
process_aozora_html(os.path.join(root, file))
def process_aozora_html(html_path):
cardname = os.path.basename(html_path.replace('.html', ''))
txt_file_path = os.path.join(MAIN_TXT_PATH, cardname+'.txt')
tsv_file_path = os.path.join(TSV_PATH, cardname+'.tsv')
if os.path.exists(tsv_file_path):
return
try:
with open(html_path, 'r', encoding='shift_jis') as f:
html = f.read()
html = strip_ruby(html)
soup = bs(html, 'lxml')
main_texts = soup.find_all('div', {'class': 'main_text'})
for main_text in main_texts:
write_main_text_txt(main_text.text, txt_file_path)
generate_tsv(txt_file_path, tsv_file_path)
except UnicodeDecodeError:
with open('failed.txt', 'a', encoding='utf-8') as f:
f.write(html_path+'\n')
def write_main_text_txt(main_text, txt_file_path):
with open(txt_file_path, 'w', encoding='utf-8') as f:
f.write(main_text)
def generate_tsv(text_file_path, tsv_file_path):
command = ['ginza', '-p', '16', text_file_path]
with open(tsv_file_path, 'w', encoding='utf-8') as f:
subprocess.run(command, stdout=f)
def strip_ruby(text):
pattern = re.compile(r'(?<\/rp><rt>.*<\/rt><rp>)?')
return pattern.sub('', text)
def main():
for root, dirs, _ in os.walk(AOZORA_PATH):
for dir in dirs:
if dir == 'files':
process_aozora_card(os.path.join(root, dir))
if __name__ == '__main__':
main()