Skip to content

Instantly share code, notes, and snippets.

@dmesquita
Last active May 24, 2018 13:36
Show Gist options
  • Save dmesquita/d0d441928df363fedf7f9d9e44153e9c to your computer and use it in GitHub Desktop.
Save dmesquita/d0d441928df363fedf7f9d9e44153e9c to your computer and use it in GitHub Desktop.
Google Sheets script that reads from two other spreadsheets and creates a new one based on their data
function evaluateQueryResults(){
var documentsSheetID = ...;
var queryDefaultConfigSheetID = ...;
var queryStopWordConfigSheetID = ...;
var queryStemmingConfigSheetID = ...;
var queryCompleteConfigSheetID = ...;
Logger.log("default");
Logger.log(createEvaluationMatrix(documentsSheetID, queryDefaultConfigSheetID));
Logger.log("stopword");
newSheet("is there a new app - StopWordConfig");
Logger.log(createEvaluationMatrix(documentsSheetID, queryStopWordConfigSheetID));
Logger.log("stemming");
newSheet("is there a new app - StemmingConfig");
Logger.log(createEvaluationMatrix(documentsSheetID, queryStemmingConfigSheetID));
Logger.log("complete");
newSheet("is there a new app - CompleteConfig");
Logger.log(createEvaluationMatrix(documentsSheetID, queryCompleteConfigSheetID));
}
function createEvaluationMatrix(documentsSheetID, resultsSheetID) {
sheet = SpreadsheetApp.getActiveSheet();
var documentsSheet = SpreadsheetApp.openById(documentsSheetID);
var resultsSheet = SpreadsheetApp.openById(resultsSheetID);
var relevanceOfDocsFromSpreadsheet = documentsSheet.getRange('B2:B201').getValues();
var resultDocumentsFromSpreadsheet = resultsSheet.getRange('A2:A'+resultsSheet.getLastRow()).getValues();
//For some reason that I still don`t know the indexOf() function doesn`t work with the array from the spreadsheet, so I`m creating a new one
var relevanceOfDocs = arrayToArrayOfStrings(relevanceOfDocsFromSpreadsheet);
var resultDocuments = arrayToArrayOfStrings(resultDocumentsFromSpreadsheet);
var measures = {accuracy:-1, coverage:-1, fmeasure:-1};
var qtdRelevantRetrievedDocs = 0;
var qtdRelevantDocsMissing = 0;
var totalRetrievedDocs = resultDocuments.length;
for (documentID = 1; documentID < 201; documentID++){
sheet.getRange('A'+documentID).setValue(documentID);
if (isRelevant(documentID) && documentsBelongsToResults(documentID) ){
sheet.getRange('B'+documentID).setValue("certo");
qtdRelevantRetrievedDocs = qtdRelevantRetrievedDocs + 1;
}else if (isRelevant(documentID) && !documentsBelongsToResults(documentID)){
sheet.getRange('B'+documentID).setValue("errado");
qtdRelevantDocsMissing = qtdRelevantDocsMissing + 1;
}else if (!isRelevant(documentID) && !documentsBelongsToResults(documentID)){
sheet.getRange('B'+documentID).setValue("certo")
}else{
sheet.getRange('B'+documentID).setValue("errado");
}
}
measures.accuracy = qtdRelevantRetrievedDocs/totalRetrievedDocs;
measures.coverage = qtdRelevantRetrievedDocs/(qtdRelevantRetrievedDocs + qtdRelevantDocsMissing);
measures.fmeasure = (2*measures.accuracy*measures.coverage)/(measures.accuracy+measures.coverage);
return measures;
function arrayToArrayOfStrings(input){
var stringConcatenated = input.toString();
return stringConcatenated.split(',');
}
function documentsBelongsToResults(documentID){
return !(resultDocuments.indexOf(documentID.toString()) == -1);
}
function isRelevant(documentID){
return (relevanceOfDocs[documentID-1] == 1);
}
}
function newSheet(sheetName) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var templateSheet = ss.getActiveSheet();
ss.insertSheet(sheetName, 0, {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment