some tweaks i dont remember

This commit is contained in:
Nitsud Yarg 2026-05-03 09:29:21 -07:00
parent 7ad2e563e1
commit abaf271bf2
2 changed files with 73 additions and 4 deletions

View File

@ -18,6 +18,7 @@ request_url = "http://127.0.0.1:19633"
request_timeout = 10 request_timeout = 10
from fugashi import Tagger from fugashi import Tagger
#import unidic
# Ensure UTF-8 output # Ensure UTF-8 output
@ -47,6 +48,28 @@ def has_japanese(text: str) -> bool:
for ch in text 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: def is_content_word(pos1: str) -> bool:
return pos1 in {"名詞", "動詞", "形容詞", "副詞"} return pos1 in {"名詞", "動詞", "形容詞", "副詞"}

View File

@ -1,5 +1,27 @@
from fugashi_parser import * 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 ---------- # ---------- CLI ----------
def main(): def main():
@ -18,11 +40,12 @@ def main():
# Example: Modify the 3rd line (index 2) # Example: Modify the 3rd line (index 2)
#lines[2] = "This is the new subtitle text\n" #lines[2] = "This is the new subtitle text\n"
## First lien has some unicode bom bs ## First lien has some unicode bom bs
for i in range(len(lines)): i = 0
while i < len(lines):
if re.fullmatch(r'\d+', lines[i].strip()) or i == 0: if re.fullmatch(r'\d+', lines[i].strip()) or i == 0:
notes_lines.append('##### ' + lines[i].strip() + " `" + lines[i+1].strip() + "`" + "\n") notes_lines.append('##### ' + lines[i].strip() + " `" + lines[i+1].strip() + "`" + "\n")
i+=1 i+=2
continue continue
if has_japanese(lines[i]): if has_japanese(lines[i]):
print(lines[i]) print(lines[i])
@ -33,7 +56,30 @@ def main():
if lines[i] != result['kana_reading']: if lines[i] != result['kana_reading']:
lines[i] = result['kana_reading'] + "\n" # lost the new line lines[i] = result['kana_reading'] + "\n" # lost the new line
else: 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: with open("subtitle.srt", "w", encoding="utf-8") as f:
f.writelines(lines) f.writelines(lines)