Debian Codegraph @

plugins/README.md

raw symbols SCIP
1 # OpenCC Plugins
2
3 `plugins/` contains segmentation plugins that are built and distributed
4 separately from the OpenCC core library.
5
6 The current plugin layout is:
7
8 - `plugins/<name>/src/`: plugin implementation and exported entry point
9 - `plugins/<name>/include/`: plugin-private headers
10 - `plugins/<name>/data/config/`: plugin-backed JSON configs
11 - `plugins/<name>/data/<resource-dir>/`: plugin resource files
12 - `plugins/<name>/tests/`: integration tests for built plugin artifacts
13
14 ## Design Rules
15
16 - The OpenCC core keeps built-in algorithms only. `mmseg` remains built in.
17 - Any non-built-in `segmentation.type` is resolved through the plugin host.
18 - A single JSON config must stay platform-neutral. Config files must not embed
19 `.so`, `.dylib`, `.dll`, or platform-specific install paths.
20 - Plugin resources belong to the plugin package, but resource names stay inside
21 the normal OpenCC data layout.
22 - Plugin tests should validate real built artifacts instead of directly testing
23 private implementation classes.
24
25 ## Naming And Installation
26
27 Runtime naming follows the segmentation type:
28
29 - Linux: `libopencc-<type>.so`
30 - macOS: `libopencc-<type>.dylib`
31 - Windows: `opencc-<type>.dll`
32
33 Windows loaders also accept the MSYS/MinGW runtime name
34 `msys-opencc-<type>.dll` when that is the emitted DLL filename.
35 On Windows, plugins must be built with an ABI-compatible toolchain/runtime as
36 the host OpenCC binary. Mixing MSVC-built hosts with MinGW-built plugins, or
37 the reverse, is unsupported.
38
39 For the `jieba` plugin, that means:
40
41 - Linux: `libopencc-jieba.so`
42 - macOS: `libopencc-jieba.dylib`
43 - Windows: `opencc-jieba.dll`
44
45 MSYS/MinGW builds may emit `msys-opencc-jieba.dll`, which is also accepted by
46 the loader.
47
48 CMake installs plugin binaries into the platform plugin directory and installs
49 plugin configs/resources into the OpenCC data directory.
50 Within a single plugin search directory, keep only one DLL for a given
51 segmentation type. On Windows this applies to both `opencc-<type>.dll` and
52 `msys-opencc-<type>.dll`. Multiple matching DLL names for the same type in one
53 search directory are treated as an error.
54
55 ## Resource Resolution
56
57 Plugin JSON uses resource names rather than platform paths. Example:
58
59 ```json
60 {
61 "segmentation": {
62 "type": "jieba",
63 "resources": {
64 "dict_path": "jieba_dict/jieba.dict.utf8",
65 "model_path": "jieba_dict/hmm_model.utf8",
66 "user_dict_path": "jieba_dict/user.dict.utf8"
67 }
68 }
69 }
70 ```
71
72 The core passes these values to the plugin host. The plugin is responsible for
73 resolving them at runtime. Relative resource paths are expected to resolve
74 within the existing OpenCC data layout rather than a plugin-specific ad hoc
75 directory tree.
76
77 For dictionary files referenced by OpenCC configs, embedded applications can
78 compose a single resource provider from the main application, plugin, and
79 system OpenCC data directories:
80
81 ```c++
82 auto resources = std::make_shared<opencc::FilesystemResourceProvider>(
83 std::vector<std::string>{
84 main_app_resource_dir,
85 plugin_resource_dir,
86 system_opencc_data_dir,
87 });
88 opencc::SimpleConverter converter("s2twp_jieba.json", resources);
89 ```
90
91 Plugins only need to tell the host which resource directory they ship. The
92 OpenCC core resolves dictionary resource names through the provider and does
93 not need plugin-specific lookup logic.
94
95 ## Segmentation ABI
96
97 The current segmentation plugin ABI entry point is:
98
99 - `opencc_get_segmentation_plugin_v2()`
100
101 Segmentation results are returned as a sequence of segment lengths measured in
102 Unicode code points, not as copied token strings. The ABI contract is:
103
104 - input text is passed to the plugin as null-terminated UTF-8
105 - the plugin returns `segment_count` plus `codepoint_lengths`
106 - each element is the number of Unicode code points in the next segment
107 - lengths must be positive and must cover the full input, in order
108 - the host reconstructs segment boundaries from the original UTF-8 input
109
110 This keeps the ABI simpler and avoids allocating one string per token across
111 the plugin boundary.
112
113 ### Customizing Jieba Dictionaries
114
115 When using the `jieba` plugin, you can add custom terminology to the segmenter by defining a custom `user.dict.utf8` or editing the installed one.
116
117 Custom dictionaries must be encoded in UTF-8. Each line follows the format: `[Word] [Frequency] [Part-of-Speech]`, separated by spaces. The frequency and POS tags are optional.
118
119 Example:
120 ```text
121 云计算 5 n
122 机器学习 8 n
123 区块链 10 nz
124 ```
125
126 ## Testing
127
128 Each plugin should prefer integration tests that exercise:
129
130 - the built `opencc` command or `libopencc`
131 - the built plugin shared library
132 - real plugin JSON configs
133 - real installed or runfiles-based resource files
134
135 Current `jieba` targets:
136
137 - CMake plugin target: `opencc_jieba`
138 - CMake integration test: `JiebaPluginIntegrationTest`
139 - Bazel plugin target: `//plugins/jieba:opencc-jieba`
140 - Bazel integration test: `//plugins/jieba:jieba_plugin_integration_test`
141
142 ## Adding A New Plugin
143
144 1. Create `plugins/<name>/src`, `include`, `data`, and `tests`.
145 2. Export `opencc_get_segmentation_plugin_v2()`.
146 3. Name the output binary using the `opencc-<type>` convention.
147 4. Keep JSON configs platform-neutral and resource-oriented.
148 5. Add both CMake and Bazel build rules.
149 6. Add an integration test that loads the built plugin through the real host.
150
151 ## Packaging for Distro Maintainers
152
153 To align with downstream Linux distribution packaging standards (e.g., Debian
154 `apt`, Arch `pacman`), OpenCC plugins support decoupled compilation. This lets
155 maintainers build and distribute the core `opencc` system separately from
156 heavier third-party plugins such as `opencc-jieba`.
157
158 ### 1. Build And Install Core OpenCC
159
160 Compile the main tree normally, but disable the optional `jieba` plugin:
161
162 ```bash
163 mkdir build_core && cd build_core
164 cmake .. -DBUILD_OPENCC_JIEBA_PLUGIN=OFF -DCMAKE_INSTALL_PREFIX=/usr
165 make && make install
166 ```
167
168 ### 2. Build The Plugin Standalone
169
170 Plugins can detect standalone builds automatically. Build from the plugin
171 directory and point `OpenCC_DIR` at the installed OpenCC CMake package:
172
173 ```bash
174 cd plugins/jieba
175 mkdir build_plugin && cd build_plugin
176 cmake .. -DOpenCC_DIR=/usr/lib/cmake/opencc -DCMAKE_INSTALL_PREFIX=/usr
177 make && make install
178 ```
179
180 Standalone default installation paths are intended to align with the core
181 OpenCC layout:
182
183 - Windows: `bin/plugins`
184 - Linux/macOS: `lib/opencc/plugins`