npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

annoy

v4.0.0

Published

Node bindings for Annoy, an efficient Approximate Nearest Neighbors implementation written in C++.

Downloads

223

Readme

annoy-node

Build Status

Node bindings for Annoy, an efficient Approximate Nearest Neighbors implementation written in C++.

Version 4.0.0 requires Node 14 or Node 16 and does not yet work on 18.

Status: Tests pass, including one that loads 3 million vectors, but API coverage is not complete. Run on OS X and Linux with Node 8, 10, and 12. Not tried on Windows yet. Support for Node 6.3 and 4.6 ended at version 2.0.1 of this package.

All of the Python API methods are implemented. The names are camel cased, JavaScript-style.

  • addItem
  • build
  • save
  • load
  • unload
  • getItem
  • getNNsByVector
  • getNNsByItem
  • getNItems
  • getDistance

There are a few minor differences in behavior:

  • If you set the "include distances" param (the fourth param) when calling getNNsByVector and getNNsByItem, rather than returning a 2D array containing the neighbors and distances, it will return an object with the properties neighbors and distances, each of which is an array.
  • get_item_vector in with the Python API is just called getItem here.

Installation

On Linux, if you don't already have Python 2.7 and g++ 4.8, you need to install them. Here's how you do it on Ubuntu:

(sudo) apt-get install python2.7
(sudo) apt-get install g++-4.8
npm config set python /path/to/executable/python2.7

Then, symlink g++ somewhere it can be found:

ln -s /usr/bin/g++-4.8 /usr/local/bin/g++

On OS X, they should already be there.

Then:

npm install annoy

Usage

var Annoy = require('annoy');
var annoyIndex1 = new Annoy(10, 'Angular');

annoyIndex1.addItem(0, [-5.0, -4.5, -3.2, -2.8, -2.1, -1.5, -0.34, 0, 3.7, 6]);
annoyIndex1.addItem(1, [5.0, 4.5, 3.2, 2.8, 2.1, 1.5, 0.34, 0, -3.7, -6]);
annoyIndex1.addItem(2, [0, 0, 0, 0, 0, -1, -1, -0.2, 0.1, 0.8]);
annoyIndex1.build();
annoyIndex1.save(annoyPath);

read();

function read() {
  var annoyIndex2 = new Annoy(10, 'Angular');

  if (annoyIndex2.load(annoyPath)) {
    var v1 = annoyIndex2.getItem(0);
    var v2 = annoyIndex2.getItem(1);
    console.log('Gotten vectors:', v1, v2);

      for (var i = 0; i < v1.length; ++i) {
        sum.push(v1[i] + v2[i]);
      }

      var neighbors = annoyIndex2.getNNsByVector(sum, 10, -1, false);
      console.log('Nearest neighbors to sum', neighbors);

      var neighborsAndDistances = annoyIndex2.getNNsByVector(sum, 10, -1, true);
      console.log('Nearest neighbors to sum with distances', neighborsAndDistances);
  }
}

Development

npm install -g node-gyp
node-gyp rebuild

Run eslint . and make prettier before committing.

Tests

Run tests with make test.

You can also run tests individually:

  • This is a short baseline test: node tests/smalltest.js
  • This is a test that uses 70K 200-dimension vectors: node tests/basictests.js

There is also a big-test target that is not a dependency of the test target. It loads about 3 million 300-dimension vectors. It takes about six minutes to run on good-for-2016 hardware. Before you can run it, you need to download GoogleNews-vectors-negative300.bin to tests/data.

Then, you can run make tests/data/GoogleNews-vectors-negative300.json, which takes a while, and gets the test data ready for the big test. (See comment about running that in the Makefile.) Then, make big-test.

Contributors

Thanks to:

License

The MIT License (MIT)

Copyright (c) 2016 Jim Kang

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.