分词器
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"],
}],
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", "standard");
analyzerParams.put("filter",
Collections.singletonList(
new HashMap<String, Object>() {{
put("type", "decompounder");
put("word_list", Arrays.asList("dampf", "schiff", "fahrt", "brot", "backen", "automat"));
}}
)
);
const analyzer_params = {
"tokenizer": "standard",
"filter":[{
"type": "decompounder", // Specifies the filter type as decompounder
"word_list": ["dampf", "schiff", "fahrt", "brot", "backen", "automat"],
}],
};
analyzerParams = map[string]any{"tokenizer": "standard",
"filter": []any{map[string]any{
"type": "decompounder",
"word_list": []string{"dampf", "schiff", "fahrt", "brot", "backen", "automat"},
}}}
# restful
analyzerParams='{
"tokenizer": "standard",
"filter": [
{
"type": "decompounder",
"word_list": [
"dampf",
"schiff",
"fahrt",
"brot",
"backen",
"automat"
]
}
]
}'
decompounder 过滤器接受以下可配置参数。
参数 |
说明 |
|---|---|
|
用于拆分复合词的单词成分列表。该字典决定了如何将复合词分解为单个术语。 |
decompounder 过滤器对标记化器生成的术语进行操作,因此必须与标记化器结合使用。有关 Milvus 中可用的标记化器列表,请参阅标准标记化器及其同类页面。
定义analyzer_params 后,可以在定义 Collections 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 参数接受包含以下字段的对象:
字段 |
字段 |
|---|---|
|
资源类型。通过 |
|
文件在 |
|
注册资源的对象存储路径中的文件名部分(例如,如果资源是通过 |
示例
在将分析器配置应用到 Collections 模式之前,请使用run_analyzer 方法验证其行为。
分析器配置
analyzer_params = {
"tokenizer": "standard",
"filter":[{
"type": "decompounder", # Specifies the filter type as decompounder
"word_list": ["dampf", "schiff", "fahrt", "brot", "backen", "automat"],
}],
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", "standard");
analyzerParams.put("filter",
Collections.singletonList(
new HashMap<String, Object>() {{
put("type", "decompounder");
put("word_list", Arrays.asList("dampf", "schiff", "fahrt", "brot", "backen", "automat"));
}}
)
);
// javascript
analyzerParams = map[string]any{"tokenizer": "standard",
"filter": []any{map[string]any{
"type": "decompounder",
"word_list": []string{"dampf", "schiff", "fahrt", "brot", "backen", "automat"},
}}}
# restful
analyzerParams='{
"tokenizer": "standard",
"filter": [
{
"type": "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)
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.service.vector.request.RunAnalyzerReq;
import io.milvus.v2.service.vector.response.RunAnalyzerResp;
ConnectConfig config = ConnectConfig.builder()
.uri("http://localhost:19530")
.build();
MilvusClientV2 client = new MilvusClientV2(config);
List<String> texts = new ArrayList<>();
texts.add("dampfschifffahrt brotbackautomat");
RunAnalyzerResp resp = client.runAnalyzer(RunAnalyzerReq.builder()
.texts(texts)
.analyzerParams(analyzerParams)
.build());
List<RunAnalyzerResp.AnalyzerResult> results = resp.getResults();
// javascript
import (
"context"
"encoding/json"
"fmt"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
client, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: "localhost:19530",
APIKey: "root:Milvus",
})
if err != nil {
fmt.Println(err.Error())
// handle error
}
bs, _ := json.Marshal(analyzerParams)
texts := []string{"dampfschifffahrt brotbackautomat"}
option := milvusclient.NewRunAnalyzerOption(texts).
WithAnalyzerParams(string(bs))
result, err := client.RunAnalyzer(ctx, option)
if err != nil {
fmt.Println(err.Error())
// handle error
}
# restful
预期输出
['dampf', 'schiff', 'fahrt', 'brotbackautomat']