1 2 3 4 5 6 7 8 9 10 11 12 13 14
/** * Adds a custom menu to the active spreadsheet, containing a single menu item * for invoking the getLanguages() function specified above. * The onOpen() function, when defined, is automatically invoked whenever the * spreadsheet is opened. */ function onOpen() { var sheet = SpreadsheetApp.getActiveSpreadsheet(); var menuEntries = [{ name : "Get languages", functionName: "getLanguages" }]; sheet.addMenu("Predictions API", menuEntries); }
getLanguages()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
function getLanguages() { var sheet = SpreadsheetApp.getActiveSheet(); var sentencesRange = sheet.getRange("A:A"); var languagesRange = sheet.getRange("B:B"); var sentencesRows = sentencesRange.getNumRows(); var sentencesValues = sentencesRange.getValues(); for (var i = 0; i <= sentencesRows - 1; i++) { //leftmost cell of the row, but the row has only one cell var sentence = sentencesValues[i][0]; if (!sentence || 0 === sentence.length) continue; Logger.log(" Analizing sentence " + i + ": " + sentence); var languageSample = Prediction.Hostedmodels.newPredictInput() .setCsvInstance([sentence]); var result = Prediction.Hostedmodels .predict("sample.languageid", {"input": languageSample}); Logger.log(" Detected language: " + result.getOutputLabel()); //remeber, getCell starts at 1:1, not 0:0 languagesRange.getCell(i+1, 1).setValue(result.getOutputLabel()); } }
sentence
Prediction.Hostedmodels.newPredictInput().setCsvInstance()
PredictionInput
Prediction.Hostedmodels.predict()
sample.languageid
languageSample
{ "id":"sample.languageid", "outputLabel":"English", "outputMulti": [{"score":0.880082, "label":"English"}, {"score":0.114125, "label":"Spanish"}, {"score":0.005793, "label":"French"}], "selfLink":"https://www.googleapis.com/prediction/v1.2/hostedmodels/%s/predict", "kind":"prediction#output" }
result.getOutputLabel()
outputMulti
sample.sentiment