'Returns a list of (similarity, document)-tuples in the model,
sorted by cosine similarity to the given document.'

def nearest_neighbors(self, document, top=10):

    v = ((self.cosine_similarity(document, d), d) for d in self.documents)
    v = [(w, d) for (w, d) in v if ((w > 0) and (d.id != document.id))]
    v = heapq.nsmallest(top, v, key=(lambda v: ((- v[0]), v[1])))
    return v
