Skip to content

Instantly share code, notes, and snippets.

@jessehattabaugh
Created August 14, 2008 21:00
Show Gist options
  • Save jessehattabaugh/5488 to your computer and use it in GitHub Desktop.
Save jessehattabaugh/5488 to your computer and use it in GitHub Desktop.
function cron_analyze_users(){
var subject = storage.users.sort(lastAnalyzed).first()
var interests = []
storage.interests.filter({user:subject,relevant:true}).forEach(function(i){
interests[i.term.string] = i.difference
})
var analyzed = []
storage.relations.filter({subject:subject}).forEach(function(r){
analyzed[r.object.id] = r.analyzed
})
storage.users.sort(function(a,b){
if(!analyzed[a.id] && analyzed[b.id]) return 1
else if(analyzed[a.id] && !analyzed[b.id]) return -1
else if(analyzed[a.id] && analyzed[b.id]) return analyzed[a.id] - analyzed[b.id]
else return a.created - b.created
}).limit(20).forEach(function(object){
if(object.email != subject.email){
relation = storage.relations.filter({subject:subject, object:object}).first()
if(!relation) relation = storage.relations.add({subject:subject, object:object})
conrelation = storage.relations.filter({subject:object, object:subject}).first()
if(!conrelation) conrelation = storage.relations.add({subject:object, object:subject})
relation.euclidean = euclidean(subject, object, interests)
conrelation.euclidean = relation.euclidean
relation.analyzed = new Date()
conrelation.analyzed = new Date()
}
})
subject.analyzed = new Date()
}
function euclidean(subject, object, interests){
sum = 0
storage.interests.filter({user:object,relevant:true}).forEach(function(i){
if(interests[i.term.string]){
sum += Math.pow(interests[i.term.string] - i.difference, 2)
}
})
if(sum) return 1/(1+Math.sqrt(sum))
else return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment