Skip to content

Instantly share code, notes, and snippets.

@jessehattabaugh
Created October 21, 2008 17:25
Show Gist options
  • Save jessehattabaugh/18357 to your computer and use it in GitHub Desktop.
Save jessehattabaugh/18357 to your computer and use it in GitHub Desktop.
function cron_analyze_users(){
var subject = storage.users.sortBy('analyzed').first()
print(subject.name)
if(!subject.interests)
subject.relevant = false
else {
storage.users.filter({relevant:true}).sortBy('active').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})
noitaler = storage.relations.filter({subject:object, object:subject}).first()
if(!noitaler) noitaler = storage.relations.add({subject:object, object:subject})
relation.score = euclidean(subject, object)
noitaler.score = relation.score
print(DIV('score for '+subject.name+' and '+object.name+' is '+relation.score))
relation.analyzed = new Date()
noitaler.analyzed = new Date()
}
})
}
subject.analyzed = new Date()
response.redirect('analyze_users')
}
function euclidean(subject, object){
var sum = 0
storage.interests.filter({user:object,relevant:true}).forEach(function(i){
if(userIndex = storage.interestsIndex[subject.id]){
if(interest = userIndex[i.term.string]) {
if(interest.relevant) {
difference = interest.score - i.score
square = Math.pow(difference, 2)
if(typeof square != 'number') square = 0
sum = sum + square
}
}
}
})
if(typeof sum != 'number') return 0
else return 1/(1+Math.sqrt(sum))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment