def count_unique_translations(self, src, lang): """Count the translations for a given key in the given language. Args: src (str): The text that is translated lang (str): The language in which it is translated Returns: An integer of how many translations are found for the given source text """ # Set the default return value to 0 count_unique = 0 # Clean source text cleaned_src = utils.td_clean_string(src) # If the text key is in translations if cleaned_src in self.data: # Get all translations for a given key all_src_data = self.data[cleaned_src] # If the language is among the translations if lang in all_src_data: # Collect all the unique translation strings translations = all_src_data[lang] all_found = [other['translation'] for other in translations] unique_all_found = set(all_found) # Set the return value to the size of the set count_unique = len(unique_all_found) return count_unique
def get_unique_translations(self, src, lang): """Return the unique translations for a given text in a given language. Args: src (str): The text that is translated lang (str): The language in which it is translated Returns: A list of sorted, unique translations """ # Set the default return value to an empty list unique = [] # Clean source text cleaned_src = utils.td_clean_string(src) # If the text key is in translations if cleaned_src in self.data: # Get all translations for a given key all_src_data = self.data[cleaned_src] # If the language is among the translations if lang in all_src_data: # Collect all the unique translation strings translations = all_src_data[lang] all_found = [other['translation'] for other in translations] unique_all_found = set(all_found) # Set the return value to the size of the set unique = sorted(list(unique_all_found)) return unique
def add_translation(self, src, other, lang, correct=False): """Add a translation to the dictionary. Many strings to be added come from questionnaires where a numbering scheme is used. The question number is removed from the text if it is discovered using the `number_prog` attribute. The cleaned translation is stored in the `other` dictionary. Args: src (str): String in the base language other (dict): A dictionary containing the CellData namedtuple and other metadata. lang (str): String name of other language correct (bool): Whether or not the input file is treated as correct """ cleaned_src = utils.td_clean_string(src) cleaned_other = utils.td_clean_string(str(other['cell'])) other['translation'] = cleaned_other if not correct and cleaned_src in self.correct: # Currently not a correct translation, but we have correct return if correct and cleaned_src not in self.correct and cleaned_src in \ self.data: # Remove the old, non-correct translation self.data.pop(cleaned_src, None) if correct: self.correct.add(cleaned_src) try: this_dict = self.data[cleaned_src] if lang in this_dict: this_dict[lang].append(other) else: this_dict[lang] = [other] except KeyError: self.data[cleaned_src] = {lang: [other]}
def get_numbered_translation(self, src, lang): """Return a translation for a source string, respecting numbering. Since many strings come from questionnaires with a numbering scheme, this method first removes the number, then translates the numberless text, and finally adds the number back. Args: src (str): String in base language for this translator. lang (str): String name of language for the translation. Returns: String in other language that is a translation of `src`. This string also has the same numbering as `src`. """ number, _ = utils.td_split_text(src) cleaned_src = utils.td_clean_string(src) clean_translation = self.get_translation(cleaned_src, lang) numbered_translation = number + clean_translation return numbered_translation