Skip to content

Instantly share code, notes, and snippets.

@okram
Created July 30, 2012 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okram/3210615 to your computer and use it in GitHub Desktop.
Save okram/3210615 to your computer and use it in GitHub Desktop.
arguments to pass in:
twitterId
traversals
numRecommendations
///////////////////////////
// this function will return an ordered collection
// one thing to optimize is to return the map, not the set of vertices (this is more information and faster)
// to do so, "return [:]" and no ".keySet()" on last line.
// finally this only works with Gremlin 2.1.0-SNAPSHOT as this has limit() added which was not in Gremlin 2.0.0. See:
// gremlin> g.v(1).outE('knows')[0..30].inV.toString()
// ==>[StartPipe, QueryPipe(out,[knows],has:false,interval:false,limit:31,edge), IdentityPipe, InVertexPipe]
def random = new Random()
followers = g.V('twitterId',twitterId).out('follows').toList()
if(followers.isEmpty()) return []
m = [:]
(0..traversals).each {
followers[random.nextInt(followers.size)].outE('follows')[0..29].inV.groupCount(m).iterate()
}
return m.sort{a,b -> b.value <=> a.value}[0..numRecommendations].keySet()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment