.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
jisho.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""
3Jisho (the Japanese-English dictionary)
4"""
5
6from urllib.parse import urlencode, urljoin
7
8# about
9about = {
10 "website": 'https://jisho.org',
11 "wikidata_id": 'Q24568389',
12 "official_api_documentation": "https://jisho.org/forum/54fefc1f6e73340b1f160000-is-there-any-kind-of-search-api",
13 "use_official_api": True,
14 "require_api_key": False,
15 "results": 'JSON',
16 "language": 'ja',
17}
18
19categories = ['dictionaries']
20paging = False
21
22URL = 'https://jisho.org'
23BASE_URL = 'https://jisho.org/word/'
24SEARCH_URL = URL + '/api/v1/search/words?{query}'
25
26
27def request(query, params):
28 query = urlencode({'keyword': query})
29 params['url'] = SEARCH_URL.format(query=query)
30 logger.debug(f"query_url --> {params['url']}")
31 return params
32
33
34def response(resp):
35 results = []
36 first_result = True
37
38 search_results = resp.json()
39
40 for page in search_results.get('data', []):
41 # Entries that are purely from Wikipedia are excluded.
42 parts_of_speech = page.get('senses') and page['senses'][0].get('parts_of_speech')
43 if parts_of_speech and parts_of_speech[0] == 'Wikipedia definition':
44 pass
45
46 # Process alternative forms
47 alt_forms = []
48 for title_raw in page['japanese']:
49 if 'word' not in title_raw:
50 alt_forms.append(title_raw['reading'])
51 else:
52 title = title_raw['word']
53 if 'reading' in title_raw:
54 title += ' (' + title_raw['reading'] + ')'
55 alt_forms.append(title)
56
57 result_url = urljoin(BASE_URL, page['slug'])
58 definitions = get_definitions(page)
59
60 # For results, we'll return the URL, all alternative forms (as title),
61 # and all definitions (as description) truncated to 300 characters.
62 content = " ".join(f"{engdef}." for _, engdef, _ in definitions)
63 results.append(
64 {'url': result_url, 'title': ", ".join(alt_forms), 'content': content[:300] + (content[300:] and '...')}
65 )
66
67 # Like Wordnik, we'll return the first result in an infobox too.
68 if first_result:
69 first_result = False
70 results.append(get_infobox(alt_forms, result_url, definitions))
71
72 return results
73
74
76 # Process definitions
77 definitions = []
78 for defn_raw in page['senses']:
79 extra = []
80 # Extra data. Since they're not documented, this implementation is based solely by the author's assumptions.
81 if defn_raw.get('tags'):
82 if defn_raw.get('info'):
83 # "usually written as kana: <kana>"
84 extra.append(defn_raw['tags'][0] + ', ' + defn_raw['info'][0] + '. ')
85 else:
86 # abbreviation, archaism, etc.
87 extra.append(', '.join(defn_raw['tags']) + '. ')
88 elif defn_raw.get('info'):
89 # inconsistent
90 extra.append(', '.join(defn_raw['info']).capitalize() + '. ')
91 if defn_raw.get('restrictions'):
92 extra.append('Only applies to: ' + ', '.join(defn_raw['restrictions']) + '. ')
93 definitions.append(
94 (
95 ', '.join(defn_raw['parts_of_speech']),
96 '; '.join(defn_raw['english_definitions']),
97 ''.join(extra)[:-1],
98 )
99 )
100 return definitions
101
102
103def get_infobox(alt_forms, result_url, definitions):
104 infobox_content = []
105 # title & alt_forms
106 infobox_title = alt_forms[0]
107 if len(alt_forms) > 1:
108 infobox_content.append(f'<p><i>Other forms:</i> {", ".join(alt_forms[1:])}</p>')
109
110 # definitions
111 infobox_content.append(
112 '''
113 <small><a href="https://www.edrdg.org/wiki/index.php/JMdict-EDICT_Dictionary_Project">JMdict</a>
114 and <a href="https://www.edrdg.org/enamdict/enamdict_doc.html">JMnedict</a>
115 by <a href="https://www.edrdg.org/edrdg/licence.html">EDRDG</a>, CC BY-SA 3.0.</small>
116 <ul>
117 '''
118 )
119 for pos, engdef, extra in definitions:
120 if pos == 'Wikipedia definition':
121 infobox_content.append('</ul><small>Wikipedia, CC BY-SA 3.0.</small><ul>')
122 pos = f'<i>{pos}</i>: ' if pos else ''
123 extra = f' ({extra})' if extra else ''
124 infobox_content.append(f'<li>{pos}{engdef}{extra}</li>')
125 infobox_content.append('</ul>')
126
127 #
128 return {
129 'infobox': infobox_title,
130 'content': ''.join(infobox_content),
131 'urls': [
132 {
133 'title': 'Jisho.org',
134 'url': result_url,
135 }
136 ],
137 }
get_definitions(page)
Definition jisho.py:75
get_infobox(alt_forms, result_url, definitions)
Definition jisho.py:103
request(query, params)
Definition jisho.py:27