MeCab (Japanese Morphological Analyzer) bindings for standalone dart. The Flutter version of this package can be found here
Try it out using flutter in the browser!.
Android | iOS | Windows | MacOS | Linux | Web | Web --wasm |
---|---|---|---|---|---|---|
✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |
- Add this plug in as a dependency in your pubspec.yaml file.
dependencies:
mecab_for_dart: <your_version>
Pre-compiled binaries are provided for Linux (arm64), Macos (x86/arm64) and Windows (x86/arm64), you can download them here.
Download the one you need and place them somwhere your dart application can access them, in this README that path will be called <LIB_MECAB_PATH>
Any dictionary that is compatible with mecab will work, but for ease of getting started, a copy of ipadic and unidic is provided here.
Download the one you need and place them somewhere your dart application can access them, in this README that path will be donated by <DICT_MECAB_PATH>
Init Mecab:
var tagger = new Mecab();
await tagger.init("<LIB_MECAB_PATH>", "<DICT_MECAB_PATH>", true);
Set the boolean option in init
function to true
if you want to get the tokens including features,
set it to false
if you only want the token surfaces.
Use the tagger to parse text:
var tokens = tagger.parse('にわにわにわにわとりがいる。');
var text = '';
for(var token in tokens) {
text += token.surface + "\t";
for(var i = 0; i < token.features.length; i++) {
text += token.features[i];
if(i + 1 < token.features.length) {
text += ",";
}
}
text += "\n";
}
This library tries to load the mecab dictionary from the WASM filesystem. The easiest way to get the dictionary in it, is by bundling it when compiling mecab to wasm. However, it may be desirable to swap dictionaries. To do this, you need to load the dictionary into libmecab's wasm memory.
cd linux/
# this builds for the architecture you are on
make libmecab
cd macos/Classes/
make libmecab
# use rosetta on apple silicone to cross compile
arch -x86_64 make libmecab
Because mecab uses nmake on windows to compile, the mecab DLL needs to be created separately.
For this open a Developer Command Prompt and change in the windows/src
directory.
In this directory execute nmake -f Makefile.x64.msvc
(compile on x86) or nmake -f Makefile.arm64.msvc
(compile on arm64).
After the build process finished, there should be a libmecab.dll
in windows/src
.
As dart is not really useed for running on iOS / Android there are no precompiled binaries or build scripts available. If you see a need for this please open an issue or PR!
On web this plugin uses WASM.
To compile for WASM this project uses Emscripten.
Therefore, to compile a wasm binary, first emscripten needs to be installed.
Then, a WASM binary can be compiled by running compile_wasm_bare.sh
(no dictionary included) or compile_wasm_embed.sh
(ipadic embedded).
This will generate libmecab.js
and libmecab.wasm
in the folder emcc_out/
.
Those files then need to be loaded by your application.
For more details, see the example.