jp_text_parsing/subtitle_processor.py

111 lines
4.0 KiB
Python

from fugashi_parser import *
import os
from pathlib import Path
import shutil
import hashlib
# destination_directory_name = '/run/media/deck/YF8SD/Sync/Notebooks/Obsidian/Japanese/Yu-Gi-Oh/AnimeSubs/'
# destination_path = Path(destination_directory_name)
# dictionary_name = 'Jitendex.org [2026-04-04]'
# #print(annotate_phrase("私は学校へ行きます"))
# ## Make sure directories exist
# if not destination_path.exists():
# destination_path.mkdir(parents=True, exist_ok=True)
# if not destination_path.joinpath('_attachments').exists():
# destination_path.joinpath('_attachments').mkdir(parents=True, exist_ok=True)
# if not destination_path.joinpath('vocab').exists():
# destination_path.joinpath('vocab').mkdir(parents=True, exist_ok=True)
# ---------- CLI ----------
def main():
parser = argparse.ArgumentParser()
#parser.add_argument("--vocab-file", required=True)
parser.add_argument("--subtitle-file", required=True)
parser.add_argument("--vocab-folder", required=False)
args = parser.parse_args()
if args.vocab_folder:
destination_path = Path(args.vocab_folder)
if destination_path.is_dir() == False:
raise ValueError("Vocab Folder is not a directory.")
return
else:
destination_path = Path(args.subtitle_file).parent
if not destination_path.joinpath('vocab').exists():
destination_path.joinpath('vocab').mkdir(parents=True, exist_ok=True)
destination_path = destination_path.joinpath('vocab')
# with open("/run/media/deck/YF8SD/Video/Yu-Gi-Oh! Duel Monsters - 001.vtt", "r", encoding="utf-8") as f:
# lines = f.readlines()
vocab = {} #load_vocab('foo.json')
## Currently just doing this line by line
with open(args.subtitle_file, "r", encoding="utf-8") as f:
lines = f.readlines()
notes_lines = []
# Example: Modify the 3rd line (index 2)
#lines[2] = "This is the new subtitle text\n"
## First lien has some unicode bom bs
i = 0
while i < len(lines):
## If the line starts with a single number build a header
if re.fullmatch(r'\d+', lines[i].strip()) or i == 0:
notes_lines.append('##### ' + lines[i].strip() + " `" + lines[i+1].strip() + "`" + "\n")
i+=2
continue
if re.fullmatch(r'(\d{2,}:)*\d{2,}:\d{2,}[,.]\d{3,}\s*-->\s*(\d{2,}:)*\d{2,}:\d{2,}[,.]\d{3,}' , lines[i].strip()):
notes_lines.append('##### ' " `" + lines[i].strip() + "`" + "\n")
i+=1
continue
if has_japanese(lines[i]):
print(lines[i])
result = analyze(lines[i],vocab)
print(result['kana_reading'])
notes_lines.append(result["ruby_html"] + "\n" )
notes_lines.append (result['notes_link'] + "\n")
if lines[i] != result['ruby_html']:
lines[i] = result['ruby_html'] + "\n" # lost the new line
# if lines[i] != result['kana_reading']:
# lines[i] = result['kana_reading'] + "\n" # lost the new line
else:
#notes_lines.append("`" + lines[i].strip() + "`" + "\n")
notes_lines.append(lines[i])
i+=1
## Lookup vocab
for word in vocab:
write_word_note(word, destination_path)
# with open("/run/media/deck/YF8SD/Video/Yu-Gi-Oh! Duel Monsters - 001.vtt", "w", encoding="utf-8") as f:
# f.writelines(lines)
## Output file with changes if changes
out_dir = Path(args.subtitle_file).parent
changed_file = out_dir.joinpath(Path(args.subtitle_file).stem + '.txt')
with open(changed_file, "w", encoding="utf-8") as f:
f.writelines(lines)
note_file = out_dir.joinpath(Path(args.subtitle_file).stem + '.md')
with open(note_file, "w", encoding="utf-8") as f:
f.writelines(notes_lines)
#vocab = load_vocab(args.vocab_file)
#result = analyze(text, vocab)
#save_vocab('foo.json', vocab)
#result["vocab_file"] = args.vocab_file
#print(json.dumps(result, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()