def index(self, metric='cosine'):
""" Build a nearest neighbor retrieval index to perform similarity
lookups and analogies
Arguments:
metric: string, or sklearn compatible callable
Returns:
self
Raises:
TokenContainerException if no pretrained vectors have been loaded
"""
if self.W is not None:
alg = 'brute' if (metric == 'cosine') else 'auto'
from sklearn.neighbors import NearestNeighbors
self._nn = NearestNeighbors(metric=metric, algorithm=alg)
self._nn.fit(self.W)
else:
raise TokenContainerException(
'cannot build similarity on vectorless structure'
)
return self
评论列表
文章目录