<commit_before>window.debug = false
window.default_hostname = window.location.hostname
window.web_resource_base = "http://" + window.default_hostname + ":8000/"
window.jsonrpc_id = 0
window.wrong_command_format_str = "Invalid command format"
window.sentinel_value = "" 
window.syntax_error_value = -1
function alert_if_debug(alert_str) {
    if(window.debug) {
	alert("DEBUG: " + alert_str)
    }
}

function was_successful_call(returnedData) {
    return 'result' in returnedData
}

function handle_error(error, term) {
    error_str = "error message: " + error['message']
    term.echo(error_str)
    console.log(error)
}

function parse_infer_result(infer_return) {
    ret_str = ""
    for(var i=0; i<infer_return.length; i++) {
	infer_return_i = infer_return[i]
	coordinates = String([infer_return_i[0], infer_return_i[1]])
	value = String(infer_return_i[2])
	ret_str += coordinates + ": " + value + "\n"
    }
    return ret_str
}

function echo_if_debug(echo_str, term) {
    if(window.debug) {
	term.echo("DEBUG: " + echo_str)
    }
}

function if_undefined(variable, default_value) {
    if(typeof(variable)=="undefined") {
	variable = default_value
    }
    return variable
}

function nan_to_blank(el) {
    return String(el).toUpperCase()=="NAN" ? "" : el
}   

function map_nan_to_blank(data) {
    row_mapper = function(row) { return row.map(nan_to_blank) }
    return data.map(row_mapper)
}

function convert_column_str_list(columns) {
    columns_out = []
    for(column_idx in columns) {
	column_dict = {"sClass": "center", "sTitle": columns[column_idx]}
	columns_out.push(column_dict)
    }
    return columns_out
}

function load_to_datatable(data, columns, sorting) {
    $('#dynamic').html( '<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"></table>' );
    data = if_undefined(data, [])
    data = map_nan_to_blank(data)
    columns = if_undefined(columns, [])
    sorting = if_undefined(sorting, [])
    $('#example').dataTable( {
	"aaData": data,
	"aoColumns": columns,
	"aaSorting": sorting,
	"aLengthMenu": [100, 200, 1000],
	"iDisplayLength": 100
    } );
}

/* Converter functions*/
function encodeValues(observedValue){
    return observedValue
}

function missingValuesQuerisToJSON(missingValueIndices){
    q = missingValueIndices
    return {"q":q}
}

function findNaNValuesInCSV(csvFile){
    var arrayDataSet = $.csv.toArrays(csvFile);
    var numCols = arrayDataSet[0].length - 1 
    var numRows = arrayDataSet.length - 1
    var missingValsList = []
    for (var i = 1; i < numRows + 1; i++){
	for (var j = 0; j < numCols; j++){
	    if (arrayDataSet[i][j] == "NaN"){
		missingValsList.push([i -1 , j - 1])
	    }
	    
	}
    }
    return missingValsList
}

function set_menu_option(option_value) {
    jQuery(document.getElementById('menu')).append(
	"<option value='" + option_value + "'>" + 
	    option_value + "</option>"
    )
}

function JSONRPC_send_method(method_name, parameters, function_to_call) {
    window.jsonrpc_id += 1
    data_in =
	{ id : toString(window.jsonrpc_id),
	  jsonrpc : "2.0",
	  method : method_name,
	  params : parameters }
    $.ajax({
	url: window.JSONRPC_URL, 
	type: 'PUT', 
	data: JSON.stringify(data_in),
	dataType: 'json', 
	async: true,	
	crossDomain: true,
	success:function(data) {
	    function_to_call(data)
	},
	error: function(httpRequest, textStatus, errorThrown) {
	    console.log(httpRequest)
	    console.log(textStatus)
	    console.log(errorThrown)
	    alert("Sorry, some error has happened.")
	},
	complete: function() {
	    //console.log('COMPLETE');
	}		
    });			    
}

function to_upper_case(str) {
    return str.toUpperCase()
}

function get_idx_of(term, str_array) {
    return $.inArray(term.toUpperCase(), str_array.map(to_upper_case))
}

function in_list(term, str_array) {
    return get_idx_of(term, str_array) != -1
}

function get_idx_after(term, str_array) {
    return get_idx_of(term, str_array) + 1
}

function get_el_after(term, str_array) {
    // case insensitive
    idx = get_idx_after(term, str_array)<commit_msg>add helper function call_and_save to call arbitrary middleware functions<commit_after>window.debug = false
window.default_hostname = window.location.hostname
window.web_resource_base = "http://" + window.default_hostname + ":8000/"
window.jsonrpc_id = 0
window.wrong_command_format_str = "Invalid command format"
window.sentinel_value = "" 
window.syntax_error_value = -1
function alert_if_debug(alert_str) {
    if(window.debug) {
	alert("DEBUG: " + alert_str)
    }
}

function was_successful_call(returnedData) {
    return 'result' in returnedData
}

function handle_error(error, term) {
    error_str = "error message: " + error['message']
    term.echo(error_str)
    console.log(error)
}

function parse_infer_result(infer_return) {
    ret_str = ""
    for(var i=0; i<infer_return.length; i++) {
	infer_return_i = infer_return[i]
	coordinates = String([infer_return_i[0], infer_return_i[1]])
	value = String(infer_return_i[2])
	ret_str += coordinates + ": " + value + "\n"
    }
    return ret_str
}

function echo_if_debug(echo_str, term) {
    if(window.debug) {
	term.echo("DEBUG: " + echo_str)
    }
}

function if_undefined(variable, default_value) {
    if(typeof(variable)=="undefined") {
	variable = default_value
    }
    return variable
}

function nan_to_blank(el) {
    return String(el).toUpperCase()=="NAN" ? "" : el
}   

function map_nan_to_blank(data) {
    row_mapper = function(row) { return row.map(nan_to_blank) }
    return data.map(row_mapper)
}

function convert_column_str_list(columns) {
    columns_out = []
    for(column_idx in columns) {
	column_dict = {"sClass": "center", "sTitle": columns[column_idx]}
	columns_out.push(column_dict)
    }
    return columns_out
}

function load_to_datatable(data, columns, sorting) {
    $('#dynamic').html( '<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"></table>' );
    data = if_undefined(data, [])
    data = map_nan_to_blank(data)
    columns = if_undefined(columns, [])
    sorting = if_undefined(sorting, [])
    $('#example').dataTable( {
	"aaData": data,
	"aoColumns": columns,
	"aaSorting": sorting,
	"aLengthMenu": [100, 200, 1000],
	"iDisplayLength": 100
    } );
}

/* Converter functions*/
function encodeValues(observedValue){
    return observedValue
}

function missingValuesQuerisToJSON(missingValueIndices){
    q = missingValueIndices
    return {"q":q}
}

function findNaNValuesInCSV(csvFile){
    var arrayDataSet = $.csv.toArrays(csvFile);
    var numCols = arrayDataSet[0].length - 1 
    var numRows = arrayDataSet.length - 1
    var missingValsList = []
    for (var i = 1; i < numRows + 1; i++){
	for (var j = 0; j < numCols; j++){
	    if (arrayDataSet[i][j] == "NaN"){
		missingValsList.push([i -1 , j - 1])
	    }
	    
	}
    }
    return missingValsList
}

function set_menu_option(option_value) {
    jQuery(document.getElementById('menu')).append(
	"<option value='" + option_value + "'>" + 
	    option_value + "</option>"
    )
}

function JSONRPC_send_method(method_name, parameters, function_to_call) {
    window.jsonrpc_id += 1
    data_in =
	{ id : toString(window.jsonrpc_id),
	  jsonrpc : "2.0",
	  method : method_name,
	  params : parameters }
    $.ajax({
	url: window.JSONRPC_URL, 
	type: 'PUT', 
	data: JSON.stringify(data_in),
	dataType: 'json', 
	async: true,	
	crossDomain: true,
	success:function(data) {
	    function_to_call(data)
	},
	error: function(httpRequest, textStatus, errorThrown) {
	    console.log(httpRequest)
	    console.log(textStatus)
	    console.log(errorThrown)
	    alert("Sorry, some error has happened.")
	},
	complete: function() {
	    //console.log('COMPLETE');
	}		
    });			    
}

function call_and_save(method_name, parameters) {
    callback_func = function(returnedData) {
	window.last_function_return = returnedData
	console.log(returnedData)
    }
    JSONRPC_send_method(method_name, parameters, callback_func)
}

function to_upper_case(str) {
    return str.toUpperCase()
}

function get_idx_of(term, str_array) {
    return $.inArray(term.toUpperCase(), str_array.map(to_upper_case))
}

function in_list(term, str_array) {
    return get_idx_of(term, str_array) != -1
}

function get_idx_after(term, str_array) {
    return get_idx_of(term, str_array) + 1
}

function get_el_after(term, str_array) {
    // case insensitive
    idx = get_idx_after(term, str_array)
{'fraction_non_alphanumeric': 0.13819691577698695, 'fraction_numerical': 0.009252669039145907, 'mean_word_length': 4.125227963525836, 'pattern_counts': {'":': 20, '<': 17, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '490295', 'n_tokens_mistral': 3047, 'n_tokens_neox': 2959, 'n_words': 550}