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 {
String english;
String japanese;
@ -108,166 +280,151 @@ class PhrasePageScrapeResult {
};
}
class YomiExample {
String example;
/* -------------------------------------------------------------------------- */
/* searchForPhrase related classes */
/* -------------------------------------------------------------------------- */
class JishoJapaneseWord {
String word;
String reading;
String meaning;
YomiExample({String example, String reading, String meaning})
{
this.example = example;
this.reading = reading;
this.meaning = meaning;
}
JishoJapaneseWord({this.word, this.reading});
Map<String, String> toJson() =>
{
'example': example,
'reading': reading,
'meaning': meaning
};
factory JishoJapaneseWord.fromJson(Map<String, dynamic> json){
return JishoJapaneseWord(
word: json['word'] as String,
reading: json['reading'] as String
);
}
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 {
String lifted;
String unlifted;
class JishoSenseLink {
String text;
String url;
ExampleSentencePiece({String lifted, String unlifted}){
this.lifted = lifted;
this.unlifted = unlifted;
}
JishoSenseLink({this.text, this.url});
Map<String, dynamic> toJson() {
return {
'lifted': lifted,
'unlifted': unlifted
};
factory JishoSenseLink.fromJson(Map<String, dynamic> json){
return JishoSenseLink(
text: json['text'] as String,
url: json['url'] as String
);
}
}
class ExampleResultData {
String kanji;
String kana;
String english;
List<ExampleSentencePiece> pieces;
class JishoWordSense {
List<String> english_definitions;
List<String> parts_of_speech;
List<JishoSenseLink> links;
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}){
this.english = english;
this.kanji = kanji;
this.kana = kana;
this.pieces = pieces;
}
JishoWordSense({
this.english_definitions,
this.parts_of_speech,
this.links,
this.tags,
this.see_also,
this.antonyms,
this.source,
this.info,
this.restrictions
});
Map<String, dynamic> toJson() {
return {
'english': english,
'kanji': kanji,
'kana': kana,
'pieces': pieces
};
factory JishoWordSense.fromJson(Map<String, dynamic> json){
return JishoWordSense(
english_definitions: json['english_definitions'] as List<String>,
parts_of_speech: json['parts_of_speech'] as List<String>,
links: json['links'] as List<JishoSenseLink>,
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 {
String query;
bool found;
String uri;
List<ExampleResultData> results;
String phrase;
class JishoAttribution {
bool jmdict;
bool jmnedict;
bool dbpedia;
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;
}
JishoAttribution({
this.jmdict,
this.jmnedict,
this.dbpedia
});
Map<String, dynamic> toJson() {
return {
'query': query,
'found': found,
'results': results,
'uri': uri,
'phrase': phrase
};
factory JishoAttribution.fromJson(Map<String, dynamic> json){
return JishoAttribution(
jmdict: json['jmdict'] as bool,
jmnedict: json['jmnedict'] as bool,
dbpedia: json['dbpedia'] as bool
);
}
}
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
/// complex and is not documented, so put on your trial-and-error hat.
/// @async
searchForPhrase(String phrase) async {
Future<List<JishoResult>> searchForPhrase(String phrase) async {
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.
@ -518,22 +537,4 @@ class JishoApi {
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));
}
}