Text Analysis - Advanced - Save Tidied Text

From Q
Jump to: navigation, search

Save the tidied text which has been processed by Text Analysis - Advanced - Setup Text AnalysisInsert > Text Analysis > Advanced > Setup Text Analysis as a new variable in the data set. This blog post describes saving tidied text in order to create a word cloud.

Usage

To use this item, first select an item created by Text Analysis - Advanced - Setup Text AnalysisInsert > Text Analysis > Advanced > Setup Text Analysis.

Code

includeWeb("QScript Utility Functions");
includeWeb("QScript Functions to Generate Outputs");
includeWeb("QScript R Output Functions");
includeWeb("QScript Selection Functions");
 
if (fileFormatVersion() < 9.11)
    log("This script requires a newer version.")
else
    main();
 
function main() {
 
    var bad_selection_message = "Select an item that has been created using Text Analysis > Setup Text Analysis.";
    var web_mode = (!!Q.isOnTheWeb && Q.isOnTheWeb());
 
    var selected_item = getSelectedROutputFromPage(["wordBag"]);
    if (selected_item === null) {
        log(bad_selection_message);
        return false;
    }

    var source_question = selected_item.getInput("formtextvar");
    var target_data_file = source_question.dataFile;
 
    var r_expression = "library(flipTextAnalysis)\r\n"
                     + "tidied.text = SaveTidiedText(" + stringToRName(selected_item.referenceName) + ")";
 
    var new_q_name = preventDuplicateQuestionName(target_data_file, "Tidied Text from " + selected_item.name);
    var new_var_name = cleanVariableName(selected_item.name) + "_tidied_text";
    new_var_name = preventDuplicateVariableName(target_data_file, new_var_name);
    try {
        var new_var = target_data_file.newRVariable(r_expression, new_var_name, new_q_name, null);
    } catch (e) {
        log("Save Tidied Text could not be created from this item: " + e);
        return false;
    }
    var new_question = new_var.question;
 
 
    // In Q, select the table showing the new variable. 
    if (!web_mode) {
        var new_group = generateGroupOfSummaryTables(new_q_name, [new_question]);
        project.report.setSelectedRaw([new_group.subItems[0]]);
    }
 
    return true;
}