ICUCompatible with Milvus 2.5.11+

icu tokenizer 基于Unicode 国际化组件(ICU)开源项目构建,该项目为软件国际化提供了关键工具。通过使用 ICU 的分词算法,令牌转换器可以准确地将世界上大多数语言的文本分割成单词。

icu 标记符号转换器会在输出中保留标点符号和空格作为单独的标记符号。例如,"Привет! Как дела?" 变成["Привет", "!", " ", "Как", " ", "дела", "?"] 。要删除这些独立的标点符号,请使用 removepunct过滤器。

配置

要配置使用icu 令牌转换器的分析器,请在analyzer_params 中将tokenizer 设置为icu

analyzer_params = {
    "tokenizer": "icu",
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", "icu");
// node
analyzerParams = map[string]any{"tokenizer": "icu"}
# curl

icu 令牌分析器可与一个或多个过滤器结合使用。例如,下面的代码定义了一个使用icu 标记器和移除标点过滤器的分析器:

analyzer_params = {
    "tokenizer": "icu",
    "filter": ["removepunct"]
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", "icu");
analyzerParams.put("filter", Collections.singletonList("removepunct"));
// node
analyzerParams = map[string]any{"tokenizer": "icu", "filter": []string{"removepunct"}}
# curl

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

示例

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

分析器配置

analyzer_params = {
    "tokenizer": "icu",
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", "icu");
// node
analyzerParams = map[string]any{"tokenizer": "icu"}
# curl

验证使用run_analyzer

from pymilvus import (
    MilvusClient,
)

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

# Sample text to analyze
sample_text = "Привет! Как дела?"

# 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("Привет! Как дела?");

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{"Привет! Как дела?"}
option := milvusclient.NewRunAnalyzerOption(texts).
    WithAnalyzerParams(string(bs))

result, err := client.RunAnalyzer(ctx, option)
if err != nil {
    fmt.Println(err.Error())
    // handle error
}
# restful

预期输出

['Привет', '!', ' ', 'Как', ' ', 'дела', '?']

想要更快、更简单、更好用的 Milvus SaaS服务 ?

Zilliz Cloud是基于Milvus的全托管向量数据库,拥有更高性能,更易扩展,以及卓越性价比

免费试用 Zilliz Cloud
反馈

此页对您是否有帮助?