stitching it all together now.
This commit is contained in:
parent
4b2cf5d0fa
commit
12b59d6847
|
|
@ -2,8 +2,10 @@ import json
|
||||||
import MeCab
|
import MeCab
|
||||||
import re
|
import re
|
||||||
|
|
||||||
# Initialize the Tagger
|
import requests
|
||||||
#tagger = MeCab.Tagger()
|
|
||||||
|
request_url = "http://127.0.0.1:19633"
|
||||||
|
request_timeout = 10
|
||||||
|
|
||||||
# Generate Hiragana and Katakana characters
|
# Generate Hiragana and Katakana characters
|
||||||
hiragana = tuple(chr(i) for i in range(12353, 12436)) # \u3041 - \u3094
|
hiragana = tuple(chr(i) for i in range(12353, 12436)) # \u3041 - \u3094
|
||||||
|
|
@ -21,12 +23,12 @@ def katakana_to_hiragana(text: str) -> str:
|
||||||
for ch in text
|
for ch in text
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_node_reading(node) -> str:
|
# def get_node_reading(node) -> str:
|
||||||
features = node.feature.split(",")
|
# features = node.feature.split(",")
|
||||||
print()
|
# print()
|
||||||
if len(features) >= 8 and features[7] != "*":
|
# if len(features) >= 8 and features[7] != "*":
|
||||||
return katakana_to_hiragana(features[7])
|
# return katakana_to_hiragana(features[7])
|
||||||
return node.surface
|
# return node.surface
|
||||||
|
|
||||||
def has_kanji(text: str) -> bool:
|
def has_kanji(text: str) -> bool:
|
||||||
return bool(KANJI_RE.search(text))
|
return bool(KANJI_RE.search(text))
|
||||||
|
|
@ -56,6 +58,72 @@ def annotate_phrase(text: str) -> str:
|
||||||
|
|
||||||
return {'reading':("".join(parts)),'vocab':vocab}
|
return {'reading':("".join(parts)),'vocab':vocab}
|
||||||
|
|
||||||
|
def elide(text: str) -> str:
|
||||||
|
elide_max_length = 100
|
||||||
|
if len(text) > elide_max_length:
|
||||||
|
return text[:100] + "..."
|
||||||
|
return 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)
|
||||||
|
|
||||||
|
# perhaps set up a filter parameter.
|
||||||
|
def flatten_string(node):
|
||||||
|
if isinstance(node,str):
|
||||||
|
yield node
|
||||||
|
elif isinstance(node, list):
|
||||||
|
for item in node:
|
||||||
|
yield from flatten_string(item)
|
||||||
|
elif isinstance(node,dict):
|
||||||
|
if isinstance(node.get("content"), str):
|
||||||
|
yield node["content"]
|
||||||
|
else:
|
||||||
|
for v in node.values():
|
||||||
|
yield from flatten_string(v)
|
||||||
|
|
||||||
|
def extract_text_by_type(node, target_type):
|
||||||
|
if isinstance(node,dict):
|
||||||
|
data = node.get("data")
|
||||||
|
if isinstance(data,dict) and data.get("content") == target_type:
|
||||||
|
yield from flatten_string(node.get("content"))
|
||||||
|
#yield from ",".join(node.get("content"))
|
||||||
|
for value in node.values():
|
||||||
|
yield from extract_text_by_type(value, target_type)
|
||||||
|
elif isinstance(node,list):
|
||||||
|
for item in node:
|
||||||
|
yield from extract_text_by_type(item,target_type)
|
||||||
|
|
||||||
|
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("私は学校へ行きます"))
|
#print(annotate_phrase("私は学校へ行きます"))
|
||||||
|
|
||||||
with open("dump2.json", "r", encoding="utf-16") as f:
|
with open("dump2.json", "r", encoding="utf-16") as f:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user