Install nodejieba via npm
masterTo use NodeJieba in your project, install it using npm:
npm install nodejiebarepository·master·Indexed 25 days ago
https://github.com/yanyiwu/nodejiebaA high-performance Node.js implementation of the Jieba Chinese segmentation algorithm powered by a C++ core. It provides functionality for Chinese word segmentation (including default, HMM, full, search engine, and small granularity modes), part-of-speech (POS) tagging, and keyword extraction using standard and TextRank algorithms. It supports dynamic dictionary updates via insertWord() and custom dictionary configuration through load().
To use NodeJieba in your project, install it using npm:
npm install nodejiebaBy default, dictionaries are loaded automatically on the first function call. You can manually trigger loading or provide custom paths for different dictionary types using load(options).
Options for load():
dict: Main dictionary (includes weights and POS tags). Use nodejieba.DEFAULT_DICT for default.hmmDict: Hidden Markov Model dictionary. Use nodejieba.DEFAULT_HMM_DICT for default.userDict: User-defined dictionary path. Use nodejieba.DEFAULT_USER_DICT for default.idfDict: IDF information for keyword extraction. Use nodejieba.DEFAULT_IDF_DICT for default.stopWordDict: Stop words list for keyword extraction. Use nodejieba.DEFAULT_STOP_WORD_DICT for default.// Load only a custom user dictionary
nodejieba.load({
userDict: './path/to/your/userdict.utf8',
});
// Or load everything explicitly
nodejieba.load({
dict: nodejieba.DEFAULT_DICT,
hmmDict: nodejieba.DEFAULT_HMM_DICT,
userDict: './path/to/your/userdict.utf8',
idfDict: nodejieba.DEFAULT_IDF_DICT,
stopWordDict: nodejieba.DEFAULT_STOP_WORD_DICT,
});Use insertWord(word) to dynamically add a new word to the dictionary during runtime so it is treated as a single unit in future segmentation calls.
var nodejieba = require("nodejieba");
nodejieba.insertWord("男默女泪");
console.log(nodejieba.cut("男默女泪"));
// ["男默女泪"]Use tag() to segment text and return each word along with its corresponding part-of-speech tag.
var nodejieba = require("nodejieba");
var result = nodejieba.tag("红掌拨清波");
// Returns: [ { word: '红掌', tag: 'n' }, { word: '拨', tag: 'v' }, { word: '清波', tag: 'n' } ]You can extract the most important keywords from a sentence using two different algorithms:
extract(sentence, topN): Standard keyword extraction.textRankExtract(sentence, topN): Keyword extraction using the TextRank algorithm.NodeJieba provides several segmentation modes:
If you encounter a BINDING_NOT_FOUND error, it means the native C++ binding (nodejieba.node) was not found. This typically happens if install scripts were skipped or the build failed.
Resolution:
--ignore-scripts flag.npm rebuild nodejieba to trigger a rebuild of the native components.Use the load(dictJson) method to configure the dictionary paths used by the segmentation engine. If no argument is provided, it loads the default dictionaries. You can pass an object to specify custom paths for specific dictionary types.
Supported keys in the dictJson object:
dict: Path to the main dictionary file.hmmDict: Path to the HMM model file.userDict: Path to the user dictionary file.idfDict: Path to the IDF dictionary file.stopWordDict: Path to the stop words dictionary file.cutHMM(text) method uses Hidden Markov Model (HMM) to segment words, which is useful for handling out-of-vocabulary words.cutForSearch(text) method segments text in a way that is optimized for search engine indexing.cutAll(text) method performs full mode segmentation, which is more exhaustive and may result in overlapping words.cutSmall(text) method performs a simplified segmentation mode.