Skip to content

Commit

Permalink
add README_Lua
Browse files Browse the repository at this point in the history
  • Loading branch information
starius committed May 1, 2016
1 parent a6f229a commit 8a89b7e
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ More info
* `Dirk Eddelbuettel <http://dirk.eddelbuettel.com/>`__ provides an `R version of Annoy <http://dirk.eddelbuettel.com/code/rcpp.annoy.html>`__.
* `Andy Sloane <http://www.a1k0n.net/>`__ provides a `Java version of Annoy <https://github.com/spotify/annoy-java>`__ although currently limited to cosine and read-only.
* There is `experimental support for Go <https://github.com/spotify/annoy/blob/master/README_GO.rst>`__ provided by Taneli Leppä.
* Boris Nagaev wrote `Lua bindings <https://github.com/spotify/annoy/blob/master/README_Lua.md>`__.
* `Presentation from New York Machine Learning meetup <http://www.slideshare.net/erikbern/approximate-nearest-neighbor-methods-and-vector-models-nyc-ml-meetup>`__ about Annoy
* Radim Řehůřek's blog posts comparing Annoy to a couple of other similar Python libraries: `Intro <http://radimrehurek.com/2013/11/performance-shootout-of-nearest-neighbours-intro/>`__, `Contestants <http://radimrehurek.com/2013/12/performance-shootout-of-nearest-neighbours-contestants/>`__, `Querying <http://radimrehurek.com/2014/01/performance-shootout-of-nearest-neighbours-querying/>`__
* `ann-benchmarks <https://github.com/erikbern/ann-benchmarks>`__ is a benchmark for several approximate nearest neighbor libraries. Annoy seems to be fairly competitive, especially at higher precisions:
Expand Down
101 changes: 101 additions & 0 deletions README_Lua.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
Install
-------

To install, you'll need Lua (binary + library) and LuaRocks.

If you have Python and Pip, you can get Lua and LuaRocks
using [hererocks](https://github.com/mpeterv/hererocks/),
written by Peter Melnichenko.

```
pip install hererocks
hererocks here --lua 5.1 --luarocks 2.2
```

This command installs Lua and LuaRocks locally to directory `here`.
To activate it, add `here/bin` to `PATH`:

```
export PATH="$(pwd)/here/bin/:$PATH"
```

Then you can use commands `lua`, `luarocks`,
and tools installed by `luarocks`.

To build and install `annoy`, type:

```
luarocks make
```

Background
----------

See the main README.

Lua code example
----------------

```lua
local annoy = require "annoy"

local f = 3
local t = annoy.AnnoyIndex(f) -- Length of item vector that will be indexed
for i = 0, 999 do
local v = {math.random(), math.random(), math.random()}
t:add_item(i, v)
end

t:build(10) -- 10 trees
t:save('test.ann')

-- ...

local u = annoy.AnnoyIndex(f)
u:load('test.ann') -- super fast, will just mmap the file

-- find the 10 nearest neighbors
local neighbors = u:get_nns_by_item(0, 10)
for rank, i in ipairs(neighbors) do
print("neighbor", rank, "is", i)
end
```

Full Lua API
------------

Lua API closely resembles Python API, see main README.


Tests
-------

File `test/annoy_test.lua` is the literal translation of
`test/annoy_test.py` from Python+Nosetests to Lua+Busted.

To run tests, you need [Busted](http://olivinelabs.com/busted/),
Elegant Lua unit testing. To install it, type:

```
luarocks install busted
```

To run tests, type:

```
busted test/annoy_test.lua
```

It will take few minutes to execute.

Discuss
-------

There might be some memory leaks if inputs are incorrect.
Some functions allocate stack objects calling Lua functions throwing
Lua errors (e.g., `luaL_checkinteger`). A Lua error may omit calling
C++ destructors when unwinding the stack. (If it does, depends on
the Lua implementation and platform being in use.)

Lua binding was written by Boris Nagaev.
You can contact me via email (see https://github.com/starius).

0 comments on commit 8a89b7e

Please sign in to comment.