-
Notifications
You must be signed in to change notification settings - Fork 15
/
similarity.py
32 lines (25 loc) · 875 Bytes
/
similarity.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
#!/usr/bin/env python
"""Output similarity between words in input."""
import sys
import numpy
import wvlib
from common import process_args, query_loop
def process_query(wv, query, options=None):
vectors = [wv.words_to_vector(q) for q in query]
words = [w for q in query for w in q]
assert len(vectors) == 2, 'internal error'
vectors = [v/numpy.linalg.norm(v) for v in vectors]
print numpy.dot(vectors[0], vectors[1])
def main(argv=None):
if argv is None:
argv = sys.argv
# TODO: remove irrelevant options
options = process_args(argv[1:])
try:
wv = wvlib.load(options.vectors, max_rank=options.max_rank)
except Exception, e:
print >> sys.stderr, 'Error: %s' % str(e)
return 1
return query_loop(wv, options, process_query, query_count=2)
if __name__ == '__main__':
sys.exit(main(sys.argv))