-
Notifications
You must be signed in to change notification settings - Fork 15
/
nearest.py
executable file
·42 lines (34 loc) · 1.24 KB
/
nearest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
"""Find nearest neighbors to input word(s) in given word representation.
This is a python + wvlib version of distance.c from word2vec
(https://code.google.com/p/word2vec/). The primary differences to
distance.c are support for additional word vector formats, increased
configurability, and reduced speed.
"""
import sys
import os
import wvlib
from common import process_args, query_loop, output_nearest
def process_query(wv, query, options=None):
words = [w for q in query for w in q]
vector = wv.words_to_vector(words)
nncount = options.number if options else 10
if options is None or not options.approximate:
nearest = wv.nearest(vector, n=nncount, exclude=words)
else:
nearest = wv.approximate_nearest(vector, n=nncount, exclude=words)
output_nearest(nearest, options)
return True
def main(argv=None):
if argv is None:
argv = sys.argv
options = process_args(argv[1:])
try:
wv = wvlib.load(options.vectors, max_rank=options.max_rank)
wv = wv.normalize()
except Exception as e:
print('Error: %s' % str(e), file=sys.stderr)
return 1
return query_loop(wv, options, process_query)
if __name__ == '__main__':
sys.exit(main(sys.argv))