### Install Annoy Go Bindings Source: https://github.com/spotify/annoy/blob/main/README_GO.rst Steps to install the Annoy Go bindings using SWIG and Go modules. Ensure Swig is installed and tested with the specified version. ```bash swig -go -intgosize 64 -cgo -c++ src/annoygomodule.i mkdir -p $(go env GOPATH)/src/annoy cp src/annoygomodule_wrap.cxx src/annoy.go src/annoygomodule.h src/annoylib.h src/kissrandom.h test/annoy_test.go $(go env GOPATH)/src/annoy cd $(go env GOPATH)/src/annoy go mod init github.com/spotify/annoy go mod tidy go test ``` -------------------------------- ### Install hererocks for Lua and LuaRocks Source: https://github.com/spotify/annoy/blob/main/README_Lua.md Use pip to install hererocks, then use hererocks to install Lua and LuaRocks locally. ```bash pip install hererocks hererocks here --lua 5.1 --luarocks 2.2 ``` -------------------------------- ### Build and Install ANNoy with LuaRocks Source: https://github.com/spotify/annoy/blob/main/README_Lua.md Use luarocks to build and install the ANNoy library. ```bash luarocks make ``` -------------------------------- ### Install Busted for Lua Unit Testing Source: https://github.com/spotify/annoy/blob/main/README_Lua.md Install the Busted testing framework using LuaRocks. ```bash luarocks install busted ``` -------------------------------- ### Annoy Go Example: Index Creation and Query Source: https://github.com/spotify/annoy/blob/main/README_GO.rst Demonstrates creating an Annoy index, adding items, building the index, saving it to disk, loading it back, and performing nearest neighbor searches. Note that item identifiers are integers and memory is allocated for max(id)+1 items. ```go package main import ( "fmt" "math/rand" "github.com/spotify/annoy" ) func main() { f := 40 t := annoy.NewAnnoyIndexAngular(f) for i := 0; i < 1000; i++ { item := make([]float32, 0, f) for x:= 0; x < f; x++ { item = append(item, rand.Float32()) } t.AddItem(i, item) } t.Build(10) t.Save("test.ann") annoy.DeleteAnnoyIndexAngular(t) t = annoy.NewAnnoyIndexAngular(f) t.Load("test.ann") result := annoyindex.NewAnnoyVectorInt() defer result.Free() t.GetNnsByItem(0, 1000, -1, result) fmt.Printf("%v\n", result.ToSlice()) } ``` -------------------------------- ### Basic ANNoy Lua Usage Source: https://github.com/spotify/annoy/blob/main/README_Lua.md Example demonstrating how to create an Annoy index, add items, build the index, save it, load it, and perform nearest neighbor searches. ```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 ``` -------------------------------- ### Checkout Master Branch Source: https://github.com/spotify/annoy/blob/main/RELEASE.md Ensure you are on the master branch and have the latest changes before starting the release process. ```bash git checkout master && git fetch && git reset --hard origin/master ``` -------------------------------- ### Get Number of Trees Source: https://github.com/spotify/annoy/blob/main/README.rst Returns the number of trees that have been built for the Annoy index. ```APIDOC ## a.get_n_trees() ### Description Returns the number of trees in the index `a`. ### Returns * int - The number of trees. ``` -------------------------------- ### Get Distance Source: https://github.com/spotify/annoy/blob/main/README.rst Calculates and returns the distance between two items in the index. ```APIDOC ## a.get_distance(i, j) ### Description Returns the distance between item `i` and item `j` in the index `a`. Note: As of Aug 2016, this returns the actual distance, not the squared distance. ### Parameters * **i** (int) - The identifier of the first item. * **j** (int) - The identifier of the second item. ### Returns * float - The distance between item `i` and item `j`. ``` -------------------------------- ### Get Number of Trees (Python) Source: https://github.com/spotify/annoy/blob/main/README.rst Returns the number of trees in the Annoy index's forest. ```python a.get_n_trees() ``` -------------------------------- ### Get Distance Between Items (Python) Source: https://github.com/spotify/annoy/blob/main/README.rst Calculates and returns the distance between two items identified by their IDs. ```python a.get_distance(i, j) ``` -------------------------------- ### Get Nearest Neighbors by Item Source: https://github.com/spotify/annoy/blob/main/README.rst Finds the `n` nearest neighbors for a given item `i`. The search can be tuned using `search_k` and can optionally return distances. ```APIDOC ## a.get_nns_by_item(i, n, search_k=-1, include_distances=False) ### Description Returns the `n` nearest neighbors to item `i`. The query inspects up to `search_k` nodes. If `search_k` is not provided, it defaults to `n_trees * n`. Setting `include_distances` to `True` returns a tuple containing two lists: neighbors and their corresponding distances. ### Parameters * **i** (int) - The item identifier to find neighbors for. * **n** (int) - The number of nearest neighbors to return. * **search_k** (int, optional) - The number of nodes to inspect during the query. Defaults to `n_trees * n`. * **include_distances** (bool, optional) - If `True`, returns distances along with neighbors. Defaults to `False`. ### Returns * list of ints or tuple(list of ints, list of floats) - A list of neighbor item identifiers, or a tuple containing neighbor identifiers and their distances. ``` -------------------------------- ### Get Item Vector Source: https://github.com/spotify/annoy/blob/main/README.rst Retrieves the vector associated with a previously added item identifier. ```APIDOC ## a.get_item_vector(i) ### Description Returns the vector for item `i` that was previously added to the index `a`. ### Parameters * **i** (int) - The identifier of the item whose vector to retrieve. ### Returns * list of floats - The vector associated with the item. ``` -------------------------------- ### Get Number of Items Source: https://github.com/spotify/annoy/blob/main/README.rst Returns the total number of items currently stored in the Annoy index. ```APIDOC ## a.get_n_items() ### Description Returns the number of items currently in the index `a`. ### Returns * int - The total number of items. ``` -------------------------------- ### Get Number of Items (Python) Source: https://github.com/spotify/annoy/blob/main/README.rst Returns the total number of items currently stored in the Annoy index. ```python a.get_n_items() ``` -------------------------------- ### Get Nearest Neighbors by Item (Python) Source: https://github.com/spotify/annoy/blob/main/README.rst Retrieves the N nearest neighbors for a given item ID. `search_k` controls the number of nodes inspected during the query. ```python a.get_nns_by_item(i, n, search_k=-1, include_distances=False) ``` -------------------------------- ### Get Nearest Neighbors by Vector (Python) Source: https://github.com/spotify/annoy/blob/main/README.rst Retrieves the N nearest neighbors for a given query vector. `search_k` controls the number of nodes inspected during the query. ```python a.get_nns_by_vector(v, n, search_k=-1, include_distances=False) ``` -------------------------------- ### Get Item Vector (Python) Source: https://github.com/spotify/annoy/blob/main/README.rst Retrieves the vector associated with a previously added item ID. ```python a.get_item_vector(i) ``` -------------------------------- ### Get Nearest Neighbors by Vector Source: https://github.com/spotify/annoy/blob/main/README.rst Finds the `n` nearest neighbors for a given vector `v`. Similar to `get_nns_by_item`, this method allows tuning with `search_k` and can return distances. ```APIDOC ## a.get_nns_by_vector(v, n, search_k=-1, include_distances=False) ### Description Returns the `n` nearest neighbors to the provided vector `v`. The query inspects up to `search_k` nodes. If `search_k` is not provided, it defaults to `n_trees * n`. Setting `include_distances` to `True` returns a tuple containing two lists: neighbors and their corresponding distances. ### Parameters * **v** (list of floats) - The vector to find neighbors for. * **n** (int) - The number of nearest neighbors to return. * **search_k** (int, optional) - The number of nodes to inspect during the query. Defaults to `n_trees * n`. * **include_distances** (bool, optional) - If `True`, returns distances along with neighbors. Defaults to `False`. ### Returns * list of ints or tuple(list of ints, list of floats) - A list of neighbor item identifiers, or a tuple containing neighbor identifiers and their distances. ``` -------------------------------- ### Update and Commit Version Source: https://github.com/spotify/annoy/blob/main/RELEASE.md Update the version number in setup.py and commit the change. ```bash git add setup.py && git commit -m "version 1.2.3" ``` -------------------------------- ### Build Distribution Packages Source: https://github.com/spotify/annoy/blob/main/RELEASE.md Create source distribution and wheel packages for the release. ```bash python setup.py sdist bdist_wheel ``` -------------------------------- ### On-Disk Build Preparation (Python) Source: https://github.com/spotify/annoy/blob/main/README.rst Prepares Annoy to build the index directly to a specified file on disk, avoiding RAM usage during the build process. This must be called before adding items. ```python a.on_disk_build(fn) ``` -------------------------------- ### Upload to PyPI Source: https://github.com/spotify/annoy/blob/main/RELEASE.md Upload the distribution packages to the Python Package Index (PyPI) using twine. ```bash twine upload dist/annoy-1.2.3* ``` -------------------------------- ### Tag Release Version Source: https://github.com/spotify/annoy/blob/main/RELEASE.md Create an annotated tag for the new release version. ```bash git tag -a v1.2.3 -m "version 1.2.3" ``` -------------------------------- ### Load and Query Annoy Index (Python) Source: https://github.com/spotify/annoy/blob/main/README.rst Loads an existing Annoy index from disk and performs a nearest neighbor search for a given item. ```python u = AnnoyIndex(f, 'angular') u.load('test.ann') # super fast, will just mmap the file print(u.get_nns_by_item(0, 1000)) # will find the 1000 nearest neighbors ``` -------------------------------- ### Load Annoy Index from Disk (Python) Source: https://github.com/spotify/annoy/blob/main/README.rst Loads (memory-maps) an Annoy index from disk. Setting `prefault=True` pre-reads the entire file into memory. ```python a.load(fn, prefault=False) ``` -------------------------------- ### On-Disk Build Source: https://github.com/spotify/annoy/blob/main/README.rst Configures the Annoy index to build directly into a specified file, avoiding the need for a separate save operation after building. ```APIDOC ## a.on_disk_build(fn) ### Description Prepares the index `a` to build directly into the file specified by `fn`. This should be called before adding items. No `save` call is needed after building when using this method. ### Parameters * **fn** (string) - The filename where the index will be built. ``` -------------------------------- ### Push Tags and Master Branch Source: https://github.com/spotify/annoy/blob/main/RELEASE.md Push the newly created tag and the master branch to the origin repository. ```bash git push --tags origin master ``` -------------------------------- ### Build Annoy Index Forest (Python) Source: https://github.com/spotify/annoy/blob/main/README.rst Builds a forest of trees for the Annoy index. More trees increase precision but also index size. `n_jobs` specifies the number of threads for building. ```python a.build(n_trees, n_jobs=-1) ``` -------------------------------- ### Load Index Source: https://github.com/spotify/annoy/blob/main/README.rst Loads an Annoy index from disk using memory mapping. The `prefault` option controls whether the file is pre-read into memory. ```APIDOC ## a.load(fn, prefault=False) ### Description Loads (memory-maps) an index from the file specified by `fn`. If `prefault` is `True`, it will pre-read the entire file into memory. Default is `False`. ### Parameters * **fn** (string) - The filename to load the index from. * **prefault** (bool, optional) - If `True`, pre-reads the entire file into memory. Defaults to `False`. ``` -------------------------------- ### Build Index Source: https://github.com/spotify/annoy/blob/main/README.rst Builds a forest of trees for the Annoy index. More trees generally lead to higher precision during queries. After building, no more items can be added. ```APIDOC ## a.build(n_trees, n_jobs=-1) ### Description Builds a forest of `n_trees` trees for the index `a`. A higher number of trees increases query precision but also index size. After calling `build`, no more items can be added. `n_jobs` specifies the number of threads to use for building; -1 uses all available CPU cores. ### Parameters * **n_trees** (int) - The number of trees to build in the forest. * **n_jobs** (int, optional) - The number of threads to use for building. Defaults to -1 (all cores). ``` -------------------------------- ### Add hererocks bin to PATH Source: https://github.com/spotify/annoy/blob/main/README_Lua.md Export the PATH environment variable to include the local bin directory of hererocks. ```bash export PATH="$(pwd)/here/bin/:$PATH" ``` -------------------------------- ### Include Annoy Library (C++) Source: https://github.com/spotify/annoy/blob/main/README.rst Includes the necessary Annoy library header file to use its C++ API. ```cpp #include "annoylib.h" ``` -------------------------------- ### AnnoyIndex Initialization Source: https://github.com/spotify/annoy/blob/main/README.rst Initializes a new Annoy index. The index can be read-write and stores vectors of a specified dimension `f`. The `metric` parameter defines the distance metric used for the index. ```APIDOC ## AnnoyIndex(f, metric) ### Description Initializes a new index that's read-write and stores vectors of `f` dimensions. The `metric` can be one of "angular", "euclidean", "manhattan", "hamming", or "dot". ### Parameters * **f** (int) - The dimensionality of the vectors. * **metric** (string) - The distance metric to use. Options: "angular", "euclidean", "manhattan", "hamming", "dot". ### Returns * AnnoyIndex - A new Annoy index object. ``` -------------------------------- ### Create and Build Annoy Index Source: https://github.com/spotify/annoy/blob/main/README.rst This Python snippet demonstrates how to create an Annoy index, add items with their corresponding vectors, build the index with a specified number of trees, and save it to a file. It uses Euclidean distance by default ('angular' is specified here). ```python from annoy import AnnoyIndex import random f = 40 # Length of item vector that will be indexed t = AnnoyIndex(f, 'angular') for i in range(1000): v = [random.gauss(0, 1) for z in range(f)] t.add_item(i, v) t.build(10) # 10 trees t.save('test.ann') ``` -------------------------------- ### Set Random Seed (Python) Source: https://github.com/spotify/annoy/blob/main/README.rst Initializes the random number generator with a given seed for reproducible tree building. This must be called before adding items or building the index. ```python a.set_seed(seed) ``` -------------------------------- ### Save Annoy Index to Disk (Python) Source: https://github.com/spotify/annoy/blob/main/README.rst Saves the Annoy index to a specified file. After saving, no more items can be added. ```python a.save(fn, prefault=False) ``` -------------------------------- ### Run ANNoy Lua Tests Source: https://github.com/spotify/annoy/blob/main/README_Lua.md Execute the ANNoy Lua tests using the Busted test runner. ```bash busted test/annoy_test.lua ``` -------------------------------- ### Save Index Source: https://github.com/spotify/annoy/blob/main/README.rst Saves the Annoy index to disk. After saving, no more items can be added. The `prefault` option influences memory loading behavior. ```APIDOC ## a.save(fn, prefault=False) ### Description Saves the index `a` to the file specified by `fn`. After saving, no more items can be added. If `prefault` is `True`, the file is pre-read into memory. Default is `False`. ### Parameters * **fn** (string) - The filename to save the index to. * **prefault** (bool, optional) - If `True`, pre-reads the entire file into memory. Defaults to `False`. ``` -------------------------------- ### Set Seed Source: https://github.com/spotify/annoy/blob/main/README.rst Initializes the random number generator with a specified seed. This is only relevant for building the index and has no effect after building or loading. ```APIDOC ## a.set_seed(seed) ### Description Initializes the random number generator with the given `seed`. This is only used during the tree building process and must be called before adding items. It has no effect after `a.build()` or `a.load()` have been called. ### Parameters * **seed** (int) - The seed value for the random number generator. ``` -------------------------------- ### Unload Annoy Index (Python) Source: https://github.com/spotify/annoy/blob/main/README.rst Unloads the Annoy index from memory. ```python a.unload() ``` -------------------------------- ### Add Item Source: https://github.com/spotify/annoy/blob/main/README.rst Adds an item with a given identifier and its corresponding vector to the Annoy index. The index will allocate memory for `max(id) + 1` items. ```APIDOC ## a.add_item(i, v) ### Description Adds item `i` (a nonnegative integer) with vector `v` to the index `a`. Note that this will allocate memory for `max(i) + 1` items, assuming items are numbered from 0 to n-1. ### Parameters * **i** (int) - The nonnegative integer identifier for the item. * **v** (list of floats) - The vector representing the item. ``` -------------------------------- ### Add Item to Annoy Index (Python) Source: https://github.com/spotify/annoy/blob/main/README.rst Adds an item with a given integer identifier and its corresponding vector to the Annoy index. Note that the index allocates memory for max(id)+1 items. ```python a.add_item(i, v) ``` -------------------------------- ### Unload Index Source: https://github.com/spotify/annoy/blob/main/README.rst Unloads the Annoy index from memory. ```APIDOC ## a.unload() ### Description Unloads the Annoy index from memory. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.