marisa-trie
This package provides a wrapper (a set of bindings) of MARISA-Trie.
Node requirements:
- Version >= 0.11.14 or Version == 0.10.x or Version == 0.8.x
Installation
npm install marisa-trie
If installation failed, try to update npm via npm install -g npm
and update
node-gyp bundled with npm.
Usage
The APIs are almost the same as the C++ library's. For example:
var Marisa = ; var keyset = ; // The keyword "new" is optional.keyset;keyset;keyset; var trie = ;trie; var agent = ;agent;while trie var key = agent; console;/* OUTPUT: * 0 'a' * 1 'app' * 2 'apple' */
APIs
For detailed information, please see the official site.
Marisa.Trie
;
-
build(keyset:Keyset [, flag:Number])
Initialize the trie with a keyset.
-
mmap(filepath:String)
Map a file of trie data into memory and build the trie with it.
-
map(buffer:Buffer [, length:Number])
Map a UTF8-encoded string buffer into memory and build the trie with it. If
length
is specified, only load the firstlength
bytes of the buffer. -
load(filepath:String)
Load trie data from a file and build the trie.
-
read(fd:Number)
Read trie data from a ReadStream and build the trie.
fd
is an integer file descriptor. -
save(filepath:String)
Dump the trie data into a file.
-
write(fd:Number)
Dump the trie data into a WriteStream.
fd
is an integer file descriptor'. -
empty(): Boolean
Check whether the trie has no keys.
-
size(): Number
Return the number of keys. It is the same as
num_keys()
. -
io_size(): Number
Return the size of trie data in byte.
-
lookup(agent:Agent): Boolean
Check whether the key
agent.query().ptr()
is registered. If it is registered, returntrue
, otherwisefalse
. The search result is available throughagent.key()
andagent.key().ptr()
should be equal toagent.query().ptr()
. -
reverse_lookup(agent:Agent)
Restore a key with its ID
agent.query().id()
. The result isagent.key().ptr()
. This method will throw an error if the ID is out of range. -
common_prefix_search(agent:Agent): Boolean
Search keys with the possible prefixes of the query string
agent.query().ptr()
. If any matching key is found, return true. The first key is available throughagent.key()
. The next match will be available after the nextcommon_prefix_search()
. -
predictive_search(agent:Agent): Boolean
Search keys started with the query string
agent.query().ptr()
. And Similar tocommon_prefix_search()
, it will return true until there are no more matching keys. There is one difference from the C++ version:agent.key().length()
is equal toBuffer.byteLength(agent.key().ptr())
.
Note: An agent keeps the internal state of common_prefix_search()
and
predictive_search()
until it is passed to another search function or
agent.set_query()
is called.
Marisa.Keyset
;
-
[index]: Key
Access the key at the index. Return
undefined
if the index is out of range. -
push_back(key:Key)
Add one key to the key set.
-
push_back(string:String [, length:Number [, weight:Number]])
Add one key to the key set. If
length
is specified, only read the firstlength
bytes from the string. Andweight
can be used as the frequency of the key to help optimize the searching performance optionally. Weights will be accumulated for the same keys. After building the trie, weights will be overwritten by key IDs because every key uses a union to store a weight or an ID.If the key string contains NUL (0x00) characters, please specify the length explicitly so as to keep the NULs.
-
empty(): Boolean
Check whether the key set has no keys.
-
size(): Number
Return the number of keys. It is the same as
num_keys()
. -
total_length(): Number
Return the total length of all keys in byte.
Marisa.Agent
;
-
query(): Query
Return the query information.
-
key(): Key
Return the search result.
-
set_query(string:String [, length:Number])
Set the query string. If
length
is specified, read only the firstlength
bytes from the string.If the query string contains NUL (0x00) characters, please specify the length explicitly so as to keep the NULs.
-
set_query(id:Number)
Set the ID of key to restore.
Marisa.Query
;
-
[index]: Number
Access the UTF-8 unit at the index. Return
undefined
if the index is out of range. -
ptr(): String
Return the query string.
-
length(): Number
Return the byte-length of the query string.
-
id(): Number
Return the ID of the key to restore.
Marisa.Key
;
-
[index]: Number
Access the UTF-8 unit at the index. Return
undefined
if the index is out of range. -
ptr(): String
Return the last result of the query as a string.
-
length(): Number
Return the byte-length of the result string.
-
id(): Number
Return the ID of the key.
Enumerations
typedef enum marisa_num_tries_ marisa_num_tries;
Marisa.MIN_NUM_TRIES
Marisa.MAX_NUM_TRIES
Marisa.DEFAULT_NUM_TRIES
typedef enum marisa_cache_level_ marisa_cache_level;
Marisa.HUGE_CACHE
Marisa.LARGE_CACHE
Marisa.NORMAL_CACHE
Marisa.SMALL_CACHE
Marisa.TINY_CACHE
Marisa.DEFAULT_CACHE
typedef enum marisa_tail_mode_ marisa_tail_mode;
Marisa.TEXT_TAIL
Marisa.BINARY_TAIL
Marisa.DEFAULT_TAIL
typedef enum marisa_node_order_ marisa_node_order;
Marisa.LABEL_ORDER
Marisa.WEIGHT_ORDER
Marisa.DEFAULT_ORDER
typedef enum marisa_config_mask_ marisa_config_mask;
Marisa.NUM_TRIES_MASK
Marisa.CACHE_LEVEL_MASK
Marisa.TAIL_MODE_MASK
Marisa.NODE_ORDER_MASK
Marisa.CONFIG_MASK
Benchmarks
Node v0.10.40 on my Macbook i5 2.4Ghz (Your mileage may vary.):
METHOD | TIME (ms/r) | SPEED (K/s) | SPEED (ns/key) | SIZE (N) | IO_SIZE (bytes) | ROUNDS (N)
build | 412.37 | 863.35 | 1158.28 | 356017 | 1301528 | 10
lookup | 284.85 | 1249.86 | 800.09 | 356017 | 1301528 | 10
reverse_lookup | 198.15 | 1796.67 | 556.59 | 356017 | 1301528 | 10
common_prefix_search | 347.49 | 1024.53 | 976.05 | 356017 | 1301528 | 10
predictive_search | 379.03 | 939.29 | 1064.64 | 356017 | 1301528 | 10
K/s – thousand keys per second
Normally, the speed of reverse_lookup
should be the highest.
Travis CI Results: https://travis-ci.org/jakwings/iojs-marisa-trie
License
The wrapper code is licensed under ISC License.
The bundled source code of MARISA-Trie is dual-licensed under LGPL-2.1+ and BSD-2-Clause license.