Rank Word-Translation Hints from Sentence Translations
Problem statement
An English sentence has one list of Spanish translation hints for each whitespace-delimited word occurrence. You are also given one bestTranslation and ordered alternativeTranslations. Every translation has the same number of whitespace-delimited tokens as the English sentence.
For each word position, rank its hints in two stages:
- Lexicographically sort the hints.
- Assign sentence priority. A hint matching the normalized token at that position in
bestTranslationhas priority0. Otherwise, its priority isj + 1for the first alternative translation at indexjwhose token matches. A hint appearing in no sentence has priorityalternativeTranslations.length + 1. Sort by priority, using the stage-one lexicographic order to break ties.
Normalization lowercases a token and removes all leading and trailing ASCII punctuation characters .,!?;:. Punctuation inside a token is retained. Return the sorted hint list for every word position, preserving the original hint strings in the output.
Function
rankTranslationHints(sentence: String, hints: String[][], bestTranslation: String, alternativeTranslations: String[]) → String[][]Examples
Example 1
sentence = "I eat apples."hints = [["me","yo"],["comer","como"],["fruta","manzana","manzanas"]]bestTranslation = "Yo como manzanas."alternativeTranslations = ["Me comer fruta.","Yo comer manzana!"]return = [["yo","me"],["como","comer"],["manzanas","fruta","manzana"]]Best-sentence tokens come first. For the final word, fruta appears in the first alternative and manzana in the second. Trailing punctuation is removed for matching.
Example 2
sentence = "Hello world"hints = [["hola","buenas"],["tierra","mundo"]]bestTranslation = "¡Hola mundo!"alternativeTranslations = []return = [["buenas","hola"],["mundo","tierra"]]The contract strips only the listed ASCII punctuation. The leading inverted exclamation mark in ¡Hola is retained, so neither first-position hint matches and lexical order decides that list. The trailing ASCII ! is stripped from mundo!.
Constraints
- The English sentence contains between
1and100whitespace-delimited tokens. hints.lengthequals the English token count.- Each hint list contains between
1and50non-empty strings. bestTranslationand every alternative contain the same number of whitespace-delimited tokens as the English sentence.0 <= alternativeTranslations.length <= 20.- Lexicographic comparison uses the original string values.