Whitespace
whitespace トークナイザーは、タブ、ラインフィード、フォームフィード、キャリッジリターン、スペースの5つの ASCII 空白文字でテキストを分割します。
トークン化ルール
whitespace トークナイザーは、以下の5つの ASCII 空白文字でのみテキストを分割します。
| 文字 | 名前 | Unicode コードポイント |
|---|---|---|
\t | 水平タブ | U+0009 |
\n | ラインフィード | U+000A |
\x0C または \f | フォームフィード | U+000C |
\r | キャリッジリターン | U+000D |
' ' | スペース | U+0020 |
これらの区切り文字は破棄され、連続する区切り文字が空のトークンを生成することはありません。句読点やその他の文字はトークン内に残ります。特に、垂直タブ(\x0B、U+000B)、ノーブレークスペース(\u00A0)、および表意文字スペース(\u3000)は分割をトリガーしません。
このセットは Rust の char::is_ascii_whitespace() に従っており、他の Unicode 空白文字は除外されています。
以下の例では、フィルターを指定せずに {"tokenizer": "whitespace"} を使用します。入力と出力は Python の文字列表記を使用しており、\t や \u00A0 などのエスケープシーケンスは実際の文字を表します。
| 入力 | 出力トークン |
|---|---|
"a\tb\nc\x0Cd\re f" | ["a", "b", "c", "d", "e", "f"] |
"Hello,World! foo_bar" | ["Hello,World!", "foo_bar"] |
"a\x0Bb" | ["a\x0Bb"] |
"a\u00A0b" | ["a\u00A0b"] |
"a\u3000b" | ["a\u3000b"] |
"\x20a\x20\x20b\x20" | ["a", "b"] |
設定
whitespace トークナイザーを使用するアナライザーを設定するには、analyzer_params で tokenizer を whitespace に設定します。
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
analyzer_params = {
"tokenizer": "whitespace",
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", "whitespace");
analyzerParams := map[string]any{"tokenizer": "whitespace"}
let analyzer_params = serde_json::json!({"tokenizer": "whitespace"});
nlohmann::json analyzer_params = {
{"tokenizer", "whitespace"}
};
const analyzer_params = {
"tokenizer": "whitespace"
};
# restful
analyzerParams='{
"tokenizer": "whitespace"
}'
whitespace トークナイザーは、1つ以上のフィルターと組み合わせて使用できます。たとえば、次のコードは whitespace トークナイザーと lowercaseフィルター を使用するアナライザーを定義しています。
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
analyzer_params = {
"tokenizer": "whitespace",
"filter": ["lowercase"]
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", "whitespace");
analyzerParams.put("filter", Collections.singletonList("lowercase"));
analyzerParams = map[string]any{"tokenizer": "whitespace", "filter": []any{"lowercase"}}
let analyzer_params = serde_json::json!({"tokenizer": "whitespace", "filter": ["lowercase"]});
nlohmann::json analyzer_params = {
{"tokenizer", "whitespace"},
{"filter", {"lowercase"}}
};
const analyzer_params = {
"tokenizer": "whitespace",
"filter": ["lowercase"]
};
# restful
analyzerParams='{
"tokenizer": "whitespace",
"filter": [
"lowercase"
]
}'
analyzer_params を定義した後、コレクションスキーマを定義する際に、それらを VARCHAR フィールドに適用できます。これにより、Zilliz Cloud は、効率的なトークン化とフィルタリングのために、指定されたアナライザーを使用してそのフィールド内のテキストを処理できます。詳細については、使用例 を参照してください。
例
アナライザー設定をコレクションスキーマに適用する前に、run_analyzer メソッドを使用してその動作を確認してください。
アナライザー設定
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
analyzer_params = {
"tokenizer": "whitespace",
"filter": ["lowercase"]
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", "whitespace");
analyzerParams.put("filter", Collections.singletonList("lowercase"));
analyzerParams = map[string]any{"tokenizer": "whitespace", "filter": []any{"lowercase"}}
let analyzer_params = serde_json::json!({"tokenizer": "whitespace", "filter": ["lowercase"]});
nlohmann::json analyzer_params = {
{"tokenizer", "whitespace"},
{"filter", {"lowercase"}}
};
const analyzer_params = {
"tokenizer": "whitespace",
"filter": ["lowercase"]
};
# restful
analyzerParams='{
"tokenizer": "whitespace",
"filter": [
"lowercase"
]
}'
run_analyzer を使用した検証
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
from pymilvus import (
MilvusClient,
)
client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN"
)
# Sample text to analyze
sample_text = "The Milvus vector database is built for scale!"
# Run the whitespace analyzer with the defined configuration
result = client.run_analyzer(sample_text, analyzer_params)
print("Whitespace 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;
import java.util.*;
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("The Milvus vector database is built for scale!");
RunAnalyzerResp resp = client.runAnalyzer(RunAnalyzerReq.builder()
.texts(texts)
.analyzerParams(analyzerParams)
.build());
List<RunAnalyzerResp.AnalyzerResult> results = resp.getResults();
import (
"context"
"fmt"
"github.com/milvus-io/milvus/client/v3/milvusclient"
)
ctx := context.Background()
client, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: "YOUR_CLUSTER_ENDPOINT",
APIKey: "YOUR_CLUSTER_TOKEN",
})
if err != nil {
fmt.Println(err.Error())
// handle error
}
texts := []string{"The Milvus vector database is built for scale!"}
option := milvusclient.NewRunAnalyzerOption(texts...).
WithAnalyzerParams(analyzerParams)
result, err := client.RunAnalyzer(ctx, option)
if err != nil {
fmt.Println(err.Error())
// handle error
}
use milvus::v2::prelude::*;
let client = ClientV2::new(&ConnectConfig::new().uri("YOUR_CLUSTER_ENDPOINT").token("YOUR_CLUSTER_TOKEN")).await?;
let sample_text = "The Milvus vector database is built for scale!";
client
.run_analyzer(
RunAnalyzerRequest::builder()
.texts([sample_text])
.analyzer_params(analyzer_params.clone())
.build()?,
)
.await?;
#include <iostream>
#include "milvus/MilvusClientV2.h"
auto client = milvus::MilvusClientV2::Create();
milvus::ConnectParam connect_param{"YOUR_CLUSTER_ENDPOINT", "YOUR_CLUSTER_TOKEN"};
auto status = client->Connect(connect_param);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
std::string text = "The Milvus vector database is built for scale!";
auto request = milvus::RunAnalyzerRequest()
.AddText(text)
.WithAnalyzerParams(analyzer_params);
milvus::RunAnalyzerResponse response;
status = client->RunAnalyzer(request, response);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
import { MilvusClient } from "@zilliz/milvus2-sdk-node";
const client = new MilvusClient({
address: "YOUR_CLUSTER_ENDPOINT",
token: "YOUR_CLUSTER_TOKEN"
});
// Sample text to analyze
const sample_text = "The Milvus vector database is built for scale!";
// Run the whitespace analyzer with the defined configuration
const result = await client.runAnalyzer({
text: sample_text,
analyzer_params: analyzer_params
});
# restful
export MILVUS_HOST="YOUR_CLUSTER_ENDPOINT"
export SAMPLE_TEXT="The Milvus vector database is built for scale!"
curl -X POST "http://${MILVUS_HOST}/v2/vectordb/common/run_analyzer" \
-H "Content-Type: application/json" \
-H "Request-Timeout: 10" \
-d '{
"text": ["'"${SAMPLE_TEXT}"'"],
"analyzerParams": "{\"tokenizer\":\"whitespace\",\"filter\":[\"lowercase\"]}"
}'
期待される出力
['the', 'milvus', 'vector', 'database', 'is', 'built', 'for', 'scale!']