Tutorial: Vocabulary Trainer - Manhunter07/MFL GitHub Wiki
Learning a new language might be hard, but tools like school books or digital learning software often makes suffering a bit less serious. In this tutorial, we want to develop a little console application for vocabulary training. You should be able to do the following things in your app, all via console:
- Select a language to learn or test
- Enter a foreign word or phrase together with its translation for the selected language
- Study a foreign word or phrase by printing it or its translation to the console, together with its correct translation
- Testing your skills by starting a short vocabulary test
All vocabulary should be saved in a file on your disk and should be loaded when the app starts.
Concept
Data model
First, we need to think of a suited data model to store the information. We need to think of a format that can both safely be stored in files and is easy to access when loaded in memory. There exist a whole set of data formats, but since we only need string value pairs with no special characters in them, the easiest would be to store them in key-value pair strings, separated by a character (ie. =
). We can save all entries in one file per language, making it easily accessible and logically-structured:
german.txt
italian.txt
For in-app memory, all we need to do is to create an array of items for each language. An item in this context consists of two strings: One for the source and one for the target language. Thus, this language-specific record array structure should be sufficient for our needs:
[{ Source = "tree", Target = "Baum"}, { Source = "lay", Target = "liegen"}, ...]
Requirements
Let us reinterate our goal in detail and identify what packages will be required to realize it.
- Since we want to work with user input from and program output to the console, we need the
System.Console
package. It offers a hand full of standard console I/O routines that we can use to work with the terminal. - Since we want to save and load our vocabulary lists to and from files, we need at least basic file access. The easiest way to achieve this are the functions from the
System.File
package.
All the rest that we need will be either done by ourself, or we will be using built-in functions from the System
package.
Preparation
Linking the required packages
As a first prerequisite, let us link all required packages. We can do this anywhere in the code, but it's conventional to do this in the beginning:
link System.Console, System.File
Creating types for our data model
Secondly, another step that might save us time and headache later would be to declare some types for our vocabulary data to be stored in:
type Entry = record(Source: string, Target: string)
type Dictionary = array(0: Entry)
type Library = record() \we will use keys as language
Implementation
As we are done with the ground work, let's do what we came here for: Actual programming.
Loading/saving dictionaries
The first thing we need to implement is the most essential one: We need to write functions for loading and saving dictionaries. As earlier described, each dictionary is stored in a separate *.txt
file. The name of the file is what we take as name for the language (ie. german.txt
is the dictionary for German). This has its limitations as some languages names are hard to store as file names and later as record keys, but for our tutorial, this is the way to go. The content is a list of key-value pairs, separated by an equal-sign (=
).
Function for loading
function LoadDict(Language: string): Dictionary =
function LoadLib: Library =
Function for saving
function SaveDict(Dict: Dictionary): Boolean =
function SaveLib(Lib: Library): Boolean =