From 3cd4e9279b9f921d4c7b5ad2bdc8fb5a58b65e65 Mon Sep 17 00:00:00 2001 From: Nitsud Yarg Date: Fri, 15 May 2026 18:51:41 -0700 Subject: [PATCH] sub processor accepts parameters --- process_markdown.py | 20 +---------- subtitle_processor.py | 84 ++++++++++++++++++++++--------------------- 2 files changed, 45 insertions(+), 59 deletions(-) diff --git a/process_markdown.py b/process_markdown.py index 498ad36..b0cf75a 100644 --- a/process_markdown.py +++ b/process_markdown.py @@ -38,7 +38,7 @@ def main(): file_changed = False while i < len(lines): ## Skip lines that start with certain elements - if lines[i][0:6] == '' or lines[i][0] in ('#','>'): + if lines[i][0:6] == '' or lines[i][0] in ('#','>','['): i+=1 continue ## If the line has japanese @@ -57,24 +57,6 @@ def main(): ## Lookup vocab for word in vocab: write_word_note(word, destination_path) - # if Path(destination_path.joinpath('/' + word + '.md')).exists() == False: - # definitions = anki_fields_term(word) - # if definitions.get("fields"): - # with open(destination_path.as_posix() + '/' + word + '.md', 'w') as vocab_note: - # for entry in definitions.get("fields"): - # #vocab_note.write("\n") - # #print(entry['expression'], file=vocab_note) - # #print(entry['furigana-plain'], file=vocab_note) - # print(entry['furigana'], file=vocab_note) - # if entry['expression'] != entry['reading']: - # print(entry['reading'], file=vocab_note) - # print("> ",re.sub(r'<.*?>', '', entry['glossary-plain-no-dictionary']), "\n", file=vocab_note) - # #print(f"Part of Speech: {entry['part-of-speech']}", file=vocab_note) - # print(f"Frequency: {entry['frequency-average-rank']}", file=vocab_note) - # #print(entry['glossary-plain-no-dictionary'], file=vocab_note) - # vocab_note.write(entry['glossary-first']) - # #print("##### Other Definitions", file=vocab_note) - # print("\n", file=vocab_note) ## Output file with changes if changes with open(args.markdown_file, "w", encoding="utf-8") as f: diff --git a/subtitle_processor.py b/subtitle_processor.py index d4e0bee..6490091 100644 --- a/subtitle_processor.py +++ b/subtitle_processor.py @@ -6,33 +6,48 @@ 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) +# 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]' +# dictionary_name = 'Jitendex.org [2026-04-04]' -#print(annotate_phrase("私は学校へ行きます")) +# #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) +# ## 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("text", nargs="*") -# args = parser.parse_args() + 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() - vocab = load_vocab('foo.json') + 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: + # 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() @@ -64,32 +79,21 @@ def main(): i+=1 ## Lookup vocab - if False: - for word in vocab: - #vocab_hash = hashlib.shake_256((word + dictionary_name).encode()).hexdigest(4) - if Path(destination_path.joinpath('vocab/' + word + '.md')).exists() == False: - definitions = anki_fields_term(word) - if definitions.get("fields"): - with open(destination_path.as_posix() + '/vocab/' + word + '.md', 'w') as vocab_note: - for entry in definitions.get("fields"): - vocab_note.write("\n") - print(entry['expression'], file=vocab_note) - #print(entry['furigana-plain'], file=vocab_note) - if entry['expression'] != entry['reading']: - print(entry['reading'], file=vocab_note) - print(entry['furigana'], file=vocab_note) - #print(f"Part of Speech: {entry['part-of-speech']}", file=vocab_note) - print(f"Frequency: {entry['frequency-average-rank']}", file=vocab_note) - #print(entry['glossary-plain-no-dictionary'], file=vocab_note) - vocab_note.write(entry['glossary-first']) - print("##### Other Definitions", file=vocab_note) - print(entry['glossary-plain-no-dictionary'], file=vocab_note) + 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: + # 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) - # with open("notestest-newchunks.md", "w", encoding="utf-8") as f: - # f.writelines(notes_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)