QScript Sentiment Analysis Functions

From Q
Jump to navigation Jump to search
This page is currently under construction, or it refers to features which are under development and not yet available for use.
This page is under construction. Its contents are only visible to developers!
function saveSentimentScores() {
    includeWeb("QScript Utility Functions");
    includeWeb("QScript Functions to Generate Outputs");
    includeWeb("QScript R Output Functions");
    includeWeb("QScript Selection Functions")
 

 
    // // Figure out what input the user has selected.
    // // R items take precendence over questions that
    // // are selected in the Data or in the selected
    // // table, but they must have the right class.
    // let selected_object;
    // const user_selections = getAllUserSelections();
    // let selected_r_items = user_selections.implicitly_selected_r_outputs;
    // let selected_text_questions = user_selections.selected_questions.filter(function (q) { return q.questionType == "Text"; });
 
    // // Look on the current page for text or wordBag items
    // let selected_raw = project.report.selectedRaw();
    // let selected_item = selected_raw[0];

    // let selected_group = user_selections.selected_top_level_pages[0];

    // let tables_on_page = [];
    // let text_on_page = [];
    // let word_bags_on_page = [];
 
    // if (selected_group != null) { // Don't look at on-page items if text variable is selected directly
    //     tables_on_page = selected_group.subItems.filter(function (item) { 
    //         return item.type == "Table" 
    //                 && item.primary != null 
    //                 && item.primary.questionType == "Text"; });
    //     text_on_page = tables_on_page.map(function (table) { return table.primary; });
    //     word_bags_on_page = selected_group.subItems.filter(function (item) { 
    //         return item.type == "R Output" 
    //                  && item.error === null 
    //                  && item.outputClasses !== null
    //                  && item.outputClasses.indexOf("wordBag") > -1; });
    // }


   



    function getBestTextSelection() {
         // Selection
        // - A WordBag output
        // - A Text Question in Data Sets
        // - A Table with a Text question in the rows (comes for free in selected_questions)
        
        const bad_selection_message = "Select either a table with text data, "
        + "a Text variable, or an item that has been created using Text Analysis > Setup Text Analysis.";
        const user_selections = getAllUserSelections();
        let selected_r_items = user_selections.selected_r_outputs;
        let selected_questions = user_selections.selected_questions;
        let selected_text_questions = selected_questions.filter(function (q) { return q.questionType == "Text"; });
        let selected_tables = user_selections.selected_tables;
        let selected_primary_text = user_selections.questions_in_rows_of_selected_tables.filter(function (q) { return q.questionType == "Text"; });

        // Can't operate on multiple R outputs.
        if (selected_r_items.length > 1) {
            log("Too many outputs selected. " + bad_selection_message);
            return null;
        }

        // If one output selected, make sure it is
        // calculated, then make sure it has the right class
        if (selected_r_items.length == 1) {
            if (!checkROutputIsValid(selected_r_items[0]))
                return null;
            if (selected_r_items[0].outputClasses.indexOf("wordBag") == -1) {
                log(bad_selection_message);
                return null;
            }
            return selected_r_items[0];
        }


        if (selected_text_questions.length > 1) {
            log(correctTerminology("Too many questions selected. ") + bad_selection_message);
            return null;
        }

        if (selected_text_questions.length == 1)
            return selected_text_questions[0];

        if (selected_primary_text.length > 1) {
            log("Too many tables selected. " + bad_selection_message);
            return null;
        }

        if (selected_primary_text.length == 1) {
            return selected_primary_text[0];
        }

        log(bad_selection_message);
        return null;
    }




 
    // if (selected_r_items.length > 1) {
    //     log("Too many inputs selected. " + bad_selection_message);
    //     return false;
    // } else if (selected_r_items.length == 1) {
    //     selected_object = selected_r_items[0];
    //     if (!checkROutputIsValid(selected_object))
    //         return false;
    //     if (selected_object.outputClasses.indexOf("wordBag") == -1) {
    //         log(bad_selection_message);
    //         return false;
    //     }
    // } else if (selected_text_questions.length > 1) {
    //     log("Too many inputs selected. " + bad_selection_message);
    //     return false;
    // } else if (selected_text_questions.length == 1 ){
    //     selected_object = selected_text_questions[0];
    // } else if (text_on_page.length + word_bags_on_page.length == 1) {
    //     if (word_bags_on_page.length == 1)
    //         selected_object = word_bags_on_page[0];
    //     else
    //         selected_object = text_on_page[0];
    // } else {
    //     log(bad_selection_message);
    //     return false;
    // }
 
    let selected_object = getBestTextSelection();
    if (selected_object == null)
        return;

 
    // Figure out the source question so we can put the new
    // variable below it, and to disambiguate in the case of
    // multiple data files.
    let source_question;
    let selected_type = selected_object.type;
    if (selected_type == "Question")
        source_question = selected_object;
    else {
        source_question = selected_object.getInput("formtextvar");
    }
 
    // Figure out the right data file to add to (if more than one)
    let target_data_file = project.dataFiles[0];
    if (project.dataFiles.length > 0) {
        target_data_file = source_question.dataFile;
    }
 
    let input_string = selected_type == "Question" ? generateDisambiguatedVariableName(selected_object.variables[0]) 
                                                   : stringToRName(selected_object.referenceName);     
    let r_expression = "library(flipTextAnalysis)\r\n"
                    +  "sentiment.scores = SaveNetSentimentScores(" + input_string 
                    + ", check.simple.suffixes = TRUE, blanks.as.missing = TRUE)"; 
 
    let new_q_name = preventDuplicateQuestionName(target_data_file, "Sentiment scores from " + selected_object.name);
    let new_var_name = selected_type == "Question" ? selected_object.variables[0].name : selected_object.name;
    new_var_name = cleanVariableName(new_var_name);
    new_var_name = preventDuplicateVariableName(target_data_file, new_var_name + "_sentiment");
    let new_var;
    try {
        new_var = target_data_file.newRVariable(r_expression, new_var_name, new_q_name, null);
    } catch (e) {
        log("Sentiment scores could not be created from this item: " + e);
        return false;
    }
    let new_question = new_var.question;
 
    // Move the new variable below its input
    target_data_file.moveAfter([new_var], source_question.variables[0]);
    insertAtHoverButtonIfShown(new_question);
    // Generating outputs
    if (!inDisplayr()) {
        var new_group = generateGroupOfSummaryTables(new_q_name, [new_question]);
        project.report.setSelectedRaw([new_group.subItems[0]]);
    }
 
    return true;
}