jp_text_parsing/create-notes.py

97 lines
4.5 KiB
Python

from 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/Cards/'
destination_path = Path(destination_directory_name)
#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)
with open("dump-withFuri.json", "r", encoding="utf-16") as f:
dump = json.load(f)
for cd in dump:
print(dump[cd]['name_J'])
if Path('YGO_2020/2020.full.illust_j.jpg.zib/' + cd + '.jpg').exists():
##TODO: Ew
if Path(destination_path.joinpath('_attachments/' + cd + '.jpg')).exists() == False:
shutil.copy2(Path('YGO_2020/2020.full.illust_j.jpg.zib/' + cd + '.jpg'), destination_path.joinpath('_attachments'))
with open(destination_path.as_posix() + '/' + cd + '.md', 'w') as card_note:
card_note.write("---\n")
card_note.write(f"aliases: [{dump[cd]['name_J']},{dump[cd]['name_E']}]\n")
card_note.write("---\n")
## Name at the top
card_note.write(f"![[{cd}.jpg]]")
card_note.write("\n#### Name\n")
card_note.write("<details><summary>")
card_note.write(dump[cd]['name_J'])
card_note.write("</summary>")
card_note.write(dump[cd]['name_E'])
card_note.write("</details>")
## the description
card_note.write("\n#### Details\n")
card_note.write("<details><summary>")
card_note.write(dump[cd]['desc_J'])
card_note.write("</summary>")
card_note.write(dump[cd]['desc_E'])
card_note.write("</details>")
# desc_Furi
card_note.write("\n#### Reading\n")
if(dump[cd].get("furi")):
card_note.write(f"{dump[cd]['furi']}\n")
else:
card_note.write(dump[cd]['name_J'])
card_note.write("\n")
card_note.write("##### Glossary\n")
for vocab in dump[cd]["glossary_Name_J"]:
print(f"{vocab} : {dump[cd]["glossary_Name_J"][vocab]}", file=card_note)
card_note.write("\n")
if(dump[cd].get("desc_Furi")):
card_note.write(f"{dump[cd]['desc_Furi']}\n")
else:
card_note.write(dump[cd]['desc_J'])
card_note.write("\n")
card_note.write("##### Glossary\n")
for vocab in dump[cd]["glossary_Desc_J"]:
vocab_hash = hashlib.shake_256((vocab + 'Jitendex.org [2026-04-04]').encode()).hexdigest(4)
## Only search and create a vocab file if it doesn't exist
if Path(destination_path.joinpath('vocab/' + vocab_hash + '.md')).exists() == False:
definitions = anki_fields_term(vocab)
if definitions.get("fields"):
with open(destination_path.as_posix() + '/vocab/' + vocab_hash + '.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)
print(f"[[{vocab_hash}|{vocab}]] : {dump[cd]["glossary_Desc_J"][vocab]}", file=card_note)
#print('>' + dump[cd]["glossary_Desc_J"][vocab], file=card_note)
card_note.write("\n")
# foo = anki_fields_term(surface)
# for entry in foo.get("fields"):
# entry['expression']
# entry['glossary-first-brief']
# entry['glossary-plain-no-dictionary']
# entry['frequency-average-rank']