Skip to content

Instantly share code, notes, and snippets.

@funador
Created September 28, 2017 23:15
Show Gist options
  • Save funador/4786ce1c3bec1fc1d986401dc2082fd9 to your computer and use it in GitHub Desktop.
Save funador/4786ce1c3bec1fc1d986401dc2082fd9 to your computer and use it in GitHub Desktop.
// take the last 100 tweets from the user
{
accountAge: 4,
mins: 2880, // number of mins to get to 100 tweets
frequentLen: 4, // When they retweet the same account more than 4 times per hundred ++
totalFreq: 81, // Total count of above
convos: 3, // number of tweets directed at another user
listed_count: 23, // how often they are listed
numOriginalTweets: 9, // how many of the 100 are created by the tweeter
sourcesLen: 3 // how many different platforms (iOS, android, web) they tweeted from
}
const bayes = require('node-bayes')
const { firebase } = require('./config')
const _ = require('underscore')
const TRAINING_COLUMNS = ['accountAge', 'mins', 'frequentLen', 'totalFreq', 'created',
'convos', 'listed_count', 'numOriginalTweets', 'sourcesLen']
let cls
const setup = async () => {
let ham, spam
await firebase
.ref('ham')
.once('value')
.then(snap => ham = snap.val())
await firebase
.ref('spam')
.once('value')
.then(snap => spam = snap.val())
const spamArr = _.shuffle(Object.keys(spam))
.slice(0, 50)
.map(key => spam[key])
const hamArr = Object.keys(ham).map(key => ham[key])
const buildArray = (arr, tag) => {
return arr.map(obj => {
delete obj.created
return [...Object.values(obj), tag]
})
}
const TRAINING_DATA = [...buildArray(hamArr, 'ham'), ...buildArray(spamArr, 'spam')]
cls = new bayes.NaiveBayes({
columns: TRAINING_COLUMNS,
data: TRAINING_DATA,
verbose: true
})
cls.train()
}
setup()
exports.twitterAlgo = (ranker, screen_name) => {
const answer = cls.predict(Object.values(ranker))
console.log(answer, screen_name)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment