search method
API query under https://krdict.korean.go.kr/api/search
start
- 검색의 시작 번호.
num
- 검색 결과 출력 건수.
advanced
- 자세히찾기 여부. y일 경우 번역 언어 변수가 적용된다.
letterStart
- 음절 수 시작.
letterEnd
- 음절 수 끝.
Implementation
Future<Iterable<SearchItem>> search(
String queryText, {
Iterable<TranslationLanguage>? transLang,
Sort? sort,
Iterable<PartOfSpeech>? pos,
Iterable<WordGrade>? level,
SearchMethod? method,
WordUnit? wordUnit,
WordType? wordtype,
int start = 1,
int num = 10,
// (hyungtaecf) I couldn't find any difference in the results when toggling
// "advanced". If anyone finds any difference please let me know.
bool advanced = false,
int letterStart = 1,
int? letterEnd,
}) async {
assert(queryText.isNotEmpty, "A query text should be provided.");
assert(start > 0 && start <= 1000, "Invalid [start] value.");
assert(num >= 10 && num <= 100, "Invalid [num] value.");
assert(letterStart > 0, "Invalid [letterStart] value.");
assert(letterEnd == null || letterEnd > 0, "Invalid [letterEnd] value.");
const apiPath = "/api/search";
final headers = <String, String>{};
final uri = Uri.https(baseUrl, apiPath, <String, String>{
..._getBaseQueryParameters(queryText, transLang),
if (sort != null) "sort": sort.name,
if (pos != null) "pos": pos.map<int>((e) => e.code).join(','),
if (level != null) "level": level.map<String>((e) => e.req).join(','),
if (method != null) "method": method.name,
if (wordUnit != null) "type1": wordUnit.name,
if (wordtype != null) "type2": wordtype.name,
if (start > 1) "start": "$start",
"num": "$num",
if (advanced) "advanced": "y", // yes
if (letterStart > 1) "letter_s": "$letterStart",
if (letterEnd != null) "letter_e": "$letterEnd",
// TODO(hyungtaecf): Implement other parameters (part, target, lang,
// multimedia, sense_cat, subject_cat)
});
final response = await http.get(uri, headers: headers);
final xml = XmlDocument.parse(response.body);
return xml
.findAllElements(ApiXmlElement.item.name)
.map(SearchItem.fromXmlElement);
}