ICU
ICU 分词器基于 Unicode 国际化组件(ICU)开源项目构建,该项目为软件国际化提供了关键工具。通过使用 ICU 的断词算法,分词器能够在世界上大多数语言中准确地将文本拆分为单词。
📘说明
ICU 分词器会在输出中将标点符号和空格保留为单独的 token。例如:
"Привет! Как дела?"
会被分割为 ["Привет", "!", " ", "Как", " ", "дела", "?"]
。
如果希望移除这些单独的标点符号 token,可以使用 removepunct 过滤器。
配置
要使用 icu
分词器,请在 analyzer_params
中将 tokenizer
设置为 icu
。
- Python
- Java
- NodeJS
- Go
- cURL
analyzer_params = {
"tokenizer": "icu",
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", "icu");
// node
analyzerParams = map[string]any{"tokenizer": "icu"}
# curl
ICU 分词器可以与一个或多个过滤器配合使用。例如,以下代码定义了一个使用 icu
分词器和 removepunct
过滤器的 Analyzer:
- Python
- Java
- NodeJS
- Go
- cURL
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
后,您可以在定义 Collection Schema 时将其应用于 VARCHAR 字段。这使得 Zilliz Cloud 能够使用指定的分析器处理该字段中的文本,以实现高效的分词和过滤。更多信息,请参阅使用示例。
使用示例
在完成 Analyzer 配置后,您可以使用 run_analyzer
方法来验证分词效果是否符合预期。
Analyzer 配置
- Python
- Java
- NodeJS
- Go
- cURL
analyzer_params = {
"tokenizer": "icu",
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", "icu");
// node
analyzerParams = map[string]any{"tokenizer": "icu"}
# curl
使用 run_analyzer 验证效果
- Python
- Java
- NodeJS
- Go
- cURL
from pymilvus import (
MilvusClient,
)
client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN"
)
# 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("YOUR_CLUSTER_ENDPOINT")
.token("YOUR_CLUSTER_TOKEN")
.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: "YOUR_CLUSTER_TOKEN",
})
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
预期输出
['Привет', '!', ' ', 'Как', ' ', 'дела', '?']