You are browsing as a guest. Sign up (or log in) to start making projects!

2h 58m 23s logged

Definitions and JSON

I have completed the code that gets definitions for words. Even though the idea is simple, the code is not!

Dictionary

The long journey of getting definitions starts at dictionaryapi.dev. This site allows anyone get a large amount of info on any given word!
At the end of the URL I can add a word and it will give me a word’s synonyms, example uses, and definitions.
Getting the website data is easy enough using reqwest :

reqwest::get(/* URL */).await.unwrap()
        .text().await.unwrap();

This gets all the data that I need (oh, ya, I’ll explain the use of async code later)!

Info

But, the real tricky part is that the data is in JSON and not Rust (who would have though).
Luckily enough, there is a library, serde_json, that helps with Rust-JSON communication!
interfacing with JSON wasn’t too hard, I just find it unusual how everything was wrapped in a vector; I had to access the data like this:

let word_map = &json_body[0]["meanings"][def_num as usize];

let word_def = &word_map["definitions"][0]["definition"];
let part_of_speech = &word_map["partOfSpeech"];

This could be completely normal but, I don’t know. By the way, [def_num as usize] was originally just [def_num] witch is invalid and silently, always returns null. This took too long to find out :sad-pf: .

Async and whats next

And now, about the async code. The program is taking longer than I would like to run, so, I’m planing on adding some async and multi-threaded code next.

Bye!

0
4

Comments 0

No comments yet. Be the first!