### NodeJieba TypeScript Type Definitions and Usage Source: https://context7.com/yanyiwu/nodejieba/llms.txt Demonstrates the usage of NodeJieba with TypeScript, showcasing type-safe function calls for text segmentation, keyword extraction, and tagging. It highlights the benefits of type definitions for improved developer experience and code safety. ```typescript import nodejieba from "nodejieba"; // All functions are fully typed const sentence: string = "我是拖拉机学院手扶拖拉机专业的。不用多久,我就会升职加薪,当上CEO,走上人生巅峰。"; // Type: string[] const cutResult: string[] = nodejieba.cut(sentence); const cutStrictResult: string[] = nodejieba.cut(sentence, true); const hmmResult: string[] = nodejieba.cutHMM(sentence); const allResult: string[] = nodejieba.cutAll(sentence); const searchResult: string[] = nodejieba.cutForSearch(sentence); const smallResult: string[] = nodejieba.cutSmall("南京市长江大桥", 3); // Type: Array<{word: string, tag: string}> interface TagResult { word: string; tag: string; } const tagResult: TagResult[] = nodejieba.tag(sentence); // Type: Array<{word: string, weight: number}> interface ExtractResult { word: string; weight: number; } const topN: number = 5; const extractResult: ExtractResult[] = nodejieba.extract(sentence, topN); const textRankResult: ExtractResult[] = nodejieba.textRankExtract(sentence, topN); // Type: boolean const inserted: boolean = nodejieba.insertWord("男默女泪"); // Type: void interface LoadOptions { dict?: string; hmmDict?: string; userDict?: string; idfDict?: string; stopWordDict?: string; } const options: LoadOptions = { userDict: './custom/userdict.utf8' }; nodejieba.load(options); ``` -------------------------------- ### Full Mode Segmentation in Node.js Source: https://context7.com/yanyiwu/nodejieba/llms.txt Employs a full segmentation mode that enumerates all possible word combinations within a given Chinese sentence. This mode is useful for comprehensive text analysis, pattern matching, and when you need to capture every potential word constituent, including overlapping words. ```javascript const nodejieba = require("nodejieba"); const text = "南京市长江大桥"; const result = nodejieba.cutAll(text); console.log(result); // Output: ["南京", "南京市", "市长", "长江", "长江大桥", "大桥"] // Full mode generates all possible segmentations const sentence = "我是拖拉机学院手扶拖拉机专业的。"; const fullResult = nodejieba.cutAll(sentence); console.log(fullResult); // Output: ["我", "是", "拖拉", "拖拉机", "学院", "手扶", "手扶拖拉机", "拖拉", "拖拉机", "专业", "的", "。"] ``` -------------------------------- ### Default Segmentation Mode in Node.js Source: https://context7.com/yanyiwu/nodejieba/llms.txt Performs Chinese word segmentation using a combination of Maximum Probability (MP) and Hidden Markov Model (HMM) algorithms. This mode is suitable for general-purpose segmentation, offering a balance between accuracy and handling of unknown words. It can also be used in a strict mode where HMM is disabled. ```javascript const nodejieba = require("nodejieba"); // Basic segmentation const result = nodejieba.cut("南京市长江大桥"); console.log(result); // Output: ["南京市", "长江大桥"] // Strict mode (HMM disabled for known words only) const strictResult = nodejieba.cut("我是拖拉机学院手扶拖拉机专业的。", true); console.log(strictResult); // Output: ["我", "是", "拖拉机", "学院", "手扶拖拉机", "专业", "的", "。"] // Handles emojis and special characters const emojiResult = nodejieba.cut("今天天气很好,🙋 我们去郊游。"); console.log(emojiResult); // Output: ["今天天气", "很", "好", ",", "🙋", " ", "我们", "去", "郊游", "。"] ``` -------------------------------- ### Search Engine Mode Segmentation in Node.js Source: https://context7.com/yanyiwu/nodejieba/llms.txt Optimizes Chinese text segmentation for search engine indexing by breaking down longer words into shorter, semantically relevant components. This mode aims to improve search recall by including both compound words and their constituent parts in the segmented output. It supports an optional strict parameter. ```javascript const nodejieba = require("nodejieba"); const text = "南京市长江大桥"; const result = nodejieba.cutForSearch(text); console.log(result); // Output: ["南京", "市", "长江", "大桥", "南京市", "长江大桥"] // Search mode with strict parameter const sentence = "手扶拖拉机专业"; const searchResult = nodejieba.cutForSearch(sentence, true); console.log(searchResult); // Output: ["手扶", "拖拉", "拖拉机", "手扶拖拉机", "专业"] // Generates both individual components and compound words for better search recall ``` -------------------------------- ### Small Granularity Mode Segmentation in Node.js Source: https://context7.com/yanyiwu/nodejieba/llms.txt Segments Chinese text into words, with a configurable maximum word length. This mode is useful for controlling the granularity of the output, ensuring that no segmented word exceeds a specified character limit. It's beneficial when dealing with specific tokenization requirements. ```javascript const nodejieba = require("nodejieba"); const text = "南京市长江大桥"; const maxWordLength = 3; const result = nodejieba.cutSmall(text, maxWordLength); console.log(result); // Output: ["南京市", "长江", "大桥"] // All words are limited to the specified length const longText = "我是拖拉机学院手扶拖拉机专业的"; const smallResult = nodejieba.cutSmall(longText, 2); console.log(smallResult); // Segments to ensure no word exceeds 2 characters ``` -------------------------------- ### HMM-Only Segmentation Mode in Node.js Source: https://context7.com/yanyiwu/nodejieba/llms.txt Segments Chinese text using only the Hidden Markov Model (HMM) algorithm. This mode is particularly useful for identifying unknown words and out-of-vocabulary terms, as it attempts to segment every word. It differs from the default mode by potentially splitting compound words that might be recognized in the default mode. ```javascript const nodejieba = require("nodejieba"); const text = "南京市长江大桥"; const result = nodejieba.cutHMM(text); console.log(result); // Output: ["南京市", "长江大桥"] // Comparison with regular cut const sentence = "我是拖拉机学院手扶拖拉机专业的。"; const hmmResult = nodejieba.cutHMM(sentence); console.log(hmmResult); // Output: ["我", "是", "拖拉机", "学院", "手", "扶", "拖拉机", "专业", "的", "。"] // Note: "手扶拖拉机" is split into separate tokens in HMM mode ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.