jp_text_parsing/make_dialog_notes.py

102 lines
5.3 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/'
destination_path = Path(destination_directory_name)
vocab_destination_path = Path('/run/media/deck/YF8SD/Sync/Notebooks/Obsidian/Japanese/vocab')
with open("scripts_dump2.json", "r", encoding="utf-8") as f:
dump = json.load(f)
if not destination_path.exists():
destination_path.mkdir(parents=True, exist_ok=True)
if not destination_path.joinpath('YLOTD_dialog').exists():
destination_path.joinpath('YLOTD_dialog').mkdir(parents=True, exist_ok=True)
counter = 0
destination_path = destination_path.joinpath('YLOTD_dialog')
for title in dump:
print(title)
counter+=1
## copy the image if it isn't already there
with open(destination_path.as_posix() + f"/{counter:03}_{title}.md", 'w') as card_note:
for block in dump[title]:
print(block)
card_note.write(f"\n###### {block}\n\n")
#reformatted = re.sub(r'(\d{2})/(\d{2})/(\d{4})', r'\3-\1-\2', date)
#re.replace('\$R')
text_J = dump[title][block]['text_J']
text_J = re.sub(r'\$R(.*?)\((.*?)\)',r'<ruby>\1<rt>\2</rt></ruby>',text_J)
print(f"<details><summary>{text_J}</summary>{dump[title][block]['text_E']}</details>",file=card_note)
text_J = dump[title][block]['text_J']
text_J = re.sub(r'\$R(.*?)\((.*?)\)',r'\1',text_J)
text_J = text_J.replace('<br>', '')
vocab = {} ## flush the vocab variable each time
result = analyze(text_J, vocab)
print(f"> [!summary]- Vocab\n>{result['notes_link']}",file=card_note)
for word in vocab:
write_word_note(word, vocab_destination_path, vocab[word]['pos'][0])
# ## add card names as properties
# 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 image
# 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")
# vocab = {} ## flush the vocab variable each time
# result = analyze(dump[cd]['name_J'],vocab)
# print(f"<span>{result['ruby_html']}</span>\n> [!summary]- Vocab\n>{result['notes_link']}\n",file=card_note)
# ## Generate the vocab item
# for word in vocab:
# 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"):
# 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"Frequency: {entry['frequency-average-rank']}", file=vocab_note)
# vocab_note.write(entry['glossary-first'])
# print("\n", file=vocab_note)
# # 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)
# vocab = {} ## flush the vocab variable each time
# result = analyze(dump[cd]['desc_J'],vocab)
# #card_note.write("\n")
# print(f"<span>{result['ruby_html']}</span>\n> [!summary]- Vocab\n>{result['notes_link']}\n",file=card_note)
# for word in vocab:
# 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"):
# 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"Frequency: {entry['frequency-average-rank']}", file=vocab_note)
# vocab_note.write(entry['glossary-first'])
# print("\n", file=vocab_note)
# card_note.write("\n")