update jisho.py according to suggestions

pull/1030/head
Austin Huang 2 years ago
parent 934ae4e086
commit a399248f56
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F

@ -14,9 +14,11 @@ about = {
"use_official_api": True, "use_official_api": True,
"require_api_key": False, "require_api_key": False,
"results": 'JSON', "results": 'JSON',
"language": 'ja',
} }
categories = ['dictionaries'] categories = ['dictionaries']
engine_type = 'online_dictionary'
paging = False paging = False
URL = 'https://jisho.org' URL = 'https://jisho.org'
@ -35,91 +37,92 @@ def response(resp):
results = [] results = []
infoboxed = False infoboxed = False
search_results = json.loads(resp.text) search_results = resp.json()
pages = search_results.get('data', []) pages = search_results.get('data', [])
for page in pages: for page in pages:
# Entries that are purely from Wikipedia are excluded. # Entries that are purely from Wikipedia are excluded.
if page['senses'][0]['parts_of_speech'][0] != 'Wikipedia definition': if page['senses'][0]['parts_of_speech'] != [] and page['senses'][0]['parts_of_speech'][0] == 'Wikipedia definition':
# Process alternative forms pass
japanese = page['japanese'] # Process alternative forms
alt_forms = [] japanese = page['japanese']
for title_raw in japanese: alt_forms = []
if 'word' not in title_raw: for title_raw in japanese:
alt_forms.append(title_raw['reading']) if 'word' not in title_raw:
else: alt_forms.append(title_raw['reading'])
title = title_raw['word'] else:
if 'reading' in title_raw: title = title_raw['word']
title += ' (' + title_raw['reading'] + ')' if 'reading' in title_raw:
alt_forms.append(title) title += ' (' + title_raw['reading'] + ')'
# Process definitions alt_forms.append(title)
definitions = [] # Process definitions
def_raw = page['senses'] definitions = []
for defn_raw in def_raw: def_raw = page['senses']
extra = '' for defn_raw in def_raw:
if not infoboxed: extra = ''
# Extra data. Since they're not documented, this implementation is based solely by the author's assumptions. if not infoboxed:
if defn_raw['tags'] != []: # Extra data. Since they're not documented, this implementation is based solely by the author's assumptions.
if defn_raw['info'] != []: if defn_raw['tags'] != []:
extra += defn_raw['tags'][0] + ', ' + defn_raw['info'][0] + '. ' # "usually written as kana: <kana>" if defn_raw['info'] != []:
else: extra += defn_raw['tags'][0] + ', ' + defn_raw['info'][0] + '. ' # "usually written as kana: <kana>"
extra += ', '.join(defn_raw['tags']) + '. ' # abbreviation, archaism, etc. else:
elif defn_raw['info'] != []: extra += ', '.join(defn_raw['tags']) + '. ' # abbreviation, archaism, etc.
extra += ', '.join(defn_raw['info']).capitalize() + '. ' # inconsistent elif defn_raw['info'] != []:
if defn_raw['restrictions'] != []: extra += ', '.join(defn_raw['info']).capitalize() + '. ' # inconsistent
extra += 'Only applies to: ' + ', '.join(defn_raw['restrictions']) + '. ' if defn_raw['restrictions'] != []:
extra = extra[:-1] extra += 'Only applies to: ' + ', '.join(defn_raw['restrictions']) + '. '
definitions.append(( extra = extra[:-1]
', '.join(defn_raw['parts_of_speech']), definitions.append((
'; '.join(defn_raw['english_definitions']), ', '.join(defn_raw['parts_of_speech']),
extra '; '.join(defn_raw['english_definitions']),
)) extra
content = '' ))
infobox_content = ''' content = ''
<small><a href="https://www.edrdg.org/wiki/index.php/JMdict-EDICT_Dictionary_Project">JMdict</a> infobox_content = '''
and <a href="https://www.edrdg.org/enamdict/enamdict_doc.html">JMnedict</a> <small><a href="https://www.edrdg.org/wiki/index.php/JMdict-EDICT_Dictionary_Project">JMdict</a>
by <a href="https://www.edrdg.org/edrdg/licence.html">EDRDG</a>, CC BY-SA 3.0.</small><ul> and <a href="https://www.edrdg.org/enamdict/enamdict_doc.html">JMnedict</a>
''' by <a href="https://www.edrdg.org/edrdg/licence.html">EDRDG</a>, CC BY-SA 3.0.</small><ul>
for pos, engdef, extra in definitions: '''
if pos == 'Wikipedia definition': for pos, engdef, extra in definitions:
infobox_content += '</ul><small>Wikipedia, CC BY-SA 3.0.</small><ul>' if pos == 'Wikipedia definition':
if pos == '': infobox_content += '</ul><small>Wikipedia, CC BY-SA 3.0.</small><ul>'
infobox_content += f"<li>{engdef}" if pos == '':
else: infobox_content += f"<li>{engdef}"
infobox_content += f"<li><i>{pos}</i>: {engdef}" else:
if extra != '': infobox_content += f"<li><i>{pos}</i>: {engdef}"
infobox_content += f" ({extra})" if extra != '':
infobox_content += '</li>' infobox_content += f" ({extra})"
content += f"{engdef}. " infobox_content += '</li>'
infobox_content += '</ul>' content += f"{engdef}. "
infobox_content += '</ul>'
# For results, we'll return the URL, all alternative forms (as title), # For results, we'll return the URL, all alternative forms (as title),
# and all definitions (as description) truncated to 300 characters. # and all definitions (as description) truncated to 300 characters.
results.append({ results.append({
'url': urljoin(BASE_URL, page['slug']), 'url': urljoin(BASE_URL, page['slug']),
'title': ", ".join(alt_forms), 'title': ", ".join(alt_forms),
'content': content[:300] + (content[300:] and '...') 'content': content[:300] + (content[300:] and '...')
}) })
# Like Wordnik, we'll return the first result in an infobox too. # Like Wordnik, we'll return the first result in an infobox too.
if not infoboxed: if not infoboxed:
infoboxed = True infoboxed = True
infobox_urls = [] infobox_urls = []
infobox_urls.append({ infobox_urls.append({
'title': 'Jisho.org', 'title': 'Jisho.org',
'url': urljoin(BASE_URL, page['slug']) 'url': urljoin(BASE_URL, page['slug'])
}) })
infobox = { infobox = {
'infobox': alt_forms[0], 'infobox': alt_forms[0],
'urls': infobox_urls 'urls': infobox_urls
} }
alt_forms.pop(0) alt_forms.pop(0)
alt_content = '' alt_content = ''
if len(alt_forms) > 0: if len(alt_forms) > 0:
alt_content = '<p><i>Other forms:</i> ' alt_content = '<p><i>Other forms:</i> '
alt_content += ", ".join(alt_forms) alt_content += ", ".join(alt_forms)
alt_content += '</p>' alt_content += '</p>'
infobox['content'] = alt_content + infobox_content infobox['content'] = alt_content + infobox_content
results.append(infobox) results.append(infobox)
return results return results

Loading…
Cancel
Save