Text Analysis - Advanced - Save Variable(s) - Tidied Text

From Q
Jump to navigation Jump to 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

var __webpack_modules__ = ({});
// The module cache
var __webpack_module_cache__ = {};

// The require function
function __webpack_require__(moduleId) {

// Check if module is in cache
var cachedModule = __webpack_module_cache__[moduleId];
if (cachedModule !== undefined) {
return cachedModule.exports;
}
// Create a new module (and put it into the cache)
var module = (__webpack_module_cache__[moduleId] = {
exports: {}
});
// Execute the module function
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);

// Return the exports of the module
return module.exports;

}

// webpack/runtime/rspack_version
(() => {
__webpack_require__.rv = () => ("1.7.2")
})();
// webpack/runtime/rspack_unique_id
(() => {
__webpack_require__.ruid = "bundler=rspack@1.7.2";
})();
includeWeb("QScript Utility Functions");
includeWeb("QScript Functions to Generate Outputs");
includeWeb("QScript R Output Functions");
includeWeb("QScript Selection Functions");

saveTidiedText();

function saveTidiedText() {
    let web_mode = inDisplayr();

    let selected_item = getSelectedROutputFromPage(["wordBag"]);
    if (selected_item === null) {
        log("Select an item that has been created using Text Analysis > Setup Text Analysis.");
        return false;
    }
    let target_data_file = getDataFileFromItemDependants(selected_item);
    if (target_data_file == null) {
        log("'Save variables' cannot be applied to an output with no associated data file.");
        return;
    }
    let r_expression = `library(flipTextAnalysis)
tidied.text = SaveTidiedText(${stringToRName(selected_item.referenceName)})`;

    let new_q_name = preventDuplicateQuestionName(target_data_file, "Tidied Text from " + selected_item.name);
    let new_var_name = cleanVariableName(selected_item.name) + "_tidied_text";
    new_var_name = preventDuplicateVariableName(target_data_file, new_var_name);
    let new_var;
    try {
        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;
    }
    let new_question = new_var.question;


    // In Q, select the table showing the new variable. 
    if (!web_mode) {
        let new_group = generateGroupOfSummaryTables(new_q_name, [new_question]);
        project.report.setSelectedRaw([new_group.subItems[0]]);
    }

    return true;
}