Returns a list of (similarity, document)-tuples in the model, sorted by cosine similarity to the given document.
(self, document, top=10)
| 1045 | similarity = cosine_similarity |
| 1046 | |
| 1047 | def nearest_neighbors(self, document, top=10): |
| 1048 | """ Returns a list of (similarity, document)-tuples in the model, |
| 1049 | sorted by cosine similarity to the given document. |
| 1050 | """ |
| 1051 | v = ((self.cosine_similarity(document, d), d) for d in self.documents) |
| 1052 | # Filter the input document from the matches. |
| 1053 | # Filter documents that score zero, and return the top. |
| 1054 | v = [(w, d) for w, d in v if w > 0 and d.id != document.id] |
| 1055 | v = heapq.nsmallest(top, v, key=lambda v: (-v[0],v[1])) |
| 1056 | return v |
| 1057 | |
| 1058 | similar = related = neighbors = nn = nearest_neighbors |
| 1059 |
no test coverage detected