TopCoder problem "TaglishTranslator" used in SRM 220 (Division II Level Three)

Problem Statement


Tagalog is one of the major languages spoken in the Philippines. Since the USA occupied the Philippines from 1899 to 1946, it has been the custom of Tagalog-speakers to use English words and conjugate them as if they were Tagalog words, creating a language they call "Taglish".

You are to write an English-Taglish translator that will convert a simple English sentence into a Taglish sentence by conjugating the verb and adjusting the syntax according to Tagalog grammar rules.

The English sentence will come in the format:

	<subject> <verb> (<object>)

The <subject> will be either a single-word name or the word "The" followed by a single-word thing. The <verb> will be a single word, or a word preceded by the word "will". It is in the future tense if it is preceded by the word "will", past tense if the verb ends in the letters "ed", or present tense otherwise. If there is an object, it will be in the format of the subject ("name" or "the thing"), but it also may be preceded by the word "to". Note that "the", "will" or "to", or the suffix "ed" may be in any case or mixed case. If the verb is preceded with the word "will" and ends with "ed", it should be considered future tense.

The returned String will be in the format:

	<re-conjugated verb> <subject> (<object>)

The verb conjugation works as follows:

  • Past Tense - Put "nag" at the beginning of the word, and remove the "ed" from the end of it.
  • Present Tense - Double the first syllable and put "nag" at the beginning of the word.
  • Future Tense - Double the first syllable and put "mag" at the beginning of the word, and remove the word "will" from before the verb.

In the present and future tense, the "first syllable" is defined as any consonants at the beginning of the word followed by exactly one vowel (where a vowel is a upper- or lower-case a, e, i, o, or u and a consonant is any other letter).

If the <subject> in the English sentence is a thing, omit the word "the" in the Taglish sentance and prepend the Tagalog article, "ang" to the subject to form the Taglish subject. If the <subject> is a name, prepend the Tagalog article "si" to the English name.

If there was an object in the English sentence, the <object> in the Taglish sentence is the object from the English sentence preceded by a Tagalog article which depends on the nature of the object:

  • If the object is a thing and wasn't preceded by the word "to" in the English sentence, it is preceded by the word "ng" in the Taglish sentence (and the word "the" is dropped).
  • If the object is a name and wasn't preceded by the word "to", it is preceded by the word "ni".
  • If the object is a thing and was preceded by the word "to", it is preceded by the word "sa" (and the words "to" and "the" are dropped).
  • If the object is a name and was preceded by the word "to", it is preceded by the word "kay" (and the word "to" is dropped).

Case should be preserved in both the words that are carried over and in any doubling of syllables, but new words or letters added to words should be added in lower-case.



Method signature:String translate(String sentence)
(be sure your method is public)


-'y' is never a vowel in Tagalog.
-The only distinction between a name and a thing is the presence of the word "the".
-Remember that the object may be preceded by two words - "to" and "the". If both are present, the "to" will precede the "the" (as in English).
-Do not change the case of letters in any words. When doubling syllables, use the same case as they were in the existing syllable. When adding other letters or words into the translation, use lower-case letters.
-"To", "the" and "will" will always be "control" words if they appear in the place where they could be used as a "control" word.


-sentence will have a valid subject and verb, and will either end with the verb or have a valid object.
-Each word in sentence will have at least one vowel (a, e, i, o, or u).
-sentence will be between 3 and 50 characters in length, inclusive.
-sentence will have between 2 and 7 words, inclusive, delimited by single spaces.
-sentence will contain only upper- and lower-case letters and spaces.
-sentence will not have any leading or trailing spaces.
-sentence will not have any consecutive spaces (words in sentence will be delimited by single spaces).
-The verb in the sentence will not be "ed".


"The fox jumps to the dog"
Returns: "nagjujumps ang fox sa dog"
These sentences don't allow that many descriptive words.
"tomek codes"
Returns: "nagcocodes si tomek"
Almost as simple of a sentence as we can take.
"tHe mAn plAyEd ThE pIAnO"
Returns: "nagplAy ang mAn ng pIAnO"
Don't forget, control words should be noticed insensitive to case, but case still matters!
"Bob will Filed the taxes"
Returns: "magFiFiled si Bob ng taxes"
"Will" takes preference over "ed".
"Matthew walked to Mathew"
Returns: "nagwalk si Matthew kay Mathew"
"StrongBad kicked TheCheat"
Returns: "nagkick si StrongBad ni TheCheat"

Problem url:

Problem stats url:




PabloGilberto , lbackstrom , brett1479

Problem categories:

String Manipulation, String Parsing