Add Jisho API objects

This commit is contained in:
Oystein Kristoffer Tveit 2020-06-20 22:55:49 +02:00
parent 2954f366ea
commit 93e34ec3f9
2 changed files with 316 additions and 158 deletions

View File

@ -1,3 +1,175 @@
/* -------------------------------------------------------------------------- */
/* searchForKanji related classes */
/* -------------------------------------------------------------------------- */
class YomiExample {
String example;
String reading;
String meaning;
YomiExample({String example, String reading, String meaning})
{
this.example = example;
this.reading = reading;
this.meaning = meaning;
}
Map<String, String> toJson() =>
{
'example': example,
'reading': reading,
'meaning': meaning
};
}
class Radical {
String symbol;
List<String> forms;
String meaning;
Radical({String symbol, List<String> forms, String meaning}){
this.symbol = symbol;
this.forms = forms;
this.meaning = meaning;
}
Map<String, dynamic> toJson() =>
{
'symbol': symbol,
'forms': forms,
'meaning': meaning
};
}
class KanjiResult {
String query;
bool found;
String taughtIn;
String jlptLevel;
int newspaperFrequencyRank;
int strokeCount;
String meaning;
List<String> kunyomi;
List<String> onyomi;
List<YomiExample> kunyomiExamples;
List<YomiExample> onyomiExamples;
Radical radical;
List<String> parts;
String strokeOrderDiagramUri;
String strokeOrderSvgUri;
String strokeOrderGifUri;
String uri;
Map<String, dynamic> toJson() {
if (found == false) {
return {
'query': query,
'found': found
};
}
var returnObject = {
'query': query,
'found': found,
'taughtIn': taughtIn,
'jlptLevel': jlptLevel,
'newspaperFrequencyRank': newspaperFrequencyRank,
'strokeCount': strokeCount,
'meaning': meaning,
'kunyomi': kunyomi,
'onyomi': onyomi,
'onyomiExamples': onyomiExamples,
'kunyomiExamples': kunyomiExamples,
'radical': radical.toJson(),
'parts': parts,
'strokeOrderDiagramUri': strokeOrderDiagramUri,
'strokeOrderSvgUri': strokeOrderSvgUri,
'strokeOrderGifUri': strokeOrderGifUri,
'uri': uri
};
return returnObject;
}
}
/* -------------------------------------------------------------------------- */
/* searchForExamples related classes */
/* -------------------------------------------------------------------------- */
class ExampleSentencePiece {
String lifted;
String unlifted;
ExampleSentencePiece({String lifted, String unlifted}){
this.lifted = lifted;
this.unlifted = unlifted;
}
Map<String, dynamic> toJson() {
return {
'lifted': lifted,
'unlifted': unlifted
};
}
}
class ExampleResultData {
String kanji;
String kana;
String english;
List<ExampleSentencePiece> pieces;
ExampleResultData({String english, String kanji, String kana, List<ExampleSentencePiece> pieces}){
this.english = english;
this.kanji = kanji;
this.kana = kana;
this.pieces = pieces;
}
Map<String, dynamic> toJson() {
return {
'english': english,
'kanji': kanji,
'kana': kana,
'pieces': pieces
};
}
}
class ExampleResults {
String query;
bool found;
String uri;
List<ExampleResultData> results;
String phrase;
ExampleResults({String query, bool found, List<ExampleResultData> results, String uri, String phrase}){
this.query = query;
this.found = found;
this.results = results;
this.uri = uri;
this.phrase = phrase;
}
Map<String, dynamic> toJson() {
return {
'query': query,
'found': found,
'results': results,
'uri': uri,
'phrase': phrase
};
}
}
/* -------------------------------------------------------------------------- */
/* scrapeForPhrase related classes */
/* -------------------------------------------------------------------------- */
class PhraseScrapeSentence { class PhraseScrapeSentence {
String english; String english;
String japanese; String japanese;
@ -108,166 +280,151 @@ class PhrasePageScrapeResult {
}; };
} }
class YomiExample { /* -------------------------------------------------------------------------- */
String example; /* searchForPhrase related classes */
/* -------------------------------------------------------------------------- */
class JishoJapaneseWord {
String word;
String reading; String reading;
String meaning;
YomiExample({String example, String reading, String meaning}) JishoJapaneseWord({this.word, this.reading});
{
this.example = example;
this.reading = reading;
this.meaning = meaning;
}
Map<String, String> toJson() => factory JishoJapaneseWord.fromJson(Map<String, dynamic> json){
{ return JishoJapaneseWord(
'example': example, word: json['word'] as String,
'reading': reading, reading: json['reading'] as String
'meaning': meaning );
};
}
class Radical {
String symbol;
List<String> forms;
String meaning;
Radical({String symbol, List<String> forms, String meaning}){
this.symbol = symbol;
this.forms = forms;
this.meaning = meaning;
}
Map<String, dynamic> toJson() =>
{
'symbol': symbol,
'forms': forms,
'meaning': meaning
};
}
class KanjiResult {
String query;
bool found;
String taughtIn;
String jlptLevel;
int newspaperFrequencyRank;
int strokeCount;
String meaning;
List<String> kunyomi;
List<String> onyomi;
List<YomiExample> kunyomiExamples;
List<YomiExample> onyomiExamples;
Radical radical;
List<String> parts;
String strokeOrderDiagramUri;
String strokeOrderSvgUri;
String strokeOrderGifUri;
String uri;
Map<String, dynamic> toJson() {
if (found == false) {
return {
'query': query,
'found': found
};
}
var returnObject = {
'query': query,
'found': found,
'taughtIn': taughtIn,
'jlptLevel': jlptLevel,
'newspaperFrequencyRank': newspaperFrequencyRank,
'strokeCount': strokeCount,
'meaning': meaning,
'kunyomi': kunyomi,
'onyomi': onyomi,
'onyomiExamples': onyomiExamples,
'kunyomiExamples': kunyomiExamples,
'radical': radical.toJson(),
'parts': parts,
'strokeOrderDiagramUri': strokeOrderDiagramUri,
'strokeOrderSvgUri': strokeOrderSvgUri,
'strokeOrderGifUri': strokeOrderGifUri,
'uri': uri
};
return returnObject;
} }
} }
class ExampleSentencePiece { class JishoSenseLink {
String lifted; String text;
String unlifted; String url;
ExampleSentencePiece({String lifted, String unlifted}){ JishoSenseLink({this.text, this.url});
this.lifted = lifted;
this.unlifted = unlifted;
}
Map<String, dynamic> toJson() { factory JishoSenseLink.fromJson(Map<String, dynamic> json){
return { return JishoSenseLink(
'lifted': lifted, text: json['text'] as String,
'unlifted': unlifted url: json['url'] as String
}; );
} }
} }
class ExampleResultData { class JishoWordSense {
String kanji; List<String> english_definitions;
String kana; List<String> parts_of_speech;
String english; List<JishoSenseLink> links;
List<ExampleSentencePiece> pieces; List<String> tags;
List<String> see_also;
List<String> antonyms;
List<dynamic> source;
List<String> info;
List<dynamic> restrictions;
ExampleResultData({String english, String kanji, String kana, List<ExampleSentencePiece> pieces}){ JishoWordSense({
this.english = english; this.english_definitions,
this.kanji = kanji; this.parts_of_speech,
this.kana = kana; this.links,
this.pieces = pieces; this.tags,
} this.see_also,
this.antonyms,
this.source,
this.info,
this.restrictions
});
Map<String, dynamic> toJson() { factory JishoWordSense.fromJson(Map<String, dynamic> json){
return { return JishoWordSense(
'english': english, english_definitions: json['english_definitions'] as List<String>,
'kanji': kanji, parts_of_speech: json['parts_of_speech'] as List<String>,
'kana': kana, links: json['links'] as List<JishoSenseLink>,
'pieces': pieces tags: json['tags'] as List<String>,
}; see_also: json['see_also'] as List<String>,
antonyms: json['antonyms'] as List<String>,
source: json['source'] as List<dynamic>,
info: json['info'] as List<String>,
restrictions: json['restrictions'] as List<dynamic>
);
} }
} }
class ExampleResults { class JishoAttribution {
String query; bool jmdict;
bool found; bool jmnedict;
String uri; bool dbpedia;
List<ExampleResultData> results;
String phrase;
ExampleResults({String query, bool found, List<ExampleResultData> results, String uri, String phrase}){ JishoAttribution({
this.query = query; this.jmdict,
this.found = found; this.jmnedict,
this.results = results; this.dbpedia
this.uri = uri; });
this.phrase = phrase;
}
Map<String, dynamic> toJson() { factory JishoAttribution.fromJson(Map<String, dynamic> json){
return { return JishoAttribution(
'query': query, jmdict: json['jmdict'] as bool,
'found': found, jmnedict: json['jmnedict'] as bool,
'results': results, dbpedia: json['dbpedia'] as bool
'uri': uri, );
'phrase': phrase
};
} }
} }
class JishoResult {
String slug;
bool is_common;
List<String> tags;
List<String> jlpt;
List<JishoJapaneseWord> japanese;
List<JishoWordSense> senses;
JishoAttribution attribution;
JishoResult({
this.slug,
this.is_common,
this.tags,
this.jlpt,
this.japanese,
this.senses,
this.attribution
})
factory JishoResult.fromJson(Map<String, dynamic> json){
return JishoResult(
slug: json['slug'] as String,
is_common: json['is_common'] as bool,
tags: json['tags'] as List<String>,
jlpt: json['jlpt'] as List<String>,
japanese: json['japanese'] as List<JishoJapaneseWord>,
senses: json['senses'] as List<JishoWordSense>,
attribution: json['attribution'] as JishoAttribution
);
}
}
class JishoResultMeta {
int status;
JishoResultMeta({this.status});
factory JishoResultMeta.fromJson(Map<String, dynamic> json){
return JishoResultMeta(
status: json['status'] as int
);
}
}
class JishoAPIResult {
JishoResultMeta meta;
List<JishoResult> data;
JishoAPIResult({this.meta, this.data});
factory JishoAPIResult.fromJson(Map<String, dynamic> json){
return JishoAPIResult(
meta: json['meta'] as JishoResultMeta,
data: json['data'] as List<JishoResult>
);
}
}

View File

@ -487,9 +487,28 @@ class JishoApi {
/// @returns {Object} The response data from the official Jisho.org API. Its format is somewhat /// @returns {Object} The response data from the official Jisho.org API. Its format is somewhat
/// complex and is not documented, so put on your trial-and-error hat. /// complex and is not documented, so put on your trial-and-error hat.
/// @async /// @async
searchForPhrase(String phrase) async { Future<List<JishoResult>> searchForPhrase(String phrase) async {
final uri = uriForPhraseSearch(phrase); final uri = uriForPhraseSearch(phrase);
return http.get(uri).then((response) => jsonDecode(response.body).data); final JishoAPIResult jsonData = await http.get(uri).then((response) => jsonDecode(response.body));
return jsonData.data;
}
/// Scrape Jisho.org for information about a kanji character.
/// @param {string} kanji The kanji to search for.
/// @returns {KanjiResult} Information about the searched kanji.
/// @async
Future<KanjiResult> searchForKanji(String kanji) async {
final uri = uriForKanjiSearch(kanji);
return http.get(uri).then((response) => parseKanjiPageData(response.body, kanji));
}
/// Scrape Jisho.org for examples.
/// @param {string} phrase The word or phrase to search for.
/// @returns {ExampleResults}
/// @async
Future<ExampleResults> searchForExamples(String phrase) async {
final uri = uriForExampleSearch(phrase);
return http.get(uri).then((response) => parseExamplePageData(response.body, phrase));
} }
/// Scrape the word page for a word/phrase. /// Scrape the word page for a word/phrase.
@ -518,22 +537,4 @@ class JishoApi {
throw err; throw err;
} }
} }
/// Scrape Jisho.org for information about a kanji character.
/// @param {string} kanji The kanji to search for.
/// @returns {KanjiResult} Information about the searched kanji.
/// @async
Future<KanjiResult> searchForKanji(String kanji) async {
final uri = uriForKanjiSearch(kanji);
return http.get(uri).then((response) => parseKanjiPageData(response.body, kanji));
}
/// Scrape Jisho.org for examples.
/// @param {string} phrase The word or phrase to search for.
/// @returns {ExampleResults}
/// @async
Future<ExampleResults> searchForExamples(String phrase) async {
final uri = uriForExampleSearch(phrase);
return http.get(uri).then((response) => parseExamplePageData(response.body, phrase));
}
} }