跳到主要内容
版本:v2.5.x

词霸

jieba 标记符号转换器可将中文文本分解为单词。

配置

Milvus 支持jieba 标记符号生成器的两种配置方法:简单配置和自定义配置。

简单配置

使用简单配置,只需将标记符设置为"jieba" 。例如

# Simple configuration: only specifying the tokenizer name
analyzer_params = {
"tokenizer": "jieba", # Use the default settings: dict=["_default_"], mode="search", hmm=true
}

此简单配置等同于以下自定义配置:

# Custom configuration equivalent to the simple configuration above
analyzer_params = {
"type": "jieba", # Tokenizer type, fixed as "jieba"
"dict": ["_default_"], # Use the default dictionary
"mode": "search", # Use search mode for improved recall (see mode details below)
"hmm": true # Enable HMM for probabilistic segmentation
}

有关参数的详细信息,请参阅自定义配置

自定义配置

为获得更多控制权,您可以提供自定义配置,允许您指定自定义字典、选择分割模式以及启用或禁用隐马尔可夫模型(HMM)。例如

# Custom configuration with user-defined settings
analyzer_params = {
"tokenizer": {
"type": "jieba", # Fixed tokenizer type
"dict": ["customDictionary"], # Custom dictionary list; replace with your own terms
"mode": "exact", # Use exact mode (non-overlapping tokens)
"hmm": false # Disable HMM; unmatched text will be split into individual characters
}
}

| 参数

|

参数

|

默认值

| | --- | --- | --- | |

type

|

标记符类型。固定为"jieba"

|

"jieba"

| |

dict

|

分析器将作为词汇源加载的词典列表。内置选项:

  • "_default_":加载引擎内置的简体中文词典。详情请参阅dict.txt

  • "_extend_default_":加载"_default_" 中的所有内容以及额外的繁体中文补充。详情请参阅dict.txt.big

    您也可以将内置词典与任意数量的自定义词典混合使用。示例:["_default_", "结巴分词器"]

|

["_default_"]

| |

mode

|

分段模式。可能的值:

  • "exact":尝试以最精确的方式分割句子,是文本分析的理想选择。

  • "search":在精确模式的基础上进一步分解长词以提高召回率,适合搜索引擎标记化。

    更多信息,请参阅Jieba GitHub 项目

|

"search"

| |

hmm

|

布尔标志,表示是否启用隐马尔可夫模型(HMM)对字典中找不到的单词进行概率分割。

|

true

|

定义analyzer_params 后,您可以在定义 Collection Schema 时将其应用到VARCHAR 字段。这样,Milvus 就能使用指定的分析器对该字段中的文本进行处理,以实现高效的标记化和过滤。有关详情,请参阅示例使用

示例

在将分析器配置应用到 Collection 模式之前,请使用run_analyzer 方法验证其行为。

分析器配置

analyzer_params = {
"tokenizer": {
"type": "jieba",
"dict": ["结巴分词器"],
"mode": "exact",
"hmm": False
}
}

验证使用run_analyzerCompatible with Milvus 2.5.11+

from pymilvus import (
MilvusClient,
)

client = MilvusClient(uri="http://localhost:19530")

# Sample text to analyze
sample_text = "milvus结巴分词器中文测试"

# Run the standard analyzer with the defined configuration
result = client.run_analyzer(sample_text, analyzer_params)
print("Standard analyzer output:", result)

预期输出

['milvus', '结巴分词器', '中', '文', '测', '试']