GithubHelp home page GithubHelp logo

mistval / unofficial-jisho-api Goto Github PK

View Code? Open in Web Editor NEW
136.0 6.0 14.0 2.18 MB

Encapsulates the official Jisho.org API and also provides kanji, example, and stroke diagram search.

License: MIT License

JavaScript 100.00%
jisho dictionary japanese japanese-language api

unofficial-jisho-api's Introduction

alt text

unofficial-jisho-api

npm version Build Status codecov install size

This module encapsulates the official Jisho.org API and also provides kanji and example search features that scrape Jisho.org. This module is intended for server-side use cases. It might be possible to use this module in a browser application, but you are likely to encounter CORS errors, and would need to use a proxy server to circumvent them.

Installation

In a Node.js project:

npm install unofficial-jisho-api

Basic usage

Below are some basic examples. There's more detailed documentation here.

Word/phrase search (provided by official Jisho API)

This returns the same results as the official Jisho.org API. See the discussion of that here.

import JishoAPI from 'unofficial-jisho-api';

const jisho = new JishoAPI();

jisho.searchForPhrase('日').then(result => {
  console.log(result);
});

Kanji search

import JishoAPI from 'unofficial-jisho-api';

const jisho = new JishoAPI();

jisho.searchForKanji('語').then(result => {
  console.log('Found: ' + result.found);
  console.log('Taught in: ' + result.taughtIn);
  console.log('JLPT level: ' + result.jlptLevel);
  console.log('Newspaper frequency rank: ' + result.newspaperFrequencyRank);
  console.log('Stroke count: ' + result.strokeCount);
  console.log('Meaning: ' + result.meaning);
  console.log('Kunyomi: ' + JSON.stringify(result.kunyomi));
  console.log('Kunyomi example: ' + JSON.stringify(result.kunyomiExamples[0]));
  console.log('Onyomi: ' + JSON.stringify(result.onyomi));
  console.log('Onyomi example: ' + JSON.stringify(result.onyomiExamples[0]));
  console.log('Radical: ' + JSON.stringify(result.radical));
  console.log('Parts: ' + JSON.stringify(result.parts));
  console.log('Stroke order diagram: ' + result.strokeOrderDiagramUri);
  console.log('Stroke order SVG: ' + result.strokeOrderSvgUri);
  console.log('Stroke order GIF: ' + result.strokeOrderGifUri);
  console.log('Jisho Uri: ' + result.uri);
});

This outputs the following:

Found: true
Taught in: grade 2
JLPT level: N5
Newspaper frequency rank: 301
Stroke count: 14
Meaning: word, speech, language
Kunyomi: ["かた.る","かた.らう"]
Kunyomi example: {"example":"語る","reading":"かたる","meaning":"to talk about, to speak of, to tell, to narrate, to recite, to chant, to indicate, to show"}
Onyomi: ["ゴ"]
Onyomi example: {"example":"語","reading":"ゴ","meaning":"language, word"}
Radical: {"symbol":"言","forms":["訁"],"meaning":"speech"}
Parts: ["口","五","言"]
Stroke order diagram: https://classic.jisho.org/static/images/stroke_diagrams/35486_frames.png
Stroke order SVG: https://d1w6u4xc3l95km.cloudfront.net/kanji-2015-03/08a9e.svg
Stroke order GIF: https://raw.githubusercontent.com/mistval/kotoba/master/resources/images/kanjianimations/08a9e_anim.gif
Jisho Uri: https://jisho.org/search/%E8%AA%9E%23kanji

Example search

import JishoAPI from 'unofficial-jisho-api';

const jisho = new JishoAPI();

jisho.searchForExamples('日').then(result => {
  console.log('Jisho Uri: ' + result.uri);
  console.log();

  for (let i = 0; i < 3; ++i) {
    let example = result.results[i];
    console.log(example.kanji);
    console.log(example.kana);
    console.log(example.english);
    console.log(JSON.stringify(example.pieces));
    console.log();
  }
});

This outputs the following:

Jisho Uri: https://jisho.org/search/%E6%97%A5%23sentences

日本人ならそんなことはけっしてしないでしょう。
にほんじんならそんなことはけっしてしないでしょう。
A Japanese person would never do such a thing.
[{"lifted":"にほんじん","unlifted":"日本人"},{"lifted":"","unlifted":"なら"},{"lifted":"","unlifted":"そんな"},{"lifted":"","unlifted":"こと"},{"lifted":"","unlifted":"は"},{"lifted":"","unlifted":"けっして"},{"lifted":"","unlifted":"しない"},{"lifted":"","
unlifted":"でしょう"}]

今日はとても暑い。
きょうはとてもあつい。
It is very hot today.
[{"lifted":"きょう","unlifted":"今日"},{"lifted":"","unlifted":"は"},{"lifted":"","unlifted":"とても"},{"lifted":"あつ","unlifted":"暑い"}]

日本には美しい都市が多い。例えば京都、奈良だ。
にほんにはうつくしいとしがおおい。たとえばきょうと、奈良だ。
Japan is full of beautiful cities. Kyoto and Nara, for instance.
[{"lifted":"にほん","unlifted":"日本"},{"lifted":"","unlifted":"には"},{"lifted":"うつく","unlifted":"美しい"},{"lifted":"とし","unlifted":"都市"},{"lifted":"","unlifted":"が"},{"lifted":"おお","unlifted":"多い"},{"lifted":"たと","unlifted":"例えば"},{"lift
ed":"きょうと","unlifted":"京都"},{"lifted":"","unlifted":"だ"}]

Word/phrase scraping

This scrapes the word/phrase page on Jisho.org. This can get you some data that the official API doesn't have, such as JLPT level and part-of-speech. The official API (searchForPhrase) should be preferred if it has the data you need.

import JishoAPI from 'unofficial-jisho-api';

const jisho = new JishoAPI();

jisho.scrapeForPhrase('谷').then((data) => {
  console.log(JSON.stringify(data, null, 2));
});

This outputs the following:

{
  "found": true,
  "query": "",
  "uri": "https://jisho.org/word/%E8%B0%B7",
  "tags": [
    "Common word",
    "JLPT N3",
    "Wanikani level 5"
  ],
  "meanings": [
    {
      "seeAlsoTerms": [],
      "sentences": [],
      "definition": "valley",
      "supplemental": [],
      "definitionAbstract": "",
      "tags": [
        "noun"
      ]
    },
    {
      "seeAlsoTerms": [],
      "sentences": [],
      "definition": "Valley",
      "supplemental": [],
      "definitionAbstract": "In geology, a valley or dale is a depression with predominant extent in one direction. A very deep river valley may be called a canyon or gorge. The terms U-shaped and V-shaped are descriptive terms of geography to characterize the form of valleys. Most valleys belong to one of these two main types or a mixture of them, (at least) with respect of the cross section of the slopes or hillsides.",
      "tags": [
        "wikipedia definition"
      ]
    }
  ],
  "otherForms": [
    {
      "kanji": "",
      "kana": "たに"
    },
    {
      "kanji": "谿",
      "kana": "たに"
    }
  ],
  "audio": [
    {
      "uri": "https://d1vjc5dkcd3yh2.cloudfront.net/audio/b9ff4f25c7a20f0f39131b3e3db0cd19.mp3",
      "mimetype": "audio/mpeg"
    },
    {
      "uri": "https://d1vjc5dkcd3yh2.cloudfront.net/audio_ogg/b9ff4f25c7a20f0f39131b3e3db0cd19.ogg",
      "mimetype": "audio/ogg"
    }
  ],
  "notes": []
}

CommonJS

You can require this module as a CommonJS module. Just const JishoAPI = require('unofficial-jisho-api');.

Parsing HTML strings

You can provide the HTML responses from Jisho yourself. This can be useful if you need to use a CORS proxy or something. You can do whatever you need to do to get the HTML and then provide it to this module's parsing functions. For example:

Parse kanji page HTML

import fetch from 'node-fetch';
import JishoAPI from 'unofficial-jisho-api';

const jisho = new JishoAPI();

const SEARCH_KANJI = '車';
const SEARCH_URI = jisho.getUriForKanjiSearch(SEARCH_KANJI);

fetch(SEARCH_URI)
  .then((res) => res.text())
  .then((text) => {
    const json = jisho.parseKanjiPageHtml(text, SEARCH_KANJI);
    console.log(`JLPT level: ${json.jlptLevel}`);
    console.log(`Stroke count: ${json.strokeCount}`);
    console.log(`Meaning: ${json.meaning}`);
  });

Parse example page HTML

import fetch from 'node-fetch';
import JishoAPI from 'unofficial-jisho-api';

const jisho = new JishoAPI();

const SEARCH_EXAMPLE = '保護者';
const SEARCH_URI = jisho.getUriForExampleSearch(SEARCH_EXAMPLE);

fetch(SEARCH_URI)
  .then((res) => res.text())
  .then((text) => {
    const json = jisho.parseExamplePageHtml(text, SEARCH_EXAMPLE);
    console.log(`English: ${json.results[0].english}`);
    console.log(`Kanji ${json.results[0].kanji}`);
    console.log(`Kana: ${json.results[0].kana}`);
  });

Parse phrase page HTML

import fetch from 'node-fetch';
import JishoAPI from 'unofficial-jisho-api';

const jisho = new JishoAPI();

const SEARCH_EXAMPLE = '保護者';
const SEARCH_URI = jisho.getUriForPhraseScrape(SEARCH_EXAMPLE);

fetch(SEARCH_URI)
  .then((res) => res.text())
  .then((text) => {
    const json = jisho.parsePhraseScrapeHtml(text, SEARCH_EXAMPLE);
    console.log(JSON.stringify(json, null, 2));
  });

About

Permission to scrape granted by Jisho's admin Kimtaro: https://jisho.org/forum/54fefc1f6e73340b1f160000-is-there-any-kind-of-search-api

Warning: Understand that other than the searchForPhrase function, all of the functions in this package scrape the HTML of Jisho.org. As such, these functions may break without warning when updates are made to Jisho.org. These functions should not be used for anything mission critical (like your manned Japanese spaceflight program). That being said, they haven't broken yet in ~3 years, but the legends say Jisho.org is getting a big update at some point.

For bugs or requested additional data, feel free to open an issue on the Github repo.

unofficial-jisho-api's People

Contributors

damien-mcmahon avatar dependabot[bot] avatar gyn7561 avatar h7x4 avatar mistval avatar nonemoticoner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

unofficial-jisho-api's Issues

Stroke order animation

Firstly, thank you for the awesome api. I'm using it in my personal project.
Secondly, I was wondering if it would be possible to get the stroke animation or should I just directly use the kanjiVG for that?

Enable Option for CORS

It would be a good idea to create a option to add a url of a CORS reverse proxy server infront of the jisho url. That would enable this api to also work on browser-side applications.

Example sentence for word scrape senses occasionally misses pieces

Jisho seems to sometimes not wrap details like symbols (parens, punctuation, etc.), katakana names and sometimes even words in a <li class="clearfix"> tag. This means that the parser will skip over them.

An example search is 赤い which will yield the sentence

ヘレンはみんなにほめられて顔を赤くした

Formatted as

<ul class="japanese japanese_gothic clearfix" lang="ja">
    ヘレン
    <li class="clearfix"><span class="unlinked"></span></li>
    <li class="clearfix"><span class="unlinked">みんな</span></li>
    <li class="clearfix"><span class="unlinked"></span></li>
    <li class="clearfix"><span class="unlinked">ほめられて</span></li>
    <li class="clearfix"><span class="furigana">かお</span><span class="unlinked"></span></li>
    <li class="clearfix"><span class="unlinked"></span></li>
    <li class="clearfix"><span class="furigana">あか</span><span class="unlinked">赤く</span></li>
    <li class="clearfix"><span class="unlinked">した</span></li><li class="english" lang="en">Helen blushed at their praise.</li>
</ul>

which makes the parser miss ヘレン and

Some other searches are かな, たら (Particle 2, this one even has kanji missing furigana outside clearfix) and .

Make searchForPhrase independent of Jisho API

I suggest to drop being dependent upon Jisho API for word search. Two reasons:

  1. Official Jisho API does not provide all the data in its word/phrase endpoint e.g. information about JLPT level is missing.
  2. searchForPhrase method is just routing data from Jisho API and anyone could make a simple fetch call. So its usefulness in current version is a bit questionable.

Btw. very useful repo ;)

Word Type Search

On jisho.org, it is possible to see the 'type' of word, for example, suru verb, godan verb etc. In the future would it be possible to get that information via the unofficial jisho api? I also now Jisho has a lot of information about inflection, being able to get that information would be super cool too.

Attempting to import this project into my React Native Project

I do not know what details to provide, but I am having an extremely hard time importing the project in my App. I went through the normally NPM install unofficial-jisho-api. Attempted to import the project by typing in import jisho from 'unofficial-jisho-api';

My project fails each and every time. I deleted the node modules and then did an npm install and still did not resolve my issue. Any help will be greatly appreciated.

Associate furigana with phrases in searchForExamples

I was looking at this feature and immediately spotted what is lacking or what would be great to have.

What we currently get is this:

今日はとても暑い
きょうはとてもあつい
It is very hot today.

From above you can't tell (at least not immediately and in more complex sentances you for sure gonna run in troubles) which kana is associated with which phrase written in kanjis. But on Jisho this is visible:
https://jisho.org/sentences/5186634ed5dda7e981000029

I assume this would likely require implementation of new method i.e. scrapForExamples but I didn't investigate much so might be wrong.

sameSite attribute issue

I'm getting this "issue" on the console,
Indicate whether to send a cookie in a cross-site request by specifying its SameSite attribute
while working with this API.
and because of this, I cant make multiple requests with this API. I tried resolving it on client side but didn't found any promising solution.

CORS

Hi, first of all thanks for making this great tool! This is exactly what I was hoping to find.

I'm getting the cross origin error when I try to use this though (and I saw it was discussed in the thread on jisho). Is there anyway to send the requests through a proxy?

reading of jukugo

I'd like get the reading of a jukugo.
The following provides meaning but not the reading.
How can one achieve this?

jisho.scrapeForPhrase('電車').then((data) => {
  console.log(JSON.stringify(data, null, 2));
});

Errors in parsing furigana

I'm going to maintain a list of queries here for which the parser does not do the right thing.

seldom (first example sentence)
(first example sentence, 々 appears in kana)

Undefined <result.strokeOrderSvgUri>

First, thank you for making this API project.
Second, when I use your API to print result.strokeOrderSvgUri what I get is Undefined and the trouble is in the animationUri link in the index.js file.
I hope you can update it with a new link, thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.