Skip to content

Instantly share code, notes, and snippets.

@nakagami nakagami/darts_sample.cpp
Last active Oct 11, 2019

Embed
What would you like to do?
Darts (Double-ARray Trie System) http://chasen.org/~taku/software/darts/ sample code.
#include <iostream>
#include <darts.h>
// Darts http://chasen.org/~taku/software/darts/ sample code
int main (int argc, char **argv)
{
using namespace std;
const Darts::DoubleArray::key_type *str[] = { "ALGOL", "ANSI", "ARCO", "ARPA", "ARPANET", "ASCII" }; // same as char*
Darts::DoubleArray::result_type val[] = { 1, 2, 3, 4, 5, 6 }; // same as int
// build
Darts::DoubleArray da;
da.build (6, str, 0, val);
// exactMatchSearch
cout << da.exactMatchSearch<Darts::DoubleArray::result_type>("ALGOL") << endl;
cout << da.exactMatchSearch<Darts::DoubleArray::result_type>("ANSI") << endl;
cout << da.exactMatchSearch<Darts::DoubleArray::result_type>("ARCO") << endl;;
cout << da.exactMatchSearch<Darts::DoubleArray::result_type>("ARPA") << endl;;
cout << da.exactMatchSearch<Darts::DoubleArray::result_type>("ARPANET") << endl;;
cout << da.exactMatchSearch<Darts::DoubleArray::result_type>("ASCII") << endl;;
cout << da.exactMatchSearch<Darts::DoubleArray::result_type>("APPARE") << endl;
// commonPrefixSearch
Darts::DoubleArray::result_pair_type result_pair[1024];
size_t num = da.commonPrefixSearch("ARPANET", result_pair, sizeof(result_pair));
cout << "found:" << num << endl;
for (size_t i = 0; i < num; ++i) {
cout << "\tvalue:" << result_pair[i].value << " matched key length:" << result_pair[i].length << endl;
}
num = da.commonPrefixSearch("ARPANET", result_pair, sizeof(result_pair));
cout << "found:" << num << endl;
// save to file
da.save("sample.dic");
da.clear();
// load from file and commonPrefixSearch
Darts::DoubleArray da2;
da2.open("sample.dic");
num = da2.commonPrefixSearch("ARPANET", result_pair, sizeof(result_pair));
cout << "found:" << num << endl;
da2.clear();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.