In this assignment, you are tasked with writing a web server what will be capable of translating text between English, Spanish, and German.
You should write all of your code in a single file named translator.js
.
An API (Application Programming Interface) is basically any interface that can be used by programs and programmers to gain access to various functionality. In this case your web server will act as a simple “Web-based API” that can be delegated the task of translating text between languages. The user of this web API can request to do various types of translations by changing the URL of the request.
For the purposes of this assignment, your server only has to be set up to run locally on your own computer.
It should run at the IP address 127.0.0.1
(aka localhost
) and on port 5000
.
The translation functionality will be controlled by the URL, particularly the path. The structure of a request to translate text by this server should be as follows:
http://127.0.0.1:5000/translate/TYPE/CONTENT
Notice the path structure of /translate/TYPE/CONTENT
.
The translate
part just indicated to the server that this is a request to translate text.
The TYPE
part of the URL can be one of 6 different types of translation:
e2s
(English to Spanish)s2e
(Spanish to English)e2g
(English to German)g2e
(German to English)g2s
(German to Spanish)s2g
(Spanish to German)The CONTENT
portion will be the actual words to translate, where spaces are replaced with +
signs.
A few examples:
http://127.0.0.1:5000/translate/e2s/you+want+to+sail
Represents a request to the server to translate the sentence “you want to sail” from English to Spanish.
In this case the server should return “vosotros querir para vela” (more on how that will work later).http://127.0.0.1:5000/translate/g2e/er+laufen+schnell
Represents a request to the server to translate the sentence “er laufen schnell” from German to English.
In this case the server should return “he walk quick”.At this point you might be asking yourself, how will the server know how to translate the text? In order to do so, you’ll have to load in the translation information from these two files:
Below is a short snippet from the Spanish.txt
file, showing an example of the format:
...
nation nacio/n[Noun]
national nacional
nationalities nacionalidades
nationality nacionalidad
nationalization nacionalizacio/n
...
The format is basically that each line has: An English word, then a tab, then a Spanish word, then possibly some other attributes or word alternatives, such as [Noun]
or /n
.
You should read in the contents of both files, and build simplistic mappings (javascript objects) that maps ENGLISH_WORD -> SPANISH_TRANSLATION
and ENGLISH_WORD -> GERMAN_TRANSLATION
.
You also might find it helpful to create two other dictionaries, mapping in the other direction.
As you can probably tell, this will NOT lead to perfect translations, as it does not account for tense and other grammatical context very well.
That is OK.
This is meant to be a super basic translation service.
For the purpose of this PA, you should process each translation line as follows:
As an example, say that you had to process this line from German.txt
:
wader Watvogel, Wasserstiefel[Noun]
The English word would end up being “wader” and it would map to “Watvogel” in German. You would ignore everything after the comma.
After you’ve read in and loaded the translations into a dictionary, you should get a simple http
server set up, and parse the URL path to figure out what kind of translation should happen.
I previously described the format of the requests, so you should grab the path, and do some string processing (splitting, mostly) to get each individual component.
After figuring out if is a translation request or not, what kind of translation to do, and the words to translate, use the js translation objects to proceed with the translation.
For any other non-translation request, the server should just reply with the text OK
.
In this section, I include several visual examples of the server working on localhost:
Make sure that you take a look at the style guide on the class site before turning in your project.
Specifically, make sure that you validate the HTML, and include header comments.
Otherwise you might lose points!
Also, you should NOT use the express
module for this server.
Use the http
module instead.
For this assignment, you only have to turn in a single file, translator.js
.
The project is due by 7pm on Friday, October 6th, 2023.