Project 2: API-based Dictionary App
Project 2: API-Based Dictionary & Vocab Builder in Python
In this intermediate project, we apply modern HTTP networking and local state persistence—HTTP Requests with requests, JSON Parsing, Exception Handling, and File Serialization with json—to construct a feature-complete Command-Line Dictionary & Vocabulary Builder.
1. System Architecture & API Specification
The application integrates with the open, public Free Dictionary API (https://api.dictionaryapi.dev/api/v2/entries/en/<word>).
Key Capabilities:
- 1Live Lexical Lookup: Queries word definitions, parts of speech, phonetic pronunciations, example usages, and synonyms.
- 2Local Vocabulary Persistence (
vocabulary.json): Automatically caches looked-up words locally so users can review vocabulary even when offline. - 3Interactive Flashcard Mode: Quizzes the user on previously saved vocabulary words to reinforce learning.
- 4Resilient Network Handling: Handles HTTP
404 Not Found(unknown words), connection errors, and malformed JSON payloads gracefully.
2. Complete Project Implementation
Visual Architecture & Process Flow
How data and code flow step-by-step
3. Sample Execution Simulation
Multiple Choice Questions
1. In this application, how is offline functionality achieved for words previously searched?
A. By downloading the entire Oxford English Dictionary on startup B. By checking the local vocabulary.json cache before making any HTTP request C. By keeping the computer connected to Bluetooth D. Offline lookup is not supported Answer: B Explanation: The client checks if the word is already stored in self.vocab_mgr.vocab (loaded from vocabulary.json); if present, it loads the cached definition without hitting the network.
2. How does the application identify when a queried word does not exist in the dictionary?
A. The API server returns HTTP status code 404 Not Found B. Python throws an IndexError C. The terminal closes D. The API returns a 500 error Answer: A Explanation: The Free Dictionary API returns an HTTP 404 Not Found status when a word cannot be found in the English lexicon.
3. Which method in Python's json module saves the vocabulary dictionary to disk?
A. json.loads() B. json.dump() C. json.write() D. json.export() Answer: B Explanation: json.dump(obj, file) serializes a Python data structure directly into a writable file stream.
4. Why does lookup_word() clean the input using .strip().lower()?
A. Because JSON keys cannot store capital letters B. To ensure case-insensitive consistency and eliminate accidental whitespace in API queries and dictionary lookups C. To convert strings into numbers D. It is required by PEP 8 Answer: B Explanation: Normalizing user strings with .strip().lower() guarantees reliable cache matching and prevents malformed URLs.
5. What random selection function is used in Flashcard mode to select a random saved word?
A. random.randint() B. random.choice() C. random.shuffle() D. random.uniform() Answer: B Explanation: random.choice(sequence) picks a single random element from a non-empty sequence.
Project 3: CLI-based To-do App
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Project 1: Expense Tracker with CSV | Project 3: CLI-based To-do App |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.