MCPcopy Index your code
hub / github.com/clips/pattern / nearest_neighbors

Method nearest_neighbors

pattern/vector/__init__.py:1047–1056  ·  view source on GitHub ↗

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

(self, document, top=10)

Source from the content-addressed store, hash-verified

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

Callers 2

vector_space_searchMethod · 0.95
06-commonsense.pyFile · 0.45

Calls 1

cosine_similarityMethod · 0.95

Tested by

no test coverage detected