GIML: Language Translation
Diversion Language translation
Write a program to translate English into Scots - for example
- scots("Do you know where Pat lives");
> "Do you ken where Pat bides"
Use the same functions to translate to politically correct speak
You will need the function lex which turns a list of characters into a list of words. The functions firstWord and butFirstWord should help.
lex(explode "one fine day") = ["one", "fine", "day"]
A function to translate a single words is quite simple:
fun franglais "house" = "maison"
| franglais "dog" = "chien"
| franglais "beware" = "regarde"
| franglais "at" = "dans"
| franglais "the" = "le"
| franglais x = x;
The last line insures that if we have missed a word out it is unchanged:
franglais "table" = "table"
Given a words translator we now need to put back spaces and implode:
fun addSpace s = s^" ";
A generalized translator then takes a "word function" f:
fun trans f = implode o(map (addSpace o f))o lex o explode;
trans franglais "beware the dog at Hectors house";
The function lex could be improved so that instead of searching for a space it searches for a non-alpha character. If we also partition the list rather than remove spaces the punctuation may be retained and spaces need not be reintroduced.
fun alpha s = (s>="A" andalso s<="Z") orelse
(s>="a" andalso s<="z");
fun takewhile f nil = nil
| takewhile f (h::t) = if f h then h::(takewhile f t)
fun dropwhile f nil = nil
| dropwhile f (h::t) = if f h then dropwhile f t else h::t;
fun lex nil = nil
| lex l = (takewhile alpha l)::
(takewhile (not o alpha) (dropwhile alpha l))::
(lex (dropwhile (not o alpha) (dropwhile alpha l)));