Skip to content

Instantly share code, notes, and snippets.

@alicemaz
Last active August 29, 2015 14:22
Show Gist options
  • Save alicemaz/455b1a7d46392f56fe1a to your computer and use it in GitHub Desktop.
Save alicemaz/455b1a7d46392f56fe1a to your computer and use it in GitHub Desktop.
Botmistress bot/human classifier
module.exports = function(input) {
var net = {"layers":[{"mtpd":{},"dptd":{},"msec":{},"dsec":{},"mrt":{},"drt":{}},{"0":{"bias":-3.6984439021091666,"weights":{"mtpd":-6.173024933059706,"dptd":10.997739857335477,"msec":2.7645625300070646,"dsec":2.815832326064508,"mrt":18.0465877829903,"drt":1.155631417743205}},"1":{"bias":2.8315295205493665,"weights":{"mtpd":5.2303592356466035,"dptd":-8.2729870958148,"msec":-2.3778963665239683,"dsec":-2.1532312095623416,"mrt":-14.328315122259214,"drt":-1.0412519208946147}},"2":{"bias":2.715125313888669,"weights":{"mtpd":4.855944732546972,"dptd":-7.991354496280592,"msec":-2.1930872312980267,"dsec":-2.1223653263694544,"mrt":-13.689745670932748,"drt":-0.9648212850166319}}},{"bot":{"bias":-2.2122390536076995,"weights":{"0":-15.7186161148084,"1":11.480607717164341,"2":10.947352405515204}},"human":{"bias":2.1200842597557106,"weights":{"0":15.801011054157996,"1":-11.430970519356372,"2":-10.892672803240483}}}],"outputLookup":true,"inputLookup":true};
for (var i = 1; i < net.layers.length; i++) {
var layer = net.layers[i];
var output = {};
for (var id in layer) {
var node = layer[id];
var sum = node.bias;
for (var iid in node.weights) {
sum += node.weights[iid] * input[iid];
}
output[id] = (1 / (1 + Math.exp(-sum)));
}
input = output;
}
return output;
}
"use strict";
const path = require("path");
const _ = require("underscore");
const ζ = require(path.join(__dirname,"..","zeta.js"));
ζ();
//normalize then average
const mean = x => {
return _.chain(x)
.norm()
.core()
.value();
};
//normalize then get standard deviation
const dev = x => {
const m = mean(x);
return Math.sqrt(
_.chain(x)
.norm()
.map(val => Math.pow(val-m,2))
.core()
.value()
);
};
const nani = x => {
return _.isNaN(x) ? 0 : x;
};
//function that takes a pulldown array and returns
const doStuff = raw => {
//obj of arrays of timestamps grouped by date, prolly use for other stuff later
const datemap = _.chain(raw)
.reject(val => _.has(val,"retweeted_status"))
.map(val => new Date(val.created_at).toISOString())
.groupBy(val => val.slice(0,10))
.value();
const rtMap = _.chain(raw)
.filter(val => _.has(val,"retweeted_status"))
.map(val => new Date(val.created_at).toISOString())
.groupBy(val => val.slice(0,10))
.value();
const tweetsPerDay = _.chain(datemap)
.values()
.map(val => val.length)
.value();
const secs = _.chain(datemap)
.values()
.flatten()
.map(val => val.slice(17,19))
.value();
const rtsPD = _.chain(rtMap)
.values()
.map(val => val.length)
.value();
const result = {
//normed avg of tweets per day
mtpd: nani(mean(tweetsPerDay)),
dptd: nani(dev(tweetsPerDay)),
//" of timestamp second values
msec: nani(mean(secs)),
dsec: nani(dev(secs)),
//" of rts per day
mrt: nani(mean(rtsPD)),
drt: nani(dev(rtsPD))
};
console.log("inside acctclass:\n",JSON.stringify(result,null,"\t"));
return result;
};
//hacky bullshit but here's an export that can analyze based on this model
module.exports = pulleddown => {return {input: doStuff(pulleddown) }};
//it me alice yr fav gay tran
//I guess it's as good a time as any to start my "include this fucking everywhere" util file
"use strict";
const _ = require("underscore");
const Zeta = function() {
_.mixin({
//given an array of reals, normalize its values to within unity
norm: arr => {
const min = _.min(arr);
const max = _.max(arr);
return _.map(arr, val => (val-min)/(max-min));
},
//simple mean
core: arr => _.reduce(arr, (m,n) => m+n, 0)/arr.length
});
};
module.exports = Zeta;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment