-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjotoba.py
176 lines (129 loc) · 4.35 KB
/
jotoba.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from typing import Optional, List
import requests
import json
from aqt import mw
from .utils import log
config = mw.addonManager.getConfig(__name__)
log(config)
LANGUAGE = config["Language"]
JOTOBA_URL = config["Jotoba_URL"]
WORDS_API_URL = JOTOBA_URL + config["API_Words_Suffix"]
SENTENCE_API_URL = JOTOBA_URL + config["API_Sentence_Suffix"]
class Word:
expression: str
reading: str
glosses: List[str]
pitch: str
part_of_speech: List[str]
audio_url: str
def __init__(self, word):
if not word:
return
if "kanji" in word["reading"]:
self.expression = word["reading"]["kanji"]
self.reading = word["reading"]["kana"]
else:
self.expression = word["reading"]["kana"]
self.reading = ""
self.pitch = get_pitch_html(word)
self.glosses = get_glosses(word)
self.part_of_speech = get_pos(word)
if "audio" in word:
self.audio_url = JOTOBA_URL + word["audio"]
def request_sentence(text):
return json.loads(request(SENTENCE_API_URL, text).text)["sentences"]
def request_word(text, kana=""):
return find_word(json.loads(request(WORDS_API_URL, text).text), text, kana)
def request(URL, text):
data = '{"query":"' + text + '","language":"' + LANGUAGE + '","no_english":true}'
headers = {"Content-Type": "application/json; charset=utf-8", "Accept": "application/json"}
return requests.post(URL, data=data.encode('utf-8'), headers=headers)
def find_word(res, expr, kana="") -> Optional[Word]:
words = res["words"]
potential_words = []
kana_words = []
for word in words:
reading = word["reading"]
if "kanji" in reading:
if reading["kanji"] == expr and (reading["kana"] == kana or kana == ""):
potential_words.append(word)
elif reading["kana"] == expr or reading["kana"] == kana: # kana word has kanji writing or kanji writing is different from expr
kana_words.append(word)
else:
if reading["kana"] == expr:
potential_words.append(word)
if len(potential_words) == 0:
potential_words = kana_words
if len(potential_words) != 1 and kana == "": # esp. multiple hits for word written in kana possible, but also for kanji words with different readings
return None
word = Word(potential_words[0])
return word
def get_pos(word) -> List[str]:
pos = []
if word is not None and "senses" in word:
for sense in word["senses"]:
for keys in sense["pos"]:
if isinstance(keys, str):
pos.append(keys)
else:
for key in keys.keys():
pos.append(key)
# if key == "Verb":
# if isinstance(keys[key], str):
# pos.append(keys[key])
# else:
# pos.append(keys[key].keys()[0]) # ?????
pos = list(dict.fromkeys(pos))
return pos
def get_katakana(word):
return word["reading"]["kana"]
def gloss_count(word):
senses = word["senses"]
count = 0
for sense in senses:
count += len(sense["glosses"])
return count
def get_glosses(word):
senses = word["senses"]
glosses = []
for sense in senses:
for gloss in sense["glosses"]:
glosses.append(gloss)
return glosses
def get_pitch(word) -> str:
if not "pitch" in word:
return ""
pitch_str = ""
pitch = word["pitch"]
for i in pitch:
part = i["part"]
high = i["high"]
if high:
pitch_str += "↑"
else:
pitch_str += "↓"
pitch_str += part
if pitch_str == "":
return ""
return pitch_str
def get_pitch_html(word) -> str:
log(word)
if word is None or "pitch" not in word:
return ""
pitch_str = ""
pitch = word["pitch"]
p_count = len(pitch)
for i, p in enumerate(pitch):
part = p["part"]
high = p["high"]
classes = ""
if high:
classes += "t"
else:
classes += "b"
if i != p_count - 1:
classes += " r"
pitch_str += f'<span class="pitch {classes}">{part}</span>'
if pitch_str == "":
return ""
return pitch_str