Python Annoy + GloVe example


pip3 install annoy

Download from:


from annoy import AnnoyIndex
file = "/Users/gary/Downloads/glove.6B/glove.6B.50d.txt"
content = []
with open(file) as f:
    content = f.readlines()
content = [x.split(" ") for x in content]
t = AnnoyIndex(50) 
idx = 0
terms = [i[0] for i in content]
for i in content:
  vec = [float(a) for a in i[1:]]
  t.add_item(idx, vec)
  idx = idx + 1 # 10 trees'test.ann')


u = AnnoyIndex(50)
near = u.get_nns_by_item(0, 10) # nearest 10 terms
nearWords = [terms[i] for i in near]

Interested in Python? I send out weekly, personalized emails with articles and conference talks. Click here to see an example and subscribe.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *