跳到主要内容
版本:v3.0.x

分词器

decompounder 过滤器可根据指定词典将复合词拆分成单个成分,从而更方便地搜索复合词的各个部分。该过滤器对于德语等经常使用复合词的语言尤其有用。组件字典可以通过word_list 参数在线提供,也可以通过word_list_file 参数从注册文件资源加载。

配置

decompounder 过滤器可通过word_list 参数以内联方式或通过word_list_file 参数从注册文件资源中接受其组件字典。

内联字表

decompounder 过滤器是 Milvus 的自定义过滤器。要使用该过滤器,请在过滤器配置中指定"type": "decompounder" 以及word_list 参数,后者提供了要识别的单词组件字典。

analyzer_params = {
"tokenizer": "standard",
"filter":[{
"type": "decompounder", # Specifies the filter type as decompounder
"word_list": ["dampf", "schiff", "fahrt", "brot", "backen", "automat"],
}],
}

decompounder 过滤器接受以下可配置参数。

| 参数

|

说明

| | --- | --- | |

word_list

|

用于拆分复合词的单词成分列表。该字典决定了如何将复合词分解为单个术语。

|

decompounder 过滤器对标记化器生成的术语进行操作,因此必须与标记化器结合使用。有关 Milvus 中可用的标记化器列表,请参阅标准标记化器及其同类页面。

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

从文件资源加载单词组件 Compatible with Milvus 3.0.x

对于大型组件字典,尤其是全语言单词表,可将组件存储在文件中,并将该文件注册为远程文件资源,然后通过word_list_file 参数从过滤器中引用该文件。你可以单独使用word_list_file ,也可以与内联word_list 同时使用;当两者都设置时,过滤器会将两个来源合并为一个组件列表。

文件为纯 UTF-8 文本,每行一个组件词。例如

dampf
schiff
fahrt
brot
backen
automat

将文件上传到 Milvus 集群配置使用的对象存储,然后注册:

from pymilvus import MilvusClient

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

# Register the uploaded file under a name you'll reference from analyzer configs.
client.add_file_resource(
name="de_components",
path="file/decompounder.txt", # full S3 object key, including the rootPath prefix
)

通过word_list_file 在过滤器中引用已注册的资源:

analyzer_params = {
"tokenizer": "standard",
"filter": [{
"type": "decompounder",
"word_list_file": {
"type": "remote",
"resource_name": "de_components",
"file_name": "decompounder.txt",
},
}],
}

word_list_file 参数接受包含以下字段的对象:

| 字段

|

字段

| | --- | --- | |

type

|

资源类型。通过add_file_resource 注册的文件使用"remote" 。有关自托管部署中使用的"local" 变体,请参阅管理文件资源

| |

resource_name

|

文件在add_file_resource 注册时使用的名称。

| |

file_name

|

注册资源的对象存储路径中的文件名部分(例如,如果资源是通过path="file/decompounder.txt" 注册的,则为"decompounder.txt" )。

|

示例

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

分析器配置

analyzer_params = {
"tokenizer": "standard",
"filter":[{
"type": "decompounder", # Specifies the filter type as decompounder
"word_list": ["dampf", "schiff", "fahrt", "brot", "backen", "automat"],
}],
}

验证使用run_analyzer

from pymilvus import (
MilvusClient,
)

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

# Sample text to analyze
sample_text = "dampfschifffahrt brotbackautomat"

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

预期输出

['dampf', 'schiff', 'fahrt', 'brotbackautomat']