Jieba is a popular open-source Python library for Chinese word segmentation, often referred to as 'jieba' (meaning 'to cut' or 'to slice'). It provides various segmentation modes, including dictionary-based, HMM (Hidden Markov Model), and a newer deep learning mode based on PaddlePaddle. The current version is 0.42.1, with an active development cadence, releasing several updates per year to address bugs and add features.
pip install jiebaVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the basic usage of Jieba for Chinese word segmentation, showing the default, full, and search engine modes. It also includes a comment on how to load a custom user dictionary.
Install `paddlepaddle` using `pip install jieba[paddle]`. For performance-critical applications, benchmark Paddle mode against default or HMM-only mode (`jieba.cut(text, HMM=False)`).
Update Jieba to v0.42 or later. Always validate input strings to ensure they are non-empty before passing them to segmentation functions, especially with `use_paddle=True`.
Update Jieba to the latest version (v0.42.1 or newer). For critical applications, manually inspect results for edge cases or consider using the default mode and post-processing.
Update Jieba to v0.40 or later. Always instantiate a `Tokenizer` and call its methods for custom dictionary management if you want isolated behavior. Ensure custom dictionary files are UTF-8 encoded.
Ensure you are in the correct Python environment and install 'jieba' using pip: `pip install jieba` or `pip3 install jieba`.
Rename your Python script file to something other than `jieba.py` (e.g., `my_segmenter.py`) to avoid a naming conflict.
Install `paddlepaddle-tiny` or the full `paddlepaddle` library, ensuring compatibility. For example: `pip install paddlepaddle-tiny==1.6.1` or `pip install paddlepaddle --upgrade`.
When opening the file, explicitly specify the correct encoding, typically 'utf-8', like so: `with open('your_file.txt', 'r', encoding='utf-8') as f:`.Ensure all text files are saved with UTF-8 encoding and explicitly specify `encoding='utf-8'` when opening files or processing strings.