making notes out of json data. First attempt.
This commit is contained in:
parent
12b59d6847
commit
0c1eebd071
|
|
@ -76,7 +76,7 @@ def anki_fields_term(term : str) -> dict:
|
||||||
return json.loads(response.text)
|
return json.loads(response.text)
|
||||||
|
|
||||||
def term_entries(term :str) -> dict:
|
def term_entries(term :str) -> dict:
|
||||||
print("Requesting termEntries:")
|
#print("Requesting termEntries:")
|
||||||
params = {
|
params = {
|
||||||
"term": term,
|
"term": term,
|
||||||
"maxEntries": 1,
|
"maxEntries": 1,
|
||||||
|
|
@ -144,12 +144,15 @@ for cd in dump:
|
||||||
print(anottated['reading'])
|
print(anottated['reading'])
|
||||||
dump[cd]['kana'] = anottated['reading']
|
dump[cd]['kana'] = anottated['reading']
|
||||||
cd_nm = dump[cd]['name_J']
|
cd_nm = dump[cd]['name_J']
|
||||||
|
if (anottated.get('vocab')):
|
||||||
|
dump[cd]['glossary_Name_J'] = list()
|
||||||
for word in anottated['vocab']:
|
for word in anottated['vocab']:
|
||||||
# Skip punctuation words hopefully
|
# Skip punctuation words hopefully
|
||||||
if len(word) == 1 and has_kanji(word) == False:
|
if len(word) == 1 and has_kanji(word) == False:
|
||||||
continue
|
continue
|
||||||
if vocab_dict.get(word) is None:
|
if vocab_dict.get(word) is None:
|
||||||
vocab_dict[word] = {
|
vocab_dict[word] = {
|
||||||
|
'Def.': "; ".join(get_glossary(word)),
|
||||||
'count': 0,
|
'count': 0,
|
||||||
'Examples': (cd_nm,)
|
'Examples': (cd_nm,)
|
||||||
}
|
}
|
||||||
|
|
@ -157,17 +160,22 @@ for cd in dump:
|
||||||
vocab_dict[word]['count'] = vocab_dict[word]['count'] + 1
|
vocab_dict[word]['count'] = vocab_dict[word]['count'] + 1
|
||||||
if cd_nm not in vocab_dict[word]['Examples']:
|
if cd_nm not in vocab_dict[word]['Examples']:
|
||||||
vocab_dict[word]['Examples'] += (cd_nm,)
|
vocab_dict[word]['Examples'] += (cd_nm,)
|
||||||
|
if vocab_dict.get(word):
|
||||||
|
dump[cd]['glossary_Name_J'].append(f"{word}: {vocab_dict[word]['Def.']}")
|
||||||
print()
|
print()
|
||||||
phrase = dump[cd]['desc_J']
|
phrase = dump[cd]['desc_J']
|
||||||
anottated = annotate_phrase(phrase)
|
anottated = annotate_phrase(phrase)
|
||||||
if anottated['reading'] != phrase:
|
if anottated['reading'] != phrase:
|
||||||
#print(anottated['reading'])
|
#print(anottated['reading'])
|
||||||
dump[cd]['desc_Furi'] = anottated['reading']
|
dump[cd]['desc_Furi'] = anottated['reading']
|
||||||
|
if (anottated.get('vocab')):
|
||||||
|
dump[cd]['glossary_Desc_J'] = list()
|
||||||
for word in anottated['vocab']:
|
for word in anottated['vocab']:
|
||||||
if len(word) == 1 and has_kanji(word) == False:
|
if len(word) == 1 and has_kanji(word) == False:
|
||||||
continue
|
continue
|
||||||
if vocab_dict.get(word) is None:
|
if vocab_dict.get(word) is None:
|
||||||
vocab_dict[word] = {
|
vocab_dict[word] = {
|
||||||
|
'Def.': "; ".join(get_glossary(word)),
|
||||||
'count': 0,
|
'count': 0,
|
||||||
'Examples': (cd_nm,)
|
'Examples': (cd_nm,)
|
||||||
}
|
}
|
||||||
|
|
@ -176,6 +184,8 @@ for cd in dump:
|
||||||
if len(vocab_dict[word]['Examples']) < 5:
|
if len(vocab_dict[word]['Examples']) < 5:
|
||||||
if cd_nm not in vocab_dict[word]['Examples']:
|
if cd_nm not in vocab_dict[word]['Examples']:
|
||||||
vocab_dict[word]['Examples'] += (cd_nm,)
|
vocab_dict[word]['Examples'] += (cd_nm,)
|
||||||
|
if vocab_dict.get(word):
|
||||||
|
dump[cd]['glossary_Desc_J'].append(f"{word}: {vocab_dict[word]['Def.']}")
|
||||||
|
|
||||||
vocab_dict = sorted(vocab_dict.items(), key= lambda x: x[1]['count'], reverse=True)
|
vocab_dict = sorted(vocab_dict.items(), key= lambda x: x[1]['count'], reverse=True)
|
||||||
|
|
||||||
|
|
|
||||||
118
create-notes.py
Normal file
118
create-notes.py
Normal file
|
|
@ -0,0 +1,118 @@
|
||||||
|
import json
|
||||||
|
import MeCab
|
||||||
|
import re
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
request_url = "http://127.0.0.1:19633"
|
||||||
|
request_timeout = 10
|
||||||
|
|
||||||
|
destination_directory_name = '/run/media/deck/YF8SD/Sync/Notebooks/Obsidian/Japanese/Yu-Gi-Oh/Cards/'
|
||||||
|
destination_path = Path(destination_directory_name)
|
||||||
|
|
||||||
|
# Generate Hiragana and Katakana characters
|
||||||
|
hiragana = tuple(chr(i) for i in range(12353, 12436)) # \u3041 - \u3094
|
||||||
|
katakana = tuple(chr(i) for i in range(12450, 12532)) # \u30A2 - \u30F4
|
||||||
|
punctuation = tuple(chr(i) for i in range(65281, 65382))
|
||||||
|
|
||||||
|
# Combine both into one tuple
|
||||||
|
all_kana = hiragana + katakana + punctuation + (' ', 'ー', '・')
|
||||||
|
|
||||||
|
KANJI_RE = re.compile(r"[\u4E00-\u9FFF]")
|
||||||
|
|
||||||
|
def katakana_to_hiragana(text: str) -> str:
|
||||||
|
return "".join(
|
||||||
|
chr(ord(ch) - 0x60) if 0x30A1 <= ord(ch) <= 0x30F6 else ch
|
||||||
|
for ch in text
|
||||||
|
)
|
||||||
|
|
||||||
|
# def get_node_reading(node) -> str:
|
||||||
|
# features = node.feature.split(",")
|
||||||
|
# print()
|
||||||
|
# if len(features) >= 8 and features[7] != "*":
|
||||||
|
# return katakana_to_hiragana(features[7])
|
||||||
|
# return node.surface
|
||||||
|
|
||||||
|
def has_kanji(text: str) -> bool:
|
||||||
|
return bool(KANJI_RE.search(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"],
|
||||||
|
"maxEntries": 1,
|
||||||
|
"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 get_glossary(word :str) -> list:
|
||||||
|
glossary = list()
|
||||||
|
dict_items = term_entries(word)
|
||||||
|
|
||||||
|
entry = next(iter(dict_items.get("dictionaryEntries",[])),None)
|
||||||
|
if entry:
|
||||||
|
definition = next(iter(entry.get("definitions",[])),None)
|
||||||
|
if definition:
|
||||||
|
def_entry = next(iter(definition.get("entries", [])),None)
|
||||||
|
if def_entry:
|
||||||
|
glossary.extend( list(extract_text_by_type(def_entry, "glossary")) )
|
||||||
|
return glossary[:2]
|
||||||
|
|
||||||
|
#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)
|
||||||
|
|
||||||
|
with open("dump2.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<details><summary>\n")
|
||||||
|
card_note.write(dump[cd]['name_J'])
|
||||||
|
card_note.write("\n</summary>\n")
|
||||||
|
card_note.write(dump[cd]['name_E'])
|
||||||
|
card_note.write("\n</details>\n")
|
||||||
|
if(dump[cd].get("kana")):
|
||||||
|
card_note.write(f"{dump[cd]['kana']}\n")
|
||||||
|
## the description
|
||||||
|
card_note.write(dump[cd]['desc_J'])
|
||||||
|
card_note.write("<details><summary>\n")
|
||||||
|
card_note.write("Translation")
|
||||||
|
card_note.write("\n</summary>\n")
|
||||||
|
card_note.write(dump[cd]['desc_E'])
|
||||||
|
card_note.write("\n</details>\n")
|
||||||
|
# desc_Furi
|
||||||
|
if(dump[cd].get("desc_Furi")):
|
||||||
|
card_note.write(f"{dump[cd]['desc_Furi']}\n")
|
||||||
|
for vocab in dump[cd]["glossary_Desc_J"]:
|
||||||
|
print(vocab, file=card_note)
|
||||||
Loading…
Reference in New Issue
Block a user