def find_nearest_neighbor(query, vectors, ban_set, cossims=None):
if cossims is None:<EOL><INDENT>cossims = np.matmul(vectors, query, out=cossims)<EOL><DEDENT>else:<EOL><INDENT>np.matmul(vectors, query, out=cossims)<EOL><DEDENT>rank = len(cossims) - <NUM_LIT:1><EOL>result_i = np.argpartition(cossims, rank)[rank]<EOL>while result_i in ban_set:<EOL><INDENT>rank -= <NUM_LIT:1><EOL>result_i = np.argpartition(cossims, rank)[rank]<EOL><DEDENT>return result_i<EOL>
query is a 1d numpy array corresponding to the vector to which you want to
find the closest vector
vectors is a 2d numpy array corresponding to the vectors you want to consider
ban_set is a set of indicies within vectors you want to ignore for nearest match
cossims is a 1d numpy array of size len(vectors), which can be passed for efficiency

returns the index of the closest match to query within vectors