melisa 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +53 -4
- data/ext/extconf.rb +33 -0
- data/ext/marisa-0.2.4/AUTHORS +1 -0
- data/ext/marisa-0.2.4/COPYING +32 -0
- data/ext/marisa-0.2.4/ChangeLog +0 -0
- data/ext/marisa-0.2.4/INSTALL +237 -0
- data/ext/marisa-0.2.4/Makefile.am +24 -0
- data/ext/marisa-0.2.4/Makefile.in +803 -0
- data/ext/marisa-0.2.4/NEWS +0 -0
- data/ext/marisa-0.2.4/README +63 -0
- data/ext/marisa-0.2.4/aclocal.m4 +976 -0
- data/ext/marisa-0.2.4/bindings/Makefile +16 -0
- data/ext/{marisa → marisa-0.2.4}/bindings/marisa-swig.cxx +0 -0
- data/ext/{marisa → marisa-0.2.4}/bindings/marisa-swig.h +0 -0
- data/ext/marisa-0.2.4/bindings/marisa-swig.i +28 -0
- data/ext/marisa-0.2.4/bindings/perl/Makefile.PL +7 -0
- data/ext/{marisa → marisa-0.2.4}/bindings/perl/marisa-swig.cxx +0 -0
- data/ext/{marisa → marisa-0.2.4}/bindings/perl/marisa-swig.h +0 -0
- data/ext/{marisa → marisa-0.2.4}/bindings/perl/marisa-swig_wrap.cxx +0 -0
- data/ext/marisa-0.2.4/bindings/perl/marisa.pm +297 -0
- data/ext/marisa-0.2.4/bindings/perl/sample.dic +0 -0
- data/ext/marisa-0.2.4/bindings/perl/sample.pl +62 -0
- data/ext/{marisa → marisa-0.2.4}/bindings/python/marisa-swig.cxx +0 -0
- data/ext/{marisa → marisa-0.2.4}/bindings/python/marisa-swig.h +0 -0
- data/ext/{marisa → marisa-0.2.4}/bindings/python/marisa-swig_wrap.cxx +0 -0
- data/ext/marisa-0.2.4/bindings/python/marisa.py +206 -0
- data/ext/marisa-0.2.4/bindings/python/sample.dic +0 -0
- data/ext/marisa-0.2.4/bindings/python/sample.py +57 -0
- data/ext/marisa-0.2.4/bindings/python/setup.py +9 -0
- data/ext/marisa-0.2.4/config.guess +1530 -0
- data/ext/marisa-0.2.4/config.sub +1773 -0
- data/ext/marisa-0.2.4/configure +17636 -0
- data/ext/marisa-0.2.4/configure.ac +172 -0
- data/ext/marisa-0.2.4/depcomp +688 -0
- data/ext/marisa-0.2.4/docs/readme.en.html +740 -0
- data/ext/marisa-0.2.4/docs/readme.ja.html +750 -0
- data/ext/marisa-0.2.4/docs/style.css +245 -0
- data/ext/marisa-0.2.4/install-sh +520 -0
- data/ext/marisa-0.2.4/lib/Makefile.am +57 -0
- data/ext/marisa-0.2.4/lib/Makefile.in +701 -0
- data/ext/{marisa → marisa-0.2.4}/lib/marisa.h +0 -0
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/agent.cc +2 -2
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/agent.h +2 -2
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/base.h +3 -3
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/exception.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/algorithm.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/algorithm/sort.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/intrin.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/io.h +3 -3
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/io/mapper.cc +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/io/mapper.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/io/reader.cc +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/io/reader.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/io/writer.cc +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/io/writer.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/trie.h +2 -2
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/trie/cache.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/trie/config.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/trie/entry.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/trie/header.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/trie/history.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/trie/key.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/trie/louds-trie.cc +5 -5
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/trie/louds-trie.h +7 -7
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/trie/range.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/trie/state.h +2 -2
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/trie/tail.cc +3 -3
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/trie/tail.h +3 -3
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/vector.h +3 -3
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/vector/bit-vector.cc +2 -2
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/vector/bit-vector.h +2 -2
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/vector/flat-vector.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/vector/pop-count.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/vector/rank-index.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/grimoire/vector/vector.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/iostream.h +0 -0
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/key.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/keyset.cc +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/keyset.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/query.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/scoped-array.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/scoped-ptr.h +1 -1
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/stdio.h +0 -0
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/trie.cc +4 -4
- data/ext/{marisa → marisa-0.2.4}/lib/marisa/trie.h +2 -2
- data/ext/marisa-0.2.4/ltmain.sh +9661 -0
- data/ext/marisa-0.2.4/m4/libtool.m4 +8001 -0
- data/ext/marisa-0.2.4/m4/ltoptions.m4 +384 -0
- data/ext/marisa-0.2.4/m4/ltsugar.m4 +123 -0
- data/ext/marisa-0.2.4/m4/ltversion.m4 +23 -0
- data/ext/marisa-0.2.4/m4/lt~obsolete.m4 +98 -0
- data/ext/marisa-0.2.4/marisa.pc.in +11 -0
- data/ext/marisa-0.2.4/missing +331 -0
- data/ext/marisa-0.2.4/mkmf.log +288 -0
- data/ext/marisa-0.2.4/pkg/bin/marisa-benchmark +0 -0
- data/ext/marisa-0.2.4/pkg/bin/marisa-build +0 -0
- data/ext/marisa-0.2.4/pkg/bin/marisa-common-prefix-search +0 -0
- data/ext/marisa-0.2.4/pkg/bin/marisa-dump +0 -0
- data/ext/marisa-0.2.4/pkg/bin/marisa-lookup +0 -0
- data/ext/marisa-0.2.4/pkg/bin/marisa-predictive-search +0 -0
- data/ext/marisa-0.2.4/pkg/bin/marisa-reverse-lookup +0 -0
- data/ext/marisa-0.2.4/pkg/include/marisa.h +14 -0
- data/ext/marisa-0.2.4/pkg/include/marisa/agent.h +73 -0
- data/ext/marisa-0.2.4/pkg/include/marisa/base.h +193 -0
- data/ext/marisa-0.2.4/pkg/include/marisa/exception.h +82 -0
- data/ext/marisa-0.2.4/pkg/include/marisa/iostream.h +18 -0
- data/ext/marisa-0.2.4/pkg/include/marisa/key.h +85 -0
- data/ext/marisa-0.2.4/pkg/include/marisa/keyset.h +80 -0
- data/ext/marisa-0.2.4/pkg/include/marisa/query.h +71 -0
- data/ext/marisa-0.2.4/pkg/include/marisa/scoped-array.h +48 -0
- data/ext/marisa-0.2.4/pkg/include/marisa/scoped-ptr.h +52 -0
- data/ext/marisa-0.2.4/pkg/include/marisa/stdio.h +15 -0
- data/ext/marisa-0.2.4/pkg/include/marisa/trie.h +64 -0
- data/ext/marisa-0.2.4/pkg/lib/libmarisa.0.dylib +0 -0
- data/ext/marisa-0.2.4/pkg/lib/libmarisa.a +0 -0
- data/ext/marisa-0.2.4/pkg/lib/libmarisa.dylib +0 -0
- data/ext/marisa-0.2.4/pkg/lib/libmarisa.la +41 -0
- data/ext/marisa-0.2.4/pkg/lib/pkgconfig/marisa.pc +11 -0
- data/ext/marisa-0.2.4/tests/Makefile.am +27 -0
- data/ext/marisa-0.2.4/tests/Makefile.in +624 -0
- data/ext/{marisa → marisa-0.2.4}/tests/base-test.cc +0 -0
- data/ext/{marisa → marisa-0.2.4}/tests/io-test.cc +0 -0
- data/ext/{marisa → marisa-0.2.4}/tests/marisa-assert.h +0 -0
- data/ext/{marisa → marisa-0.2.4}/tests/marisa-test.cc +0 -0
- data/ext/{marisa → marisa-0.2.4}/tests/trie-test.cc +0 -0
- data/ext/{marisa → marisa-0.2.4}/tests/vector-test.cc +0 -0
- data/ext/marisa-0.2.4/tools/Makefile.am +39 -0
- data/ext/marisa-0.2.4/tools/Makefile.in +616 -0
- data/ext/{marisa → marisa-0.2.4}/tools/cmdopt.cc +0 -0
- data/ext/{marisa → marisa-0.2.4}/tools/cmdopt.h +0 -0
- data/ext/{marisa → marisa-0.2.4}/tools/marisa-benchmark.cc +0 -0
- data/ext/{marisa → marisa-0.2.4}/tools/marisa-build.cc +0 -0
- data/ext/{marisa → marisa-0.2.4}/tools/marisa-common-prefix-search.cc +0 -0
- data/ext/{marisa → marisa-0.2.4}/tools/marisa-dump.cc +0 -0
- data/ext/{marisa → marisa-0.2.4}/tools/marisa-lookup.cc +0 -0
- data/ext/{marisa → marisa-0.2.4}/tools/marisa-predictive-search.cc +0 -0
- data/ext/{marisa → marisa-0.2.4}/tools/marisa-reverse-lookup.cc +0 -0
- data/ext/marisa-0.2.4/vs2008/base-test/base-test.vcproj +200 -0
- data/ext/marisa-0.2.4/vs2008/io-test/io-test.vcproj +199 -0
- data/ext/marisa-0.2.4/vs2008/libmarisa/libmarisa.vcproj +347 -0
- data/ext/marisa-0.2.4/vs2008/marisa-benchmark/marisa-benchmark.vcproj +203 -0
- data/ext/marisa-0.2.4/vs2008/marisa-build/marisa-build.vcproj +203 -0
- data/ext/marisa-0.2.4/vs2008/marisa-common-prefix-search/marisa-common-prefix-search.vcproj +203 -0
- data/ext/marisa-0.2.4/vs2008/marisa-dump/marisa-dump.vcproj +203 -0
- data/ext/marisa-0.2.4/vs2008/marisa-lookup/marisa-lookup.vcproj +203 -0
- data/ext/marisa-0.2.4/vs2008/marisa-predictive-search/marisa-predictive-search.vcproj +203 -0
- data/ext/marisa-0.2.4/vs2008/marisa-reverse-lookup/marisa-reverse-lookup.vcproj +203 -0
- data/ext/marisa-0.2.4/vs2008/marisa-test/marisa-test.vcproj +199 -0
- data/ext/marisa-0.2.4/vs2008/trie-test/trie-test.vcproj +199 -0
- data/ext/marisa-0.2.4/vs2008/vector-test/vector-test.vcproj +199 -0
- data/ext/marisa-0.2.4/vs2008/vs2008.sln +123 -0
- data/ext/marisa-0.2.4/vs2008/vs2008.suo +0 -0
- data/ext/{marisa/bindings/ruby/marisa-swig.cxx → marisa-swig.cxx} +0 -0
- data/ext/{marisa/bindings/ruby/marisa-swig.h → marisa-swig.h} +0 -0
- data/ext/{marisa/bindings/ruby/marisa-swig_wrap.cxx → marisa-swig_wrap.cxx} +0 -0
- data/lib/melisa/bytes_trie.rb +5 -0
- data/lib/melisa/trie.rb +2 -0
- data/lib/melisa/version.rb +1 -1
- data/melisa.gemspec +7 -9
- data/spec/bytes_trie_spec.rb +6 -1
- data/spec/int_trie_spec.rb +5 -0
- data/spec/search_spec.rb +6 -7
- data/spec/trie_spec.rb +4 -4
- metadata +178 -121
- data/ext/marisa/bindings/ruby/extconf.rb +0 -5
@@ -0,0 +1,750 @@
|
|
1
|
+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
2
|
+
<html lang="ja">
|
3
|
+
<head>
|
4
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
5
|
+
<title>MARISA: Matching Algorithm with Recursively Implemented StorAge</title>
|
6
|
+
<link rel="stylesheet" type="text/css" href="./style.css">
|
7
|
+
</head>
|
8
|
+
<body>
|
9
|
+
<div id="header">
|
10
|
+
<div class="left">MARISA: Matching Algorithm with Recursively Implemented StorAge</div>
|
11
|
+
<div class="right">Last modified: 13 April 2013</div>
|
12
|
+
<div class="end"></div>
|
13
|
+
</div><!-- header -->
|
14
|
+
<div id="body">
|
15
|
+
<h1>MARISA: Matching Algorithm with Recursively Implemented StorAge</h1>
|
16
|
+
<p id="abstract">
|
17
|
+
<span id="heading">Abstract: </span>
|
18
|
+
Matching Algorithm with Recursively Implemented StorAge (MARISA) は Trie をコンパクトに表現する程度の能力を持つデータ構造です.libmarisa は MARISA を C++ で実装したライブラリであり,MARISA による辞書を構築したり,辞書からの検索をおこなったりできます.libmarisa の基本的な機能に対応するコマンドラインツールを用意しているので,辞書のサイズがどのくらいになるのか,検索にどのくらい時間がかかるのか,などを手軽に試すことができます.
|
19
|
+
</p><!-- abstract -->
|
20
|
+
|
21
|
+
<div class="section">
|
22
|
+
<h2><a name="introduction">はじめに</a></h2>
|
23
|
+
<div class="subsection">
|
24
|
+
<h3><a name="overview">概要</a></h3>
|
25
|
+
<p>
|
26
|
+
Matching Algorithm with Recursively Implemented StorAge (MARISA) は Trie に対するコンパクトなデータ構造であり,動的な更新には対応していないものの,高い空間効率とそれなりの時間効率を実現できます.また,Trie の特徴を引き継いでいるため,MARISA による辞書は,単純な辞書引きだけでなく,逆引き,Common Prefix Search や Predictive Search を効率良く実現できます.
|
27
|
+
</p>
|
28
|
+
<p>
|
29
|
+
ほとんどの場合,MARISA による辞書は,登録文字列の集合を連結してできるテキストと比べて,ずっとコンパクトになります.そのため,登録文字列をそのまま保持する標準的な二分探索木やハッシュ表と比べると,ずーっとコンパクトです.一方で,確率的なデータ構造である Bloom Filter と比べてみると,空間効率の面では劣りますが,False Positive がなく,逆引きに加えて Common Prefix Search や Predictive Search を効率良く実現できることが特徴になります.
|
30
|
+
</p>
|
31
|
+
<p>
|
32
|
+
libmarisa は MARISA を C++ で実装したライブラリであり,MARISA による辞書を構築したり,辞書からの検索をおこなったりできます.libmarisa の基本的な機能に対応するコマンドラインツールを用意しているので,辞書のサイズがどのくらいになるのか,検索にどのくらい時間がかかるのか,などを手軽に試すことができます.
|
33
|
+
</p>
|
34
|
+
</div><!-- subsection -->
|
35
|
+
<div class="subsection">
|
36
|
+
<h3><a name="ability">できること</a></h3>
|
37
|
+
<p>
|
38
|
+
libmarisa による辞書の構築では,登録文字列に <var>0</var> から順に固有の ID を割り当てるようになっています.ID は自動的に割り当てられるので,登録文字列に任意の ID を割り当てることはできません.検索においては,文字列あるいは ID をクエリとして受け取り,適合する登録文字列と ID の組を検索結果として返すようになっています.
|
39
|
+
</p>
|
40
|
+
<ul>
|
41
|
+
<li>辞書引き(Lookup)
|
42
|
+
<ul>
|
43
|
+
<li>入力文字列が登録されているかどうかを確認します.</li>
|
44
|
+
</ul>
|
45
|
+
</li>
|
46
|
+
<li>逆引き(Reverse Lookup)
|
47
|
+
<ul>
|
48
|
+
<li>入力された ID から登録文字列を復元します.</li>
|
49
|
+
</ul>
|
50
|
+
</li>
|
51
|
+
<li>Common Prefix Search
|
52
|
+
<ul>
|
53
|
+
<li>入力文字列の前半部分に一致する登録文字列を検索します.</li>
|
54
|
+
</ul>
|
55
|
+
</li>
|
56
|
+
<li>Predictive Search
|
57
|
+
<ul>
|
58
|
+
<li>入力文字列で始まる登録文字列を検索します.</li>
|
59
|
+
</ul>
|
60
|
+
</li>
|
61
|
+
</ul>
|
62
|
+
</div><!-- subsection -->
|
63
|
+
</div><!-- section -->
|
64
|
+
<div class="section">
|
65
|
+
<h2><a name="source">ソースコード</a></h2>
|
66
|
+
<div class="subsection">
|
67
|
+
<h3><a name="license">ライセンス</a></h3>
|
68
|
+
<p>
|
69
|
+
libmarisa および付属のコマンドラインツールはフリーソフトウェアです.使用・再配布については,二条項 BSD ライセンスと LGPL のデュアルライセンスを採用しています.
|
70
|
+
</p>
|
71
|
+
</div><!-- subsection -->
|
72
|
+
<div class="subsection">
|
73
|
+
<h3><a name="download">ダウンロード</a></h3>
|
74
|
+
<p>
|
75
|
+
プロジェクトの管理やソースコードの配布には <a href="http://code.google.com/hosting/">Google Project Hosting</a> を利用しています.
|
76
|
+
</p>
|
77
|
+
<ul>
|
78
|
+
<li>プロジェクト
|
79
|
+
<ul>
|
80
|
+
<li><a href="http://code.google.com/p/marisa-trie/">http://code.google.com/p/marisa-trie/</a></li>
|
81
|
+
</ul>
|
82
|
+
</li>
|
83
|
+
<li>ソースコード
|
84
|
+
<ul>
|
85
|
+
<li><a href="http://marisa-trie.googlecode.com/files/marisa-0.2.4.tar.gz">marisa-0.2.4.tar.gz</a></li>
|
86
|
+
</ul>
|
87
|
+
</li>
|
88
|
+
</ul>
|
89
|
+
</div><!-- subsection -->
|
90
|
+
</div><!-- section -->
|
91
|
+
|
92
|
+
<div class="section">
|
93
|
+
<h2><a name="install">インストール</a></h2>
|
94
|
+
<div class="subsection">
|
95
|
+
<h3><a name="gcc">GCC & Clang</a></h3>
|
96
|
+
<div class="float">
|
97
|
+
<pre class="console">$ tar zxf marisa-0.2.4.tar.gz
|
98
|
+
$ cd marisa-0.2.4
|
99
|
+
$ ./configure
|
100
|
+
$ make
|
101
|
+
$ make check
|
102
|
+
$ make install</pre>
|
103
|
+
</div><!-- float -->
|
104
|
+
<p>
|
105
|
+
<kbd>configure</kbd> と <kbd>make</kbd> によりインストールできるようになっています.<kbd>make install</kbd> については,必要に応じて <kbd>sudo</kbd> を付けてご利用ください.また,特に指定がなければ libmarisa は共有ライブラリとしてインストールされるので,<kbd>ldconfig</kbd> が必要になるかもしれません.
|
106
|
+
</p>
|
107
|
+
<p>
|
108
|
+
POPCNT 命令が使える環境では,<kbd>configure</kbd> に <kbd>--enable-popcnt</kbd> を渡すことで,少し高速化することができます.同様に,<kbd>--enable-sse2</kbd>, <kbd>--enable-sse3</kbd>, <kbd>--enable-ssse3</kbd>, <kbd>--enable-sse4.1</kbd>, <kbd>--enable-sse4.2</kbd>, <kbd>--enable-sse4</kbd>, <kbd>--enable-sse4a</kbd> というオプションがあります.また,スタティックライブラリが必要なときは,<kbd>configure</kbd> に <kbd>--enable-static</kbd> を渡すようにしてください.その他,<kbd>configure</kbd> のオプションについては <kbd>./configure --help</kbd> をご覧ください.
|
109
|
+
</p>
|
110
|
+
</div><!-- subsection -->
|
111
|
+
<div class="subsection">
|
112
|
+
<h3><a name="vc">Visual C++ 2008</a></h3>
|
113
|
+
<p>
|
114
|
+
Visual C++ 2008 にて作成したファイルを <kbd>vs2008/</kbd> 以下に配置しています.Visual C++ 2008 以降であれば,<kbd>vs2008/vs2008.sln</kbd> を開くだけでスタティックライブラリ <kbd>libmarisa.lib</kbd> とコマンドラインツールをビルドできます.
|
115
|
+
</p>
|
116
|
+
<p>
|
117
|
+
Visual C++ 2008 より古い環境では,新しくプロジェクトを作る必要があります.プロジェクトを作成すれば問題なくビルドできると思いますが,試していないので断言はできません.
|
118
|
+
</p>
|
119
|
+
</div><!-- subsection -->
|
120
|
+
<div class="subsection">
|
121
|
+
<h3><a name="vc">Perl バインディング</a></h3>
|
122
|
+
<div class="float">
|
123
|
+
<pre class="console">$ cd bindings/perl
|
124
|
+
$ perl Makefile.PL
|
125
|
+
$ make
|
126
|
+
$ make install</pre>
|
127
|
+
</div><!-- float -->
|
128
|
+
<p>
|
129
|
+
<a href="http://www.swig.org/">SWIG</a> による Perl バインディングが <kbd>bindings/perl/</kbd> にあります.<kbd>perl Makefile.PL</kbd> により <kbd>Makefile</kbd> を作成し,<kbd>make install</kbd> を実行することでインストールできます.使い方については,<kbd>bindings/perl/sample.pl</kbd> を参考にしてください.
|
130
|
+
</p>
|
131
|
+
</div><!-- subsection -->
|
132
|
+
<div class="subsection">
|
133
|
+
<h3><a name="vc">Python バインディング</a></h3>
|
134
|
+
<div class="float">
|
135
|
+
<pre class="console">$ cd bindings/python
|
136
|
+
$ python setup.py build
|
137
|
+
$ python setup.py install</pre>
|
138
|
+
</div><!-- float -->
|
139
|
+
<p>
|
140
|
+
<a href="http://www.swig.org/">SWIG</a> による Python バインディングが <kbd>bindings/python/</kbd> にあります.<kbd>python setup.py install</kbd> により インストールできます.使い方については,<kbd>bindings/python/sample.py</kbd> を参考にしてください.
|
141
|
+
</p>
|
142
|
+
</div><!-- subsection -->
|
143
|
+
<div class="subsection">
|
144
|
+
<h3><a name="vc">Ruby バインディング</a></h3>
|
145
|
+
<div class="float">
|
146
|
+
<pre class="console">$ cd bindings/ruby
|
147
|
+
$ ruby extconf.rb
|
148
|
+
$ make
|
149
|
+
$ make install</pre>
|
150
|
+
</div><!-- float -->
|
151
|
+
<p>
|
152
|
+
<a href="http://www.swig.org/">SWIG</a> による Ruby バインディングが <kbd>bindings/ruby/</kbd> にあります.<kbd>ruby extconf.rb</kbd> により <kbd>Makefile</kbd> を作成し,<kbd>make install</kbd> を実行することでインストールできます.使い方については,<kbd>bindings/ruby/sample.rb</kbd> を参考にしてください.
|
153
|
+
</p>
|
154
|
+
</div><!-- subsection -->
|
155
|
+
<div class="subsection">
|
156
|
+
<h3><a name="vc">その他</a></h3>
|
157
|
+
<p>
|
158
|
+
上記のバインディング以外にも,<a href="https://github.com/kmike/marisa-trie/">高速・高機能な Python バインディング</a> や <a href="https://github.com/komiya-atsushi/node-marisa-trie">Node.js から使うためのモジュール</a> が開発されています.
|
159
|
+
</p>
|
160
|
+
</div><!-- subsection -->
|
161
|
+
</div><!-- section -->
|
162
|
+
|
163
|
+
<div class="section">
|
164
|
+
<h2><a name="tools">コマンドラインツール</a></h2>
|
165
|
+
<div class="subsection">
|
166
|
+
<h3><a name="marisa-build">marisa-build</a></h3>
|
167
|
+
<div class="float">
|
168
|
+
<pre class="console">$ marisa-build < keyset.txt > keyset.dic
|
169
|
+
#keys: 1342099
|
170
|
+
#nodes: 1832373
|
171
|
+
size: 7841664</pre>
|
172
|
+
</div><!-- float -->
|
173
|
+
<p>
|
174
|
+
<kbd>marisa-build</kbd> は辞書を構築するツールです.改行を区切りとして文字列を受け取り,構築した辞書を標準出力に書き出すようになっています.
|
175
|
+
</p>
|
176
|
+
<p>
|
177
|
+
構築する辞書のパラメータについては,オプションを使って指定できます.オプションの一覧は <kbd>marisa-build -h</kbd> により確認できます.
|
178
|
+
</p>
|
179
|
+
<p>
|
180
|
+
入力は改行区切りとなっていますが,水平タブが存在する行については,最後の水平タブ以降を文字列の重みとして扱うようになっています.文字列の出現頻度や出現確率を与えることにより,検索時間を短縮できる可能性があります.
|
181
|
+
</p>
|
182
|
+
</div><!-- subsection -->
|
183
|
+
<div class="subsection">
|
184
|
+
<h3><a name="marisa-lookup">marisa-lookup</a></h3>
|
185
|
+
<div class="float">
|
186
|
+
<pre class="console">$ marisa-lookup keyset.dic
|
187
|
+
東方
|
188
|
+
174385 東方
|
189
|
+
とうほう
|
190
|
+
-1 とうほう</pre>
|
191
|
+
</div><!-- float -->
|
192
|
+
<p>
|
193
|
+
<kbd>marisa-lookup</kbd> は単純な辞書引きをおこなうツールです.入力された文字列が登録されていれば ID とともに出力し,登録されていなければ <var>-1</var> とともに出力します.
|
194
|
+
</p>
|
195
|
+
<p>
|
196
|
+
オプションの一覧は <kbd>marisa-lookup -h</kbd> により確認できます.
|
197
|
+
</p>
|
198
|
+
</div><!-- subsection -->
|
199
|
+
<div class="subsection">
|
200
|
+
<h3><a name="marisa-reverse-lookup">marisa-reverse-lookup</a></h3>
|
201
|
+
<div class="float">
|
202
|
+
<pre class="console">$ marisa-reverse-lookup keyset.dic
|
203
|
+
800000
|
204
|
+
800000 紀元前475年</pre>
|
205
|
+
</div><!-- float -->
|
206
|
+
<p>
|
207
|
+
<kbd>marisa-reverse-lookup</kbd> は辞書に登録されている文字列を ID から復元するツールです.登録文字列には <var>0</var> から順に固有の ID が割り当てられるので,指定できる値は <var>0</var> 以上で<var>登録文字列数</var>より小さい整数となります.範囲外の値を入力するとエラーになるので注意してください.
|
208
|
+
</p>
|
209
|
+
<p>
|
210
|
+
オプションの一覧は <kbd>marisa-reverse-lookup -h</kbd> により確認できます.
|
211
|
+
</p>
|
212
|
+
</div><!-- subsection -->
|
213
|
+
<div class="subsection">
|
214
|
+
<h3><a name="marisa-common-prefix-search">marisa-common-prefix-search</a></h3>
|
215
|
+
<div class="float">
|
216
|
+
<pre class="console">$ marisa-common-prefix-search keyset.dic
|
217
|
+
東方
|
218
|
+
2 found
|
219
|
+
3542 東 東方
|
220
|
+
174385 東方 東方</pre>
|
221
|
+
</div><!-- float -->
|
222
|
+
<p>
|
223
|
+
<kbd>marisa-common-prefix-search</kbd> は Common Prefix Search をおこなうツールです.入力された文字列の前半部分に一致する登録文字列を ID とともに出力します.
|
224
|
+
</p>
|
225
|
+
<p>
|
226
|
+
オプションの一覧は <kbd>marisa-common-prefix-search -h</kbd> により確認できます.
|
227
|
+
</p>
|
228
|
+
</div><!-- subsection -->
|
229
|
+
<div class="subsection">
|
230
|
+
<h3><a name="marisa-predictive-search">marisa-predictive-search</a></h3>
|
231
|
+
<div class="float">
|
232
|
+
<pre class="console">$ marisa-predictive-search keyset.dic -n 2
|
233
|
+
東方
|
234
|
+
200 found
|
235
|
+
174385 東方 東方
|
236
|
+
639679 東方文花帖 東方</pre>
|
237
|
+
</div><!-- float -->
|
238
|
+
<p>
|
239
|
+
<kbd>marisa-predictive-search</kbd> は Predictive Search をおこなうツールです.入力された文字列で始まる登録文字列を ID とともに出力します.
|
240
|
+
</p>
|
241
|
+
<p>
|
242
|
+
オプションの一覧は <kbd>marisa-predictive-search -h</kbd> により確認できます.
|
243
|
+
</p>
|
244
|
+
</div><!-- subsection -->
|
245
|
+
<div class="subsection">
|
246
|
+
<h3><a name="marisa-benchmark">marisa-benchmark</a></h3>
|
247
|
+
<div class="float">
|
248
|
+
<pre class="console">$ marisa-benchmark keyset.txt
|
249
|
+
Number of tries: 1 - 5
|
250
|
+
TAIL mode: Text mode
|
251
|
+
Node order: Descending weight order
|
252
|
+
Cache level: Normal cache
|
253
|
+
Number of keys: 1342099
|
254
|
+
Total length: 28308027
|
255
|
+
------+----------+--------+--------+--------+--------+--------
|
256
|
+
#tries size build lookup reverse prefix predict
|
257
|
+
lookup search search
|
258
|
+
[bytes] [K/s] [K/s] [K/s] [K/s] [K/s]
|
259
|
+
------+----------+--------+--------+--------+--------+--------
|
260
|
+
1 11588904 506.45 1187.70 1109.17 1040.39 596.49
|
261
|
+
2 8467920 424.71 699.01 677.83 636.07 300.25
|
262
|
+
3 7841664 405.47 615.64 601.84 563.91 254.67
|
263
|
+
4 7633584 399.43 593.85 583.52 545.57 242.69
|
264
|
+
5 7548584 395.90 526.31 563.91 504.55 236.70
|
265
|
+
------+----------+--------+--------+--------+--------+--------</pre>
|
266
|
+
</div><!-- float -->
|
267
|
+
<p>
|
268
|
+
<kbd>marisa-benchmark</kbd> は,<kbd>marisa-build</kbd> と同様の入力を受け取り,辞書のサイズや構築・検索にかかる時間を調べるツールです.辞書を構成する Trie の数を選択するのに有用です.
|
269
|
+
</p>
|
270
|
+
<p>
|
271
|
+
検索時間については,入力された文字列を一通り検索するのに要した時間を <code>std::clock()</code> で計測した結果を出力します.文字列を整列してから入力とした場合はキャッシュが効きやすい状況での検索時間になり,文字列をランダムに並べ替えてから入力とした場合はキャッシュが効きにくい状況での検索時間になります.
|
272
|
+
</p>
|
273
|
+
<p>
|
274
|
+
オプションの一覧は <kbd>marisa-benchmark -h</kbd> により確認できます.
|
275
|
+
</p>
|
276
|
+
</div><!-- subsection -->
|
277
|
+
<div class="subsection">
|
278
|
+
<h3><a name="marisa-dump">marisa-dump</a></h3>
|
279
|
+
<div class="float">
|
280
|
+
<pre class="console">$ marisa-dump keyset.dic | head -3
|
281
|
+
input: keyset.dic
|
282
|
+
フ
|
283
|
+
ファ
|
284
|
+
ファン</pre>
|
285
|
+
</div><!-- float -->
|
286
|
+
<p>
|
287
|
+
<kbd>marisa-dump</kbd> は辞書に登録されている文字列をすべて出力するツールです.デフォルトの区切り文字は改行(LF)ですが,オプションにより変更することができます.
|
288
|
+
</p>
|
289
|
+
<p>
|
290
|
+
オプションの一覧は <kbd>marisa-dump -h</kbd> により確認できます.
|
291
|
+
</p>
|
292
|
+
</div><!-- subsection -->
|
293
|
+
</div><!-- section -->
|
294
|
+
|
295
|
+
<div class="section">
|
296
|
+
<h2><a name="library">ライブラリ</a></h2>
|
297
|
+
<div class="subsection">
|
298
|
+
<h3><a name="howto">使い方</a></h3>
|
299
|
+
<div class="float">
|
300
|
+
<pre class="code">// sample.cc
|
301
|
+
#include <iostream>
|
302
|
+
#include <marisa.h>
|
303
|
+
|
304
|
+
int main() {
|
305
|
+
marisa::Keyset keyset;
|
306
|
+
keyset.push_back("a");
|
307
|
+
keyset.push_back("app");
|
308
|
+
keyset.push_back("apple");
|
309
|
+
|
310
|
+
marisa::Trie trie;
|
311
|
+
trie.build(keyset);
|
312
|
+
|
313
|
+
marisa::Agent agent;
|
314
|
+
agent.set_query("apple");
|
315
|
+
while (trie.common_prefix_search(agent)) {
|
316
|
+
std::cout.write(agent.key().ptr(), agent.key().length());
|
317
|
+
std::cout << ": " << agent.key().id() << std::endl;
|
318
|
+
}
|
319
|
+
return 0;
|
320
|
+
}</pre>
|
321
|
+
</div><!-- float -->
|
322
|
+
<div class="float">
|
323
|
+
<pre class="console">$ g++ sample.cc -lmarisa
|
324
|
+
$ ./a.out
|
325
|
+
a: 0
|
326
|
+
app: 1
|
327
|
+
apple: 2</pre>
|
328
|
+
</div><!-- float -->
|
329
|
+
<p>
|
330
|
+
libmarisa のヘッダは <kbd>marisa.h</kbd> です.名前空間には <code>marisa</code> を使っています.危険なので,<code>using namespace marisa</code> とするのは避けてください.最後に,<kbd>gcc</kbd> や <kbd>clang</kbd> によるリンクでは <kbd>-lmarisa</kbd> が必要となることに注意してください.
|
331
|
+
</p>
|
332
|
+
<p>
|
333
|
+
libmarisa の主要なクラスは <a href="#keyset">Keyset</a>, <a href="#agent">Agent</a>, <a href="#trie">Trie</a> の 3 つです.サンプルコードでは明示的に使っていませんが,例外のクラスとして <a href="#exception">Exception</a> があるほか,<code>Keyset</code>, <code>Agent</code> のメンバとして <a href="#key">Key</a> や <a href="#query">Query</a> が存在します.
|
334
|
+
</p>
|
335
|
+
<ul>
|
336
|
+
<li><code>Keyset</code>: 文字列の集合を格納するクラスです.辞書を構築するときの入力として使うほか,検索結果の保存にも利用できます.</li>
|
337
|
+
<li><code>Agent</code>: 検索の入出力と途中経過を格納するクラスです.検索用の関数はすべて Agent への参照を引数とします.</li>
|
338
|
+
<li><code>Trie</code>: 辞書のクラスです.</li>
|
339
|
+
</ul>
|
340
|
+
<p>
|
341
|
+
コマンドラインツールのソースコードが <kbd>tools/</kbd> にあり,例外処理やファイル入出力,Predictive Search などのサンプルとして利用できます.
|
342
|
+
</p>
|
343
|
+
</div><!-- subsection -->
|
344
|
+
|
345
|
+
<div class="subsection">
|
346
|
+
<h3><a name="enum">定数</a></h3>
|
347
|
+
<div class="subsubsection">
|
348
|
+
<h4>エラーコード</h4>
|
349
|
+
<div class="float">
|
350
|
+
<pre class="code">typedef enum marisa_error_code_ {
|
351
|
+
MARISA_OK = 0,
|
352
|
+
MARISA_STATE_ERROR = 1,
|
353
|
+
MARISA_NULL_ERROR = 2,
|
354
|
+
MARISA_BOUND_ERROR = 3,
|
355
|
+
MARISA_RANGE_ERROR = 4,
|
356
|
+
MARISA_CODE_ERROR = 5,
|
357
|
+
MARISA_RESET_ERROR = 6,
|
358
|
+
MARISA_SIZE_ERROR = 7,
|
359
|
+
MARISA_MEMORY_ERROR = 8,
|
360
|
+
MARISA_IO_ERROR = 9,
|
361
|
+
MARISA_FORMAT_ERROR = 10,
|
362
|
+
} marisa_error_code;</pre>
|
363
|
+
</div><!-- float -->
|
364
|
+
<p>
|
365
|
+
libmarisa では,ファイル入出力に失敗したときや辞書のサイズが上限に到達したときなどに,<code>Exception</code> を送出します.そして,<code>Exception</code> に格納される情報の 1 つが <code>marisa_error_code</code> です.
|
366
|
+
</p>
|
367
|
+
<p>
|
368
|
+
辞書の入出力に関するエラーコードである <var>MARISA_IO_ERROR</var> と <var>MARISA_FORMAT_ERROR</var> 以外については,バグによる可能性が高いと思います.各エラーコードの詳細については,<kbd>marisa/base.h</kbd> をご覧ください.
|
369
|
+
</p>
|
370
|
+
</div><!-- subsubsection -->
|
371
|
+
<div class="subsubsection">
|
372
|
+
<h4>トライの数</h4>
|
373
|
+
<div class="float">
|
374
|
+
<pre class="code">
|
375
|
+
typedef enum marisa_num_tries_ {
|
376
|
+
MARISA_MIN_NUM_TRIES = 0x00001,
|
377
|
+
MARISA_MAX_NUM_TRIES = 0x0007F,
|
378
|
+
MARISA_DEFAULT_NUM_TRIES = 0x00003,
|
379
|
+
} marisa_num_tries;</pre>
|
380
|
+
</div><!-- float -->
|
381
|
+
<p>
|
382
|
+
MARISA は複数の Patricia Trie を組み合わせて 1 つの Trie を構成することが特徴の 1 つであり,Patricia Trie の数を増やすほど,辞書はコンパクトになるものの,検索が遅くなるという傾向があります.<code>marisa_num_tries</code> では,辞書を構成する Patricia Trie の数について,最小値・最大値とデフォルトの値を提供します.
|
383
|
+
</p>
|
384
|
+
<p>
|
385
|
+
適切な設定は登録文字列やアプリケーションによって異なります.ほとんどの場合はデフォルトの設定で問題ないと思いますが,検索時間が問題になるときは,思い切って <var>1</var> にしてください.また,登録文字列が長くて少し複雑な構成になる場合,デフォルトより大きな値にすることで,辞書のサイズをさらに小さくできることがあります.設定が気になるときは,<kbd>marisa-benchmark</kbd> をお試しください.
|
386
|
+
</p>
|
387
|
+
</div><!-- subsubsection -->
|
388
|
+
<div class="subsubsection">
|
389
|
+
<h4>キャッシュのサイズ</h4>
|
390
|
+
<div class="float">
|
391
|
+
<pre class="code">typedef enum marisa_cache_level_ {
|
392
|
+
MARISA_HUGE_CACHE = 0x00080,
|
393
|
+
MARISA_LARGE_CACHE = 0x00100,
|
394
|
+
MARISA_NORMAL_CACHE = 0x00200,
|
395
|
+
MARISA_SMALL_CACHE = 0x00400,
|
396
|
+
MARISA_TINY_CACHE = 0x00800,
|
397
|
+
MARISA_DEFAULT_CACHE = MARISA_NORMAL_CACHE
|
398
|
+
} marisa_cache_level;</pre>
|
399
|
+
</div><!-- float -->
|
400
|
+
<p>
|
401
|
+
libmarisa では,検索時間の短縮を目的として,辞書にキャッシュを埋め込むようになっています.キャッシュの内容は通過する確率の高い遷移に関する情報であり,キャッシュを大きくすることによって,辞書は大きくなるものの,検索時間を短縮できます.
|
402
|
+
</p>
|
403
|
+
<p>
|
404
|
+
<code>marisa_cache_level</code> は,キャッシュのサイズを制御するための定数を提供します.<var>MARISA_NORMAL_CACHE</var> を基準として,<var>MARISA_LARGE_CACHE</var> は 2 倍,<var>MARISA_HUGE_CACHE</var> は 4 倍になり,<var>MARISA_SMALL_CACHE</var> は 1/2,<var>MARISA_TINY_CACHE</var> は 1/4 になります.
|
405
|
+
</p>
|
406
|
+
</div><!-- subsubsection -->
|
407
|
+
<div class="subsubsection">
|
408
|
+
<h4>TAIL の種類</h4>
|
409
|
+
<div class="float">
|
410
|
+
<pre class="code">typedef enum marisa_tail_mode_ {
|
411
|
+
MARISA_TEXT_TAIL = 0x01000,
|
412
|
+
MARISA_BINARY_TAIL = 0x02000,
|
413
|
+
MARISA_DEFAULT_TAIL = MARISA_TEXT_TAIL,
|
414
|
+
} marisa_tail_mode;</pre>
|
415
|
+
</div><!-- float -->
|
416
|
+
<p>
|
417
|
+
libmarisa による辞書では,最後の Patiricia Trie について,ラベルをそのまま保存するようになっています.<code>marisa_tail_mode</code> はラベルの保存方法を選ぶためのパラメータです.
|
418
|
+
</p>
|
419
|
+
<p>
|
420
|
+
<var>MARISA_TEXT_TAIL</var> はラベルを <var>'\0'</var> を終端とする文字列として保存します.そのため,ラベルに <var>'\0'</var> が含まれるときは,自動的に <var>MARISA_BINARY_TAIL</var> へと切り替わるようになっています.明示的に <var>MARISA_BINARY_TAIL</var> を選ぶ理由はほとんどありません.
|
421
|
+
</p>
|
422
|
+
<p>
|
423
|
+
一方,<var>MARISA_BINARY_TAIL</var> では,ラベルの終端を検出するために,<var>'\0'</var> の代わりにビット列を使用します.そのため,ラベルの平均長が <var>8 bytes</var> を超えるときは <var>MARISA_TEXT_TAIL</var> の方がコンパクトになります.
|
424
|
+
</p>
|
425
|
+
</div><!-- subsubsection -->
|
426
|
+
<div class="subsubsection">
|
427
|
+
<h4>ノードの順序</h4>
|
428
|
+
<div class="float">
|
429
|
+
<pre class="code">typedef enum marisa_node_order_ {
|
430
|
+
MARISA_LABEL_ORDER = 0x10000,
|
431
|
+
MARISA_WEIGHT_ORDER = 0x20000,
|
432
|
+
MARISA_DEFAULT_ORDER = MARISA_WEIGHT_ORDER,
|
433
|
+
} marisa_node_order;</pre>
|
434
|
+
</div><!-- float -->
|
435
|
+
<p>
|
436
|
+
libmarisa では,ノードの順序が辞書のパラメータになっています.選択肢は <var>MARISA_LABEL_ORDER</var> と <var>MARISA_WEIGHT_ORDER</var> の 2 つであり,前者はラベルが昇順になるようにノードを配列し,後者は重み(出現しやすさ)が降順になるようにノードを配列します.一般的な Trie の実装では <var>MARISA_LABEL_ORDER</var> の順序を用いますが,libmarisa では <var>MARISA_WEIGHT_ORDER</var> がデフォルトになっています.
|
437
|
+
</p>
|
438
|
+
<p>
|
439
|
+
<var>MARISA_WEIGHT_ORDER</var> の目的は,出現しやすいノードから順に並べておくことにより,線形探索の効率を高め,検索時間を短縮することにあります.日本語の単語やフレーズを用いた実験においては,辞書引きにかかる時間を 1/2 程度に短縮できることが確認されています.一方,<var>MARISA_LABEL_ORDER</var> については,検索時間は長くなるものの,Predictive Search の検索結果が文字列昇順になるという特徴があります.
|
440
|
+
</p>
|
441
|
+
</div><!-- subsubsection -->
|
442
|
+
<div class="subsubsection">
|
443
|
+
<h4>別名</h4>
|
444
|
+
<div class="float">
|
445
|
+
<pre class="code">namespace marisa {
|
446
|
+
typedef ::marisa_error_code ErrorCode;
|
447
|
+
typedef ::marisa_cache_level CacheLevel;
|
448
|
+
typedef ::marisa_tail_mode TailMode;
|
449
|
+
typedef ::marisa_node_order NodeOrder;
|
450
|
+
} // namespace marisa</pre>
|
451
|
+
</div><!-- float -->
|
452
|
+
<p>
|
453
|
+
以上の列挙型については,マクロとの衝突を避けるために,グローバル名前空間にて定義しています.<code>namespace marisa</code> に別名を用意しているので,お好きな方をご利用ください.
|
454
|
+
</p>
|
455
|
+
</div><!-- subsubsection -->
|
456
|
+
</div><!-- subsection -->
|
457
|
+
|
458
|
+
<div class="subsection">
|
459
|
+
<h3><a name="exception">class Exception</a></h3>
|
460
|
+
<div class="float">
|
461
|
+
<pre class="code">class Exception {
|
462
|
+
public:
|
463
|
+
const char *filename() const;
|
464
|
+
int line() const;
|
465
|
+
ErrorCode error_code() const;
|
466
|
+
const char *error_message() const;
|
467
|
+
|
468
|
+
const char *what() const;
|
469
|
+
};</pre>
|
470
|
+
</div><!-- float -->
|
471
|
+
<p>
|
472
|
+
<code>Exception</code> は libmarisa が例外として送出するクラスです.エラーが検出されたファイルの名前(<code>__FILE__</code>)と行番号(<code>__LINE__</code>),さらにエラーコードを取り出せるようになっています.<code>what()</code> は使いやすさのために用意した関数であり,<code>error_message()</code> と同じく,<var>__FILE__:__LINE__: error_code: error_message</var> という書式の文字列を返します.
|
473
|
+
</p>
|
474
|
+
</div><!-- subsection -->
|
475
|
+
|
476
|
+
<div class="subsection">
|
477
|
+
<h3><a name="key">class Key</a></h3>
|
478
|
+
<div class="float">
|
479
|
+
<pre class="code">class Key {
|
480
|
+
public:
|
481
|
+
char operator[](std::size_t i) const;
|
482
|
+
const char *ptr() const;
|
483
|
+
std::size_t length() const;
|
484
|
+
std::size_t id() const;
|
485
|
+
};</pre>
|
486
|
+
</div><!-- float -->
|
487
|
+
<p>
|
488
|
+
<code>Key</code> は後述する <a href="#keyset">Keyset</a> および <a href="#agent">Agent</a> のメンバになっているクラスです.登録しようとしている文字列や,検索で見つけた登録文字列の情報を格納するために使われています.基本的な使い方では,既に情報が格納されたインスタンスのみを目にすることになります.
|
489
|
+
</p>
|
490
|
+
</div><!-- subsection -->
|
491
|
+
|
492
|
+
<div class="subsection">
|
493
|
+
<h3><a name="query">class Query</a></h3>
|
494
|
+
<div class="float">
|
495
|
+
<pre class="code">class Query {
|
496
|
+
public:
|
497
|
+
char operator[](std::size_t i) const;
|
498
|
+
const char *ptr() const;
|
499
|
+
std::size_t length() const;
|
500
|
+
std::size_t id() const;
|
501
|
+
};</pre>
|
502
|
+
</div><!-- float -->
|
503
|
+
<p>
|
504
|
+
<code>Query</code> は後述する <a href="#agent">Agent</a> のメンバになっているクラスです.検索しようとしている文字列や参照したい登録文字列の ID を格納するようになっています.<code>Query</code> に対する文字列や ID の設定は <code>Agent</code> を介しておこなうため,基本的な使い方では,意識する必要はありません.内容を確認したいときに参照する程度です.
|
505
|
+
</p>
|
506
|
+
</div><!-- subsection -->
|
507
|
+
|
508
|
+
<div class="subsection">
|
509
|
+
<h3><a name="keyset">class Keyset</a></h3>
|
510
|
+
<div class="float">
|
511
|
+
<pre class="code">class Keyset {
|
512
|
+
public:
|
513
|
+
Keyset();
|
514
|
+
|
515
|
+
void push_back(const Key &key);
|
516
|
+
void push_back(const Key &key, char end_marker);
|
517
|
+
|
518
|
+
void push_back(const char *str);
|
519
|
+
void push_back(const char *ptr,
|
520
|
+
std::size_t length,
|
521
|
+
float weight = 1.0);
|
522
|
+
|
523
|
+
const Key &operator[](std::size_t i) const;
|
524
|
+
Key &operator[](std::size_t i);
|
525
|
+
|
526
|
+
std::size_t num_keys();
|
527
|
+
|
528
|
+
bool empty() const;
|
529
|
+
std::size_t size() const;
|
530
|
+
std::size_t total_length() const;
|
531
|
+
|
532
|
+
void reset();
|
533
|
+
|
534
|
+
void clear();
|
535
|
+
void swap(Keyset &rhs);
|
536
|
+
};</pre>
|
537
|
+
</div><!-- float -->
|
538
|
+
<div class="subsubsection">
|
539
|
+
<h4>概要</h4>
|
540
|
+
<p>
|
541
|
+
<code>Keyset</code> は辞書に登録しようとしている文字列もしくは登録されている文字列を詰め込むためのクラスです.辞書を構築するときの入力として,あるいは検索結果を保存しておくために使います.
|
542
|
+
</p>
|
543
|
+
</div><!-- subsubsection -->
|
544
|
+
<div class="subsubsection">
|
545
|
+
<h4>辞書の構築</h4>
|
546
|
+
<p>
|
547
|
+
辞書の構築に使う場合,<code>push_back()</code> で登録したい文字列を追加してから,後述する <a href="#trie">Trie</a> の <code>build()</code> に渡します.<var>weight</var> は文字列の出現しやすさを示す重みであり,同じ文字列を繰り返し追加した場合,辞書を構築する段階で加算されるようになっています.
|
548
|
+
</p>
|
549
|
+
<p>
|
550
|
+
辞書を構築した後は,<code>operator[]()</code> により登録文字列の ID を確認できます.その代わり,<code>Key</code> は重みと ID を共用体のメンバとして持つため,辞書の構築に使用した重みを参照できなくなります.
|
551
|
+
</p>
|
552
|
+
</div><!-- subsubsection -->
|
553
|
+
<div class="subsubsection">
|
554
|
+
<h4>検索結果の保存</h4>
|
555
|
+
<p>
|
556
|
+
検索結果の保存に使う場合,後述する <a href="#agent">Agent</a> に格納されている検索結果を <code>push_back()</code> に渡すことで,文字列を複製し,ID を残しておくことができます.検索結果の文字列に終端記号を加えたいときは <var>end_marker</var> を利用してください.文字列の長さには影響を与えず,<var>end_marker</var> を終端文字として加えることができます.
|
557
|
+
</p>
|
558
|
+
<p>
|
559
|
+
検索結果を破棄して別の検索結果を保存するために再利用するという場合,<code>clear()</code> の代わりに <code>reset()</code> を使うことで,既に確保している領域を再利用できます.メモリの確保・解放にかかるオーバーヘッドが気になるときにご利用ください.
|
560
|
+
</p>
|
561
|
+
<p>
|
562
|
+
<code>empty()</code> は文字列が格納されていないかどうかを返す関数です.<code>size()</code> は <code>num_keys()</code> と同じく格納されている文字列の数を返す関数であり,<code>total_length()</code> は格納されている文字列の合計長を byte 単位で返す関数です.
|
563
|
+
</p>
|
564
|
+
</div><!-- subsubsection -->
|
565
|
+
</div><!-- subsection -->
|
566
|
+
|
567
|
+
<div class="subsection">
|
568
|
+
<h3><a name="agent">class Agent</a></h3>
|
569
|
+
<div class="float">
|
570
|
+
<pre class="code">class Agent {
|
571
|
+
public:
|
572
|
+
Agent();
|
573
|
+
|
574
|
+
const Query &query() const;
|
575
|
+
const Key &key() const;
|
576
|
+
|
577
|
+
void set_query(const char *str);
|
578
|
+
void set_query(const char *ptr,
|
579
|
+
std::size_t length);
|
580
|
+
void set_query(std::size_t key_id);
|
581
|
+
};</pre>
|
582
|
+
</div><!-- float -->
|
583
|
+
<p>
|
584
|
+
<code>Agent</code> は <code>Query</code> と <code>Key</code> をメンバとして持つクラスです.検索における入出力の受け渡し,および途中経過の保存に使います.後述する <a href="#trie">Trie</a> の検索関数は,例外なく <code>Agent</code> への参照を引数として受け取るようになっています.
|
585
|
+
</p>
|
586
|
+
<p>
|
587
|
+
検索の手順は,<code>set_query()</code> を使って検索したい文字列もしくは参照したい登録文字列の ID を設定し,<code>Trie</code> の関数に渡した後,<code>key()</code> により検索結果を取り出すという流れになります.
|
588
|
+
</p>
|
589
|
+
</div><!-- subsection -->
|
590
|
+
|
591
|
+
<div class="subsection">
|
592
|
+
<h3><a name="trie">class Trie</a></h3>
|
593
|
+
<div class="float">
|
594
|
+
<pre class="code">class Trie {
|
595
|
+
public:
|
596
|
+
Trie();
|
597
|
+
|
598
|
+
void build(Keyset &keyset,
|
599
|
+
int config_flags = 0);
|
600
|
+
|
601
|
+
void mmap(const char *filename);
|
602
|
+
void map(const void *ptr,
|
603
|
+
std::size_t size);
|
604
|
+
|
605
|
+
void load(const char *filename);
|
606
|
+
void read(int fd);
|
607
|
+
|
608
|
+
void save(const char *filename) const;
|
609
|
+
void write(int fd) const;
|
610
|
+
|
611
|
+
bool lookup(Agent &agent) const;
|
612
|
+
void reverse_lookup(Agent &agent) const;
|
613
|
+
bool common_prefix_search(Agent &agent) const;
|
614
|
+
bool predictive_search(Agent &agent) const;
|
615
|
+
|
616
|
+
std::size_t num_tries() const;
|
617
|
+
std::size_t num_keys() const;
|
618
|
+
std::size_t num_nodes() const;
|
619
|
+
|
620
|
+
TailMode tail_mode() const;
|
621
|
+
NodeOrder node_order() const;
|
622
|
+
|
623
|
+
bool empty() const;
|
624
|
+
std::size_t size() const;
|
625
|
+
std::size_t io_size() const;
|
626
|
+
|
627
|
+
void clear();
|
628
|
+
void swap(Trie &rhs);
|
629
|
+
};</pre>
|
630
|
+
</div><!-- float -->
|
631
|
+
<div class="subsubsection">
|
632
|
+
<h4>概要</h4>
|
633
|
+
<p>
|
634
|
+
<code>Trie</code> は辞書のクラスです.libmarisa において最も重要なクラスであり,辞書の構築・検索からファイル入出力にいたるまで,あらゆる操作に必要となります.
|
635
|
+
</p>
|
636
|
+
<p>
|
637
|
+
実際には,辞書のハンドルに相当するクラスであり,辞書の実体がない状況では,<code>build()</code>, <code>mmap()</code>, <code>map()</code>, <code>load()</code>, <code>read()</code>, <code>clear()</code>, <code>swap()</code> 以外の関数を呼び出すと例外が送出されます.
|
638
|
+
</p>
|
639
|
+
</div><!-- subsubsection -->
|
640
|
+
<div class="subsubsection">
|
641
|
+
<h4>辞書の構築</h4>
|
642
|
+
<p>
|
643
|
+
辞書の構築には <code>build()</code> を使います.引数は,前述の <a href="#keyset">Keyset</a> と,辞書の設定を XOR(<code>|</code>) で組み合わせた <var>config_flags</var> です.<var>config_flags</var> については,<var>2 | MARISA_BINARY_TAIL</var> のように指定します.この例では,辞書を構成する Patricia Trie の数を <var>2</var> つに制限し,ラベルの保存方法を <var>MARISA_BINARY_TAIL</var> に固定します.省略されているノードの順序とキャッシュのサイズについては,デフォルトの設定である <var>MARISA_DEFAULT_ORDER</var> と <var>MARISA_DEFAULT_CACHE</var> が採用されます.
|
644
|
+
</p>
|
645
|
+
<p>
|
646
|
+
辞書の構築において登録文字列に割り当てられた ID は,<var>keyset</var> の <code>operator[]()</code> を使って確認できます.登録文字列に対して関連付ける情報がある場合にご利用ください.
|
647
|
+
</p>
|
648
|
+
</div><!-- subsubsection -->
|
649
|
+
<div class="subsubsection">
|
650
|
+
<h4>ファイル入出力</h4>
|
651
|
+
<p>
|
652
|
+
<code>mmap()</code> は,Memory Mapped I/O により,辞書全体をファイルから読み込むことなく検索できる状態にする関数です.少ししか検索しないのに辞書全体を読み込むのは勿体ないという状況や,異なるプロセスで同じ辞書を共有したいという状況で使うと効果的です.逆に,たくさんの文字列を検索する場合,あらかじめ辞書全体を読み込んでおかないと,外部記憶に対するランダムアクセスにより検索時間が極端に長くなる可能性があります.
|
653
|
+
</p>
|
654
|
+
<p>
|
655
|
+
<code>map()</code> はメモリ上に展開されている辞書のバイナリを使って検索できる状態にする関数です.<code>load()</code> と <code>read()</code> は辞書を入力する関数であり,<code>save()</code> と <code>write()</code> は辞書を出力する関数です.
|
656
|
+
</p>
|
657
|
+
</div><!-- subsubsection -->
|
658
|
+
<div class="subsubsection">
|
659
|
+
<h4>辞書からの検索</h4>
|
660
|
+
<p>
|
661
|
+
検索をおこなう関数は <code>lookup()</code>, <code>reverse_lookup()</code>, <code>common_prefix_search()</code>, <code>predictive_search()</code> の 4 種類です.
|
662
|
+
</p>
|
663
|
+
<ul>
|
664
|
+
<li>
|
665
|
+
<code>lookup()</code>: 文字列が登録されているかどうかを確認します.登録されていれば <var>true</var> を返します.このとき,<code>agent.key()</code> により検索結果を取り出すことができます.ただし,<code>agent.key().ptr()</code> については,入力として渡された文字列を指しているだけであり,文字列の複製を持っているわけではないことに注意してください.登録されていなければ <var>false</var> を返して終了です.
|
666
|
+
</li>
|
667
|
+
<li>
|
668
|
+
<code>reverse_lookup()</code>: ID から登録文字列を復元します.返り値はなく,復元された文字列は <var>agent.key()</var> を介してアクセスできます.文字列の実体は <var>agent</var> 内部に保持されています.<var>agent</var> を使って次の検索をおこなった段階で失われるものと考えてください.ID が範囲外であれば例外を送出して終了です.
|
669
|
+
</li>
|
670
|
+
<li>
|
671
|
+
<code>common_prefix_search()</code>: 入力文字列の前半部分に一致する登録文字列を検索します.該当する登録文字列があれば <var>true</var> を返します.このとき,<code>agent.key()</code> には検索結果が格納されています.<code>agent.key().ptr() == agent.query().ptr()</code> が成立することに注意してください.該当する登録文字列が複数ある場合,返り値が <var>false</var> になるまで繰り返し <code>common_prefix_search()</code> を呼び出すことにより,すべての検索結果を取得できます.
|
672
|
+
</li>
|
673
|
+
<li>
|
674
|
+
<code>predictive_search()</code>: 入力文字列で始まる登録文字列を検索します.該当する登録文字列があれば <var>true</var> を返します.検索によって復元された文字列には,<code>agent.key()</code> を介してアクセスできます.文字列の実体は,<var>agent</var> 内部に検索の途中経過として保持されているので,<var>agent</var> を使って次の検索をおこなった段階で失われるものと考えてください.該当する登録文字列が複数ある場合,返り値が <var>false</var> になるまで繰り返し <code>predictive_search()</code> を呼び出すことにより,すべての検索結果を取得できます.
|
675
|
+
</li>
|
676
|
+
</ul>
|
677
|
+
<p>
|
678
|
+
繰り返しにより検索が進行する <code>common_prefix_search()</code> と <code>predictive_search()</code> については,<code>agent</code> が検索の途中経過を保持するようになっています.そのため,<code>agent</code> を別の関数に渡したり,<code>agent.set_query()</code> を呼び出したりした時点で,検索の進行はリセットされます.
|
679
|
+
</p>
|
680
|
+
<p>
|
681
|
+
<code>empty()</code> は登録文字列が存在するかどうかを返す関数です.<code>size()</code> は <code>num_keys()</code> と同じく登録文字列の数を返す関数であり,<code>io_size()</code> は辞書をファイルに出力した場合のサイズを返す関数です.
|
682
|
+
</p>
|
683
|
+
</div><!-- subsubsection -->
|
684
|
+
</div><!-- subsection -->
|
685
|
+
|
686
|
+
<div class="subsection">
|
687
|
+
<h3><a name="stdio">stdio</a></h3>
|
688
|
+
<div class="float">
|
689
|
+
<pre class="code">void fread(std::FILE *file, Trie *trie);
|
690
|
+
void fwrite(std::FILE *file, const Trie &trie);</pre>
|
691
|
+
</div><!-- float -->
|
692
|
+
<p>
|
693
|
+
<code>std::FILE</code> を用いる関数は <kbd>marisa/stdio.h</kbd> で宣言されています.<code>#include <cstdio></code> を入れたくないときは,<kbd>marisa.h</kbd> の代わりに <kbd>marisa/trie.h</kbd> を使ってください.
|
694
|
+
</p>
|
695
|
+
</div><!-- subsection -->
|
696
|
+
|
697
|
+
<div class="subsection">
|
698
|
+
<h3><a name="iostream">iostream</a></h3>
|
699
|
+
<div class="float">
|
700
|
+
<pre class="code">std::istream &read(std::istream &stream, Trie *trie);
|
701
|
+
std::ostream &write(std::ostream &stream, const Trie &trie);
|
702
|
+
|
703
|
+
std::istream &operator>>(std::istream &stream, Trie &trie);
|
704
|
+
std::ostream &operator<<(std::ostream &stream, const Trie &trie);</pre>
|
705
|
+
</div><!-- float -->
|
706
|
+
<p>
|
707
|
+
<code>std::iostream</code> を用いる関数は <kbd>marisa/iostream.h</kbd> で宣言されています.<code>#include <iosfwd></code> を入れたくないときは,<kbd>marisa.h</kbd> の代わりに <kbd>marisa/trie.h</kbd> を使ってください.
|
708
|
+
</p>
|
709
|
+
</div><!-- subsection -->
|
710
|
+
</div><!-- section -->
|
711
|
+
|
712
|
+
<div class="section">
|
713
|
+
<h2><a name="compatibility">辞書の互換性</a></h2>
|
714
|
+
<p>
|
715
|
+
libmarisa により構築される辞書の書式はアーキテクチャに依存します.Little Endian な環境で構築した辞書は,Big Endian な環境では使えません.あらためて構築しなおす必要があります.また,Little Endian 形式の辞書は 32/64-bit 環境における互換性があるのに対し,Big Endian 形式の辞書は 32/64-bit 環境における互換性がありません.
|
716
|
+
</p>
|
717
|
+
</div><!-- section -->
|
718
|
+
|
719
|
+
<div class="section">
|
720
|
+
<h2><a name="references">参考資料</a></h2>
|
721
|
+
<ul>
|
722
|
+
<li><a href="http://www.anlp.jp/proceedings/annual_meeting/2011/html/program.html">言語処理学会第 17 回年次大会(NLP2011)</a>
|
723
|
+
<ul>
|
724
|
+
<li>言語処理学会年次大会の予稿集が公開されていて,MARISA に関する論文(<a href="http://www.anlp.jp/proceedings/annual_meeting/2011/pdf_dir/F2-6.pdf">F2-6.pdf</a>)をダウンロードできます.</li>
|
725
|
+
</ul>
|
726
|
+
</li>
|
727
|
+
<li><a href="http://d.hatena.ne.jp/s-yata/20110310/1299777228">発表資料(Prefix/Patricia Trie の入れ子による辞書圧縮)</a>
|
728
|
+
<ul>
|
729
|
+
<li>MARISA に関する発表資料(<a href="http://sites.google.com/site/headdythehero/cabine/2011/0311/NLP2011-F2-6.pptx?attredirects=0&d=1">NLP2011-F2-6.pptx</a>, <a href="http://sites.google.com/site/headdythehero/cabine/2011/0311/NLP2011-F2-6.pdf?attredirects=0&d=1">NLP2011-F2-6.pdf</a>, <a href="http://sites.google.com/site/headdythehero/cabine/2011/0311/NLP2011-F2-6-notes.pdf?attredirects=0&d=1">NLP2011-F2-6-notes.pdf</a>)をダウンロードできます.</li>
|
730
|
+
</ul>
|
731
|
+
</li>
|
732
|
+
</ul>
|
733
|
+
</div><!-- section -->
|
734
|
+
|
735
|
+
<div class="section">
|
736
|
+
<h2><a name="conclusion">おわりに</a></h2>
|
737
|
+
<p>
|
738
|
+
質問などありましたら,欄外のメールアドレス宛てに,お気軽にご連絡ください.
|
739
|
+
</p>
|
740
|
+
</div><!-- section -->
|
741
|
+
</div><!-- body -->
|
742
|
+
<div id="footer">
|
743
|
+
<div class="left">MARISA: Matching Algorithm with Recursively Implemented StorAge</div>
|
744
|
+
<div class="right">
|
745
|
+
moc.liamg@atay.umusus
|
746
|
+
</div>
|
747
|
+
<div class="end"></div>
|
748
|
+
</div><!-- footer -->
|
749
|
+
</body>
|
750
|
+
</html>
|