From abaf271bf26f95f38d172c3577acad32c74dae61 Mon Sep 17 00:00:00 2001 From: Nitsud Yarg Date: Sun, 3 May 2026 09:29:21 -0700 Subject: [PATCH] some tweaks i dont remember --- fugashi_parser.py | 23 ++++++++++++++++++ subtitle_processor.py | 54 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/fugashi_parser.py b/fugashi_parser.py index a153b69..92f76d4 100644 --- a/fugashi_parser.py +++ b/fugashi_parser.py @@ -18,6 +18,7 @@ request_url = "http://127.0.0.1:19633" request_timeout = 10 from fugashi import Tagger +#import unidic # Ensure UTF-8 output @@ -47,6 +48,28 @@ def has_japanese(text: str) -> bool: for ch in text ) +def anki_fields_term(term : str) -> dict: + params = { + "text": term, + "type": "term", + #"markers": ["audio", "cloze-body-kana", "conjugation", "expression", "furigana", "furigana-plain", "glossary", "glossary-brief", "glossary-no-dictionary", "glossary-first", "glossary-first-brief", "glossary-first-no-dictionary", "part-of-speech", "phonetic-transcriptions", "pitch-accents", "pitch-accent-graphs", "pitch-accent-graphs-jj", "pitch-accent-positions", "pitch-accent-categories", "reading", "tags", "clipboard-image", "clipboard-text", "cloze-body", "cloze-prefix", "cloze-suffix", "dictionary", "dictionary-alias", "document-title", "frequencies", "frequency-harmonic-rank", "frequency-harmonic-occurrence", "frequency-average-rank", "frequency-average-occurrence", "screenshot", "search-query", "popup-selection-text", "sentence", "sentence-furigana", "sentence-furigana-plain", "url"], + "markers": ["audio", "expression", "cloze-body" , "furigana","furigana-plain", "part-of-speech", "reading", "glossary-first", "glossary-plain-no-dictionary" , "glossary-first-no-dictionary" , "dictionary", "frequencies", "frequency-average-rank", "frequency-average-occurrence", "screenshot", "sentence", "sentence-furigana", "sentence-furigana-plain" ], + "maxEntries": 2, + "includeMedia": False, + } + response = requests.post(request_url + "/ankiFields", json = params, timeout = request_timeout) + return json.loads(response.text) + +def term_entries(term :str) -> dict: + #print("Requesting termEntries:") + params = { + "term": term, + "maxEntries": 1, + "includeMedia": False, + } + response = requests.post(request_url + "/termEntries", json = params, timeout = request_timeout) + return json.loads(response.text) + def is_content_word(pos1: str) -> bool: return pos1 in {"名詞", "動詞", "形容詞", "副詞"} diff --git a/subtitle_processor.py b/subtitle_processor.py index ae0a3cb..ce88236 100644 --- a/subtitle_processor.py +++ b/subtitle_processor.py @@ -1,5 +1,27 @@ 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(): @@ -18,11 +40,12 @@ def main(): # Example: Modify the 3rd line (index 2) #lines[2] = "This is the new subtitle text\n" - ## First lien has some unicode bom bs - for i in range(len(lines)): + ## First lien has some unicode bom bs + i = 0 + while i < len(lines): if re.fullmatch(r'\d+', lines[i].strip()) or i == 0: notes_lines.append('##### ' + lines[i].strip() + " `" + lines[i+1].strip() + "`" + "\n") - i+=1 + i+=2 continue if has_japanese(lines[i]): print(lines[i]) @@ -33,7 +56,30 @@ def main(): 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].strip() + "`" + "\n") + notes_lines.append(lines[i]) + i+=1 + + ## Lookup vocab + 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) with open("subtitle.srt", "w", encoding="utf-8") as f: f.writelines(lines)