Fix JSON encoding
This commit is contained in:
parent
cbbd60f4c3
commit
aa29d73477
|
@ -140,10 +140,12 @@ class KanjiResult {
|
||||||
|
|
||||||
Map<String, dynamic> toJson() {
|
Map<String, dynamic> toJson() {
|
||||||
|
|
||||||
if (found == false) return {
|
if (found == false) {
|
||||||
'query': query,
|
return {
|
||||||
'found': found
|
'query': query,
|
||||||
};
|
'found': found
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'query': query,
|
'query': query,
|
||||||
|
@ -175,6 +177,13 @@ class ExampleSentencePiece {
|
||||||
this.unlifted = unlifted;
|
this.unlifted = unlifted;
|
||||||
this.lifted = lifted;
|
this.lifted = lifted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
return {
|
||||||
|
'unlifted': unlifted,
|
||||||
|
'lifted': lifted
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ExampleResultData {
|
class ExampleResultData {
|
||||||
|
@ -183,12 +192,21 @@ class ExampleResultData {
|
||||||
String english;
|
String english;
|
||||||
List<ExampleSentencePiece> pieces;
|
List<ExampleSentencePiece> pieces;
|
||||||
|
|
||||||
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.kanji = kanji;
|
||||||
this.kana = kana;
|
this.kana = kana;
|
||||||
this.english = english;
|
|
||||||
this.pieces = pieces;
|
this.pieces = pieces;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
return {
|
||||||
|
'english': english,
|
||||||
|
'kanji': kanji,
|
||||||
|
'kana': kana,
|
||||||
|
'pieces': pieces
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ExampleResults {
|
class ExampleResults {
|
||||||
|
@ -198,13 +216,23 @@ class ExampleResults {
|
||||||
List<ExampleResultData> results;
|
List<ExampleResultData> results;
|
||||||
String phrase;
|
String phrase;
|
||||||
|
|
||||||
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.query = query;
|
||||||
this.found = found;
|
this.found = found;
|
||||||
this.uri = uri;
|
|
||||||
this.results = results;
|
this.results = results;
|
||||||
|
this.uri = uri;
|
||||||
this.phrase = phrase;
|
this.phrase = phrase;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
return {
|
||||||
|
'query': query,
|
||||||
|
'found': found,
|
||||||
|
'results': results,
|
||||||
|
'uri': uri,
|
||||||
|
'phrase': phrase
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ import 'package:http/http.dart' as http;
|
||||||
import 'package:html_unescape/html_unescape.dart' as html_entities;
|
import 'package:html_unescape/html_unescape.dart' as html_entities;
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'package:html/parser.dart';
|
import 'package:html/parser.dart';
|
||||||
|
import 'package:html/dom.dart';
|
||||||
|
|
||||||
final htmlUnescape = html_entities.HtmlUnescape();
|
final htmlUnescape = html_entities.HtmlUnescape();
|
||||||
|
|
||||||
|
@ -236,7 +237,7 @@ String uriForExampleSearch(String phrase) {
|
||||||
return '${SCRAPE_BASE_URI}${Uri.encodeComponent(phrase)}%23sentences';
|
return '${SCRAPE_BASE_URI}${Uri.encodeComponent(phrase)}%23sentences';
|
||||||
}
|
}
|
||||||
|
|
||||||
ExampleResultData getKanjiAndKana(div) {
|
ExampleResultData getKanjiAndKana(Element div) {
|
||||||
final ul = div.querySelector('ul');
|
final ul = div.querySelector('ul');
|
||||||
final contents = ul.children;
|
final contents = ul.children;
|
||||||
|
|
||||||
|
@ -245,10 +246,10 @@ ExampleResultData getKanjiAndKana(div) {
|
||||||
var kana = '';
|
var kana = '';
|
||||||
for (var i = 0; i < contents.length; i += 1) {
|
for (var i = 0; i < contents.length; i += 1) {
|
||||||
final content = contents[i];
|
final content = contents[i];
|
||||||
if (content.tagName == 'li') {
|
if (content.localName == 'li') {
|
||||||
final li = content;
|
final li = content;
|
||||||
final furigana = li.querySelector('.furigana').text;
|
final furigana = li.querySelector('.furigana')?.text;
|
||||||
final unlifted = li.querySelector('.unlinked').text;
|
final unlifted = li.querySelector('.unlinked')?.text;
|
||||||
|
|
||||||
if (furigana != null) {
|
if (furigana != null) {
|
||||||
kanji += unlifted;
|
kanji += unlifted;
|
||||||
|
@ -284,21 +285,21 @@ ExampleResultData getKanjiAndKana(div) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<ExampleSentencePiece> getPieces(sentenceElement) {
|
List<ExampleSentencePiece> getPieces(Element sentenceElement) {
|
||||||
final pieceElements = sentenceElement.querySelectorAll('li.clearfix');
|
final pieceElements = sentenceElement.querySelectorAll('li.clearfix');
|
||||||
final pieces = [];
|
final List<ExampleSentencePiece> pieces = [];
|
||||||
for (var pieceIndex = 0; pieceIndex < pieceElements.length; pieceIndex += 1) {
|
for (var pieceIndex = 0; pieceIndex < pieceElements.length; pieceIndex += 1) {
|
||||||
final pieceElement = pieceElements[pieceIndex];
|
final pieceElement = pieceElements[pieceIndex];
|
||||||
pieces.add(ExampleSentencePiece(
|
pieces.add(ExampleSentencePiece(
|
||||||
lifted: pieceElement.querySelector('.furigana').text,
|
lifted: pieceElement.querySelector('.furigana')?.text,
|
||||||
unlifted: pieceElement.querySelector('.unlinked').text,
|
unlifted: pieceElement.querySelector('.unlinked')?.text,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
return pieces;
|
return pieces;
|
||||||
}
|
}
|
||||||
|
|
||||||
ExampleResultData parseExampleDiv(div) {
|
ExampleResultData parseExampleDiv(Element div) {
|
||||||
final result = getKanjiAndKana(div);
|
final result = getKanjiAndKana(div);
|
||||||
result.english = div.querySelector('.english').text;
|
result.english = div.querySelector('.english').text;
|
||||||
result.pieces = getPieces(div);
|
result.pieces = getPieces(div);
|
||||||
|
@ -310,7 +311,7 @@ ExampleResults parseExamplePageData(String pageHtml, String phrase) {
|
||||||
final document = parse(pageHtml);
|
final document = parse(pageHtml);
|
||||||
final divs = document.querySelectorAll('.sentence_content');
|
final divs = document.querySelectorAll('.sentence_content');
|
||||||
|
|
||||||
final results = divs.map((div) => parseExampleDiv(div));
|
final results = divs.map((div) => parseExampleDiv(div)).toList();
|
||||||
|
|
||||||
return ExampleResults(
|
return ExampleResults(
|
||||||
query: phrase,
|
query: phrase,
|
||||||
|
@ -325,7 +326,7 @@ ExampleResults parseExamplePageData(String pageHtml, String phrase) {
|
||||||
|
|
||||||
/* PHRASE SCRAPE FUNCTIONS START */
|
/* PHRASE SCRAPE FUNCTIONS START */
|
||||||
|
|
||||||
List<String> getTags(document) {
|
List<String> getTags(Document document) {
|
||||||
final tags = [];
|
final tags = [];
|
||||||
final tagElements = document.querySelectorAll('.concept_light-tag');
|
final tagElements = document.querySelectorAll('.concept_light-tag');
|
||||||
|
|
||||||
|
@ -337,7 +338,7 @@ List<String> getTags(document) {
|
||||||
return tags;
|
return tags;
|
||||||
}
|
}
|
||||||
|
|
||||||
PhrasePageScrapeResult getMeaningsOtherFormsAndNotes(document) {
|
PhrasePageScrapeResult getMeaningsOtherFormsAndNotes(Document document) {
|
||||||
final returnValues = PhrasePageScrapeResult( otherForms: [], notes: [] );
|
final returnValues = PhrasePageScrapeResult( otherForms: [], notes: [] );
|
||||||
|
|
||||||
// const meaningsWrapper = $('#page_container > div > div > article > div > div.concept_light-meanings.medium-9.columns > div');
|
// const meaningsWrapper = $('#page_container > div > div > article > div > div.concept_light-meanings.medium-9.columns > div');
|
||||||
|
|
|
@ -20,7 +20,7 @@ void runTestCases(List<String> testCaseFiles, Function apiFunction) async {
|
||||||
final testCase = jsonDecode(file);
|
final testCase = jsonDecode(file);
|
||||||
await test('Test ${testCount}', () async {
|
await test('Test ${testCount}', () async {
|
||||||
final result = await apiFunction(testCase['query']);
|
final result = await apiFunction(testCase['query']);
|
||||||
expect(result, testCase['expectedResult']);
|
expect(jsonEncode(result), jsonEncode(testCase['expectedResult']));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue