From 19b2ed53674dd4803d575a4ba06c369353bc5a8f Mon Sep 17 00:00:00 2001 From: Shunsuke Kanda Date: Sat, 10 Jul 2021 23:27:49 +0900 Subject: [PATCH] add --- pybind/README.md | 92 ++++++++++++++++++++++++++++++++++++++++++++++++ pybind/sample.py | 2 +- 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 pybind/README.md diff --git a/pybind/README.md b/pybind/README.md new file mode 100644 index 0000000..bcaf8c1 --- /dev/null +++ b/pybind/README.md @@ -0,0 +1,92 @@ +# Python binding of Xcdat + +The Python3 binding of Xcdat is implemented through [pybind11](https://github.com/pybind/pybind11). + +After installing Xcdat and pybind11, you can install the Python module `xcdat` with the following command. + +```sh +$ python -m pip install . +``` + +## Sample usage + +`sample.py` provides a sample usage. + +```python +#!/usr/bin/env python3 + +import xcdat +import os + +# Prepare the dataset of keywords +keyset = xcdat.Keyset() +keyset.append('AirPods') +keyset.append('AirTag') +keyset.append('Mac') +keyset.append('MacBook') +keyset.append('MacBook_Air') +keyset.append('MacBook_Pro') +keyset.append('Mac_Mini') +keyset.append('Mac_Pro') +keyset.append('iMac') +keyset.append('iPad') +keyset.append('iPhone') +keyset.append('iPhone_SE') +keyset.complete() + +# You can choose a trie type from the four types. +Trie = xcdat.Trie8 +# Trie = xcdat.Trie16 +# Trie = xcdat.Trie7 +# Trie = xcdat.Trie15 + +# Build the trie dictionary +trie = Trie(keyset) + +# Get the statistics +print(f'Number of keys: {trie.num_keys()}') +print(f'Number of trie nodes: {trie.num_nodes()}') +print(f'Number of DA units: {trie.num_units()}') +print(f'Memory usage in bytes: {trie.memory_in_bytes()}') + +# Lookup the ID for a query key. +print(f'Lookup(Mac_Pro) = {trie.lookup("Mac_Pro")}') +print(f'Lookup(Google_Pixel) = {trie.lookup("Google_Pixel")}') + +# Decode the key for a query ID. +print(f'Decode(4) = {trie.decode(4)}') + +# Common prefix search +print('CommonPrefixSearch(MacBook_Air) = {') +itr = trie.make_prefix_iterator('MacBook_Air') +while itr.next(): + print(f' ({itr.decoded()}, {itr.id()})') +print('}') + +# Predictive search (in lexicographical order). +print('PredictiveSearch(Mac) = {') +itr = trie.make_predictive_iterator('Mac') +while itr.next(): + print(f' ({itr.decoded()}, {itr.id()})') +print('}') + +# Enumerate all the keys (in lexicographical order). +print('Enumerate() = {') +itr = trie.make_enumerative_iterator() +while itr.next(): + print(f' ({itr.decoded()}, {itr.id()})') +print('}') + +# Save the trie to the file +trie.save('dic.bin') + +# Load the trie from the file +other = Trie('dic.bin') +assert trie.num_keys() == other.num_keys() +assert trie.num_nodes() == other.num_nodes() +assert trie.num_units() == other.num_units() +assert trie.memory_in_bytes() == other.memory_in_bytes() + +os.remove('dic.bin') +``` + diff --git a/pybind/sample.py b/pybind/sample.py index 6f221d3..39fcf80 100755 --- a/pybind/sample.py +++ b/pybind/sample.py @@ -19,7 +19,7 @@ keyset.append('iPhone') keyset.append('iPhone_SE') keyset.complete() -# You can choose the trie type from the four types. +# You can choose a trie type from the four types. Trie = xcdat.Trie8 # Trie = xcdat.Trie16 # Trie = xcdat.Trie7