natto-py is a Python package that provides a Foreign Function Interface (FFI) binding to MeCab, the part-of-speech and morphological analyzer for the Japanese language. It allows Python applications to leverage MeCab's capabilities without requiring SWIG or a C compiler for installation. The current version is 1.0.1, and the library is actively maintained with an irregular release cadence.
pip install natto-pyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize `natto-py` with MeCab and perform both string-based and node-based parsing. The `with` statement is used for proper resource management, and MeCab options are set to obtain detailed morphological features for each node.
Upgrade to Python 3.7+ or pin `natto-py==0.9.2` for Python 2 projects.
Ensure MeCab and a system dictionary are installed and accessible on your system's PATH, or explicitly set `os.environ['MECAB_PATH']` and `os.environ['MECAB_CHARSET']` in your Python code before importing `natto`.
Wrap `MeCab` instantiation in a `with` statement: `with MeCab(...) as nm:`.
Pass formatting options like `r'-F%m,%f[0],%f[1] -U?,?,? '` to the `MeCab` constructor and iterate over `MeCabNode` instances using `nm.parse(text, as_nodes=True)`.
Ensure the package is installed using pip: `pip install natto-py`
Review your MeCab instantiation parameters, particularly the `-F` (node-format) and `-U` (unknown-format) options. Ensure your MeCab version supports the requested features and avoid problematic format specifiers. You might need to adjust them, for example, `with MeCab(r'-F%m,%f[0],%h,%f[8] -U?,?,?,?') as nm:`
Install MeCab (v0.996+) and a system dictionary (e.g., IPA, Unidic) on your operating system. If `natto-py` cannot automatically locate it, set the `MECAB_PATH` and `MECAB_CHARSET` environment variables (e.g., `os.environ['MECAB_PATH'] = '/path/to/libmecab.so'` and `os.environ['MECAB_CHARSET'] = 'utf8'`) in your Python code before importing `natto`.