def nn(model, text, vectors, query, k=5):
 qf = encode(model, [query])
 qf /= norm(qf)
 scores = numpy.dot(qf, vectors.T).flatten()
 sorted_args = numpy.argsort(scores)[::(-1)]
 sentences = [text[a] for a in sorted_args[:k]]
 print ('QUERY: ' + query)
 print 'NEAREST: '
 for (i, s) in enumerate(sentences):
  print s, sorted_args[i]

def nn ( model , text , vectors , query , k = 5 ) : qf = encode ( model , [ query ] ) qf /= norm ( qf ) scores = numpy . dot ( qf , vectors . T ) . flatten ( ) sorted_args = numpy . argsort ( scores ) [ : : ( - 1 ) ] sentences = [ text [ a ] for a in sorted_args [ : k ] ] print ( 'QUERY: ' + query ) print 'NEAREST: ' for ( i , s ) in enumerate ( sentences ) : print s , sorted_args [ i ]
return the nearest neighbour sentences to query text: list of sentences vectors: the corresponding representations for text query: a string to search .