Create New Variables - Translate Text

From Q
Jump to navigation Jump to search

This QScript translates text questions to another language and saves the translated text as new questions. The user is prompted for the text questions to translate, as well as the source and output languages. A variable may be supplied by the user containing the source language for each case.Translates a Text variable set to another language and saves the translated text as a new variable set. You are able to choose the source languag and output language. A variable may be supplied by the user containing the source language for each case. Translation is done by Google Cloud Translation.

Example

The table below shows the original Chinese text in the first column and the English translation in the second column:

How to apply this QScript

  • Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
  • Click on the QScript when it appears in the QScripts and Rules section of the search results.

OR

  • Select Automate > Browse Online Library.
  • Select this QScript from the list.

Customizing the QScript

This QScript is written in JavaScript and can be customized by copying and modifying the JavaScript.

Customizing QScripts in Q4.11 and more recent versions

  • Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
  • Hover your mouse over the QScript when it appears in the QScripts and Rules section of the search results.
  • Press Edit a Copy (bottom-left corner of the preview).
  • Modify the JavaScript (see QScripts for more detail on this).
  • Either:
    • Run the QScript, by pressing the blue triangle button.
    • Save the QScript and run it at a later time, using Automate > Run QScript (Macro) from File.

Customizing QScripts in older versions

  • Copy the JavaScript shown on this page.
  • Create a new text file, giving it a file extension of .QScript. See here for more information about how to do this.
  • Modify the JavaScript (see QScripts for more detail on this).
  • Run the file using Automate > Run QScript (Macro) from File.

JavaScript

includeWeb("QScript Utility Functions");
includeWeb("QScript Selection Functions");
includeWeb("QScript Functions to Generate Outputs");
includeWeb("QScript R Output Functions");

var languages = ["Afrikaans", "Albanian", "Amharic", "Arabic", "Armenian",
                 "Assamese", "Aymara", "Azerbaijani", "Bambara", "Basque", "Belarusian",
                 "Bengali", "Bhojpuri", "Bosnian", "Bulgarian", "Catalan", "Cebuano",
                 "Chichewa", "Chinese (Simplified)", "Chinese (Traditional)",
                 "Corsican", "Croatian", "Czech", "Danish", "Divehi", "Dogri",
                 "Dutch", "English", "Esperanto", "Estonian", "Ewe", "Filipino",
                 "Finnish", "French", "Frisian", "Galician", "Ganda", "Georgian",
                 "German", "Greek", "Guarani", "Gujarati", "Haitian Creole",
                 "Hausa", "Hawaiian", "Hebrew", "Hindi", "Hmong", "Hungarian",
                 "Icelandic", "Igbo", "Iloko", "Indonesian", "Irish Gaelic", "Italian", "Japanese",
                 "Javanese", "Kannada", "Kazakh", "Khmer", "Kinyarwanda", "Konkani",
                 "Korean", "Krio", "Kurdish (Kurmanji)", "Kurdish (Sorani)", "Kyrgyz",
                 "Lao", "Latin", "Latvian", "Lingala", "Lithuanian", "Luxembourgish",
                 "Macedonian", "Maithili", "Malagasy", "Malay", "Malayalam", "Maltese",
                 "Maori", "Marathi", "Meiteilon (Manipuri)", "Mizo", "Mongolian",
                 "Myanmar (Burmese)", "Nepali", "Northern Sotho", "Norwegian",
                 "Odia (Oriya)", "Oromo", "Pashto", "Persian", "Polish", "Portuguese",
                 "Punjabi", "Quechua", "Romanian", "Russian", "Samoan", "Sanskrit",
                 "Scots Gaelic", "Serbian", "Sesotho", "Shona", "Sindhi", "Sinhala",
                 "Slovak", "Slovenian", "Somali", "Spanish", "Sundanese", "Swahili",
                 "Swedish", "Tajik", "Tamil", "Tatar", "Telugu", "Thai", "Tigrinya",
                 "Tsonga", "Turkish", "Turkmen", "Twi", "Ukrainian", "Urdu", "Uyghur",
                 "Uzbek", "Vietnamese", "Welsh", "Xhosa", "Yiddish", "Yoruba", "Zulu"];


function getVariableOrQuestionLabel(variable) {
	if(/- Multi/.test(variable.question.variableSetStructure)) {
		return variable.question.name + " " + variable.label;
	} else {
		return variable.label;
	}
}

translateText();

function translateText() {
    const allowed_types = ["Text", "Text - Multi"];
    let selected_questions = selectInputQuestions(allowed_types);
    if (!selected_questions)
        return false;
    let data_file = getDataFileFromQuestions(selected_questions);
    let source_language;
    let language_var_guid;
    if (!inDisplayr()) {
        let source_index = selectOne('Select the source language', ["English", "Specify with variable", "Auto-detect"].concat(languages), null, 0);
        if (source_index === 0)
            source_language = "'English'";
        else if (source_index === 1)
        {
            let candidate_variables = getVariables(data_file.questions).filter(function(x) { return(!x.question.isHidden) });
            let source_language_var = selectOneVariableByNameAndLabel("Select the source language variable", candidate_variables, false);
            source_language = "'Specify with variable'"
            language_var_guid = source_language_var.guid;
        } else if (source_index === 2) {
            source_language = "'Auto-detect'";
        } else
            source_language = "'" + languages[source_index - 3] + "'";
    } else
        source_language = "'Auto-detect'";

    // Prompt user for output language
    let output_index = selectOne('Select the output language (translated by Google Cloud Translation)', ["English"].concat(languages), null, 0);
    if (output_index === 0)
        output_language = "'English'";
    else
        output_language = "'" + languages[output_index - 1] + "'";

    if (source_language == output_language)
    {
        log("The source language is the same as the output language. No translation has been performed.");
        return false;
    }

    let r_code = `
source.lang <- get0("formSourceLanguageVariable", ifnotfound = formSourceLanguage)
apply(as.data.frame(formInputs, optional = TRUE), 2, flipTextAnalysis::Translate,
          source.language = source.lang, target.language = formOutputLanguage)`;

    let js_code   = `
let languages = ['${languages.join("','")}'];
form.dropBox({name: 'formInputs',
              label: 'Variables',
              duplicates: true,
              types: ['Variable:Text'],
              multi:true,
              prompt: 'Text variables to translate.'});
let source_language = form.comboBox({name: 'formSourceLanguage',
               alternatives: ["Auto-detect", "English", "Specify with variable"].concat(languages),
               label: 'Source language',
               prompt: 'Source language of input text. Use "Auto-detect" to automatically detect the language',
               default_value: ${source_language}}).getValue();
if (source_language === "Specify with variable")
    form.dropBox({name: 'formSourceLanguageVariable',
                                label: 'Source language variable',
                                types: ['Variable:Text,Categorical'],
                                multi:false,
                                prompt: 'Text variable containing the source language.'});
form.comboBox({name: 'formOutputLanguage',
               alternatives: ["English"].concat(languages),
               label: 'Output language',
               prompt: 'Language to use for output text.',
               default_value: ${output_language}});
`; 


    let new_r_questions = [];
    const structure_name = inDisplayr() ? "variable set" : "question";
    for (var i = 0; i < selected_questions.length; i++)
    {
        let question = selected_questions[i];
        let variables = question.variables;
        let last_variable = getLastVariable(variables);
        let new_question_name = preventDuplicateQuestionName(question.dataFile, output_language.replace(/'/g, "") + " translation of " + question.name);
        let temp_var_name = randomVariableName(16); // temporary name, random to (almost) guarantee uniqueness
	    let controls = {formInputs: variables.map(v => v.guid).join(';')};
        if (source_language === "'Specify with variable'")
            controls['formSourceLanguageVariable'] = language_var_guid;
        // Run expression to create R question
        let new_r_question;
        try {
            new_r_question = data_file.newRQuestion(r_code, new_question_name, temp_var_name, last_variable, js_code, controls);
            if (variables.length === 1)
                new_r_question.questionType = "Text";
            else
                new_r_question.questionType = "Text - Multi";
            insertAtHoverButtonIfShown(new_r_question);
        }
        catch (e) {
            log("The translation could not be performed for this " + structure_name + " : " + e);
            return false;
        }

        // Replace temporary variable names
        nameSequentialVariables(new_r_question.variables, "translated");
        new_r_questions.push(new_r_question);
    }
    reportNewRQuestion(new_r_questions, "Translated question(s)");
    return true;
}

See also