Fix example sentence symbols

This commit is contained in:
Oystein Kristoffer Tveit 2020-06-22 23:07:23 +02:00
parent 86b8ccfdbb
commit 5d7b7696d7
2 changed files with 27 additions and 12 deletions

View File

@ -10,19 +10,37 @@ String uriForExampleSearch(String phrase) {
return '${SCRAPE_BASE_URI}${Uri.encodeComponent(phrase)}%23sentences'; return '${SCRAPE_BASE_URI}${Uri.encodeComponent(phrase)}%23sentences';
} }
/* TODO: This is the wrong approach. List<Element> getChildrenAndSymbols(Element ul) {
* Symbols such as are missing in mid sentence final ulText = ul.text;
* Maybe also JP fullwidth numbers? final ulCharArray = ulText.split('');
*/ final ulChildren = ul.children;
var offsetPointer = 0;
List<Element> result = [];
String getEndSymbolsOfExampleSentence(Element ul) { for (var element in ulChildren) {
final endSymbols = RegExp(r'<\/li>([^<>]+)$'); if (element.text != ulText.substring(offsetPointer, offsetPointer + element.text.length)){
return endSymbols.firstMatch(ul.innerHtml).group(1); var symbols = '';
while (element.text.substring(0,1) != ulCharArray[offsetPointer]) {
symbols += ulCharArray[offsetPointer];
offsetPointer++;
}
final symbolElement = Element.html('<span>' + symbols + '</span>');
result.add(symbolElement);
}
offsetPointer += element.text.length;
result.add(element);
}
if (offsetPointer + 1 != ulText.length){
final symbols = ulText.substring(offsetPointer, ulText.length-1);
final symbolElement = Element.html('<span>' + symbols + '</span>');
result.add(symbolElement);
}
return result;
} }
ExampleResultData getKanjiAndKana(Element div) { ExampleResultData getKanjiAndKana(Element div) {
final ul = div.querySelector('ul'); final ul = div.querySelector('ul');
final contents = ul.children; final contents = getChildrenAndSymbols(ul);
var kanji = ''; var kanji = '';
var kana = ''; var kana = '';
@ -60,9 +78,6 @@ ExampleResultData getKanjiAndKana(Element div) {
} }
} }
} }
final endSymbols = getEndSymbolsOfExampleSentence(ul).trim();
kanji+= endSymbols;
kana += endSymbols;
return ExampleResultData( return ExampleResultData(
kanji: kanji, kanji: kanji,

View File

@ -1,4 +1,4 @@
import 'package:unofficial_jisho_api/src/objects.dart'; import './objects.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'dart:convert'; import 'dart:convert';