Skip to content

Instantly share code, notes, and snippets.

View ismaelc's full-sized avatar

Chris Ismael ismaelc

View GitHub Profile
function trainModel(data) {
classifier = new Classifier({
nGramMin: 1,
nGramMax: 3,
});
for (let key in data) {
classifier.train(data[key], key);
}
}
function predictRange() {
// Get saved data and train it
let processed = this.getTrainData()
this.trainModel(processed)
// Get selected range and data
let sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
let resolved = this.resolveRange(sheet)
let range = sheet.getRange(resolved)
function setTrainData(trainData) {
PropertiesService.getScriptProperties().setProperty('trainData', JSON.stringify(trainData));
}
@ismaelc
ismaelc / capture.js
Created February 2, 2021 04:00
Capture cells from Sheets
// Copy range data for training/prediction later
function captureRange(e) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
let range = this.resolveRange(sheet)
var data = sheet.getRange(range).getValues();
let processed = this.processData(data)
this.setTrainData(processed)
Browser.msgBox('Captured training set!')
}
@ismaelc
ismaelc / menu.js
Created February 1, 2021 20:29
Adding menu items in Apps Script
// Add menu items
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('ML Train Menu')
.addItem('Get training set', 'captureRange')
.addItem('Predict', 'predictRange')
.addToUi();
}
@ismaelc
ismaelc / import.js
Created February 1, 2021 17:40
Importing libraries to Apps Script
// Import ml-classify-text
eval(UrlFetchApp.fetch('https://cdn.jsdelivr.net/npm/ml-classify-text@2.0.0/lib/index.js').getContentText());
let classifier = new Classifier()
@ismaelc
ismaelc / sentiment.js
Last active February 5, 2021 19:33
ml-classify-text-js-sample
let positive = [
'This is great, so cool!',
'Wow, I love it!',
'It really is amazing',
]
let negative = [
'This is really bad',
'I hate it with a passion',
'Just terrible!',
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
>>> from pyteaser import SummarizeUrl
>>> url = 'http://www.huffingtonpost.com/2013/11/22/twitter-forward-secrecy_n_4326599.html'
>>> summaries = SummarizeUrl(url)
>>> print summaries
Output
[
"Twitter's move is the latest response from U.S. Internet firms following disclosures by former spy agency contractor Edward Snowden about widespread, classified U.S. government surveillance programs.",
"Since then, it has become clearer and clearer how important that step was to protecting our users' privacy.",
"The online messaging service, which began scrambling communications in 2011 using traditional HTTPS encryption, said on Friday it has added an advanced layer of protection for HTTPS known as 'forward secrecy'",
functions:
summarize:
handler: handler.summarize
events:
- http:
path: summarize
method: post