Jieba
jieba トークナイザーは、中国語テキストを構成単語に分割して処理します。
jieba トークナイザーは、出力時に句読点を個別のトークンとして保持します。たとえば、"你好!世界。" は ["你好", "!", "世界", "。"] となります。これらの単独の句読点トークンを除去するには、removepunct フィルターを使用してください。
設定
Milvus では、jieba トークナイザーに対してシンプル設定とカスタム設定の 2 通りの設定方法がサポートされています。
シンプル設定
シンプル設定では、トークナイザーを "jieba" に指定するだけです。例:
- Python
- Java
- NodeJS
- Go
- cURL
- C++
# Simple configuration: only specifying the tokenizer name
analyzer_params = {
"tokenizer": "jieba", # Use the default settings: dict=["_default_"], mode="search", hmm=True
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", "jieba");
const analyzer_params = {
"tokenizer": "jieba",
};
analyzerParams = map[string]any{"tokenizer": "jieba"}
# restful
analyzerParams='{
"tokenizer": "jieba"
}'
nlohmann::json analyzer_params = {
{"tokenizer", "jieba"}
};
このシンプル設定は、以下のカスタム設定と同等です。
- Python
- Java
- NodeJS
- Go
- cURL
- C++
# Custom configuration equivalent to the simple configuration above
analyzer_params = {
"type": "jieba", # Tokenizer type, fixed as "jieba"
"dict": ["_default_"], # Use the default dictionary
"mode": "search", # Use search mode for improved recall (see mode details below)
"hmm": True # Enable HMM for probabilistic segmentation
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("type", "jieba");
analyzerParams.put("dict", Collections.singletonList("_default_"));
analyzerParams.put("mode", "search");
analyzerParams.put("hmm", true);
// javascript
analyzerParams = map[string]any{"type": "jieba", "dict": []any{"_default_"}, "mode": "search", "hmm": true}
# restful
nlohmann::json analyzer_params = {
{"tokenizer", "jieba"},
{"dict", {"_default_"}},
{"mode", "search"},
{"hmm", true}
};
パラメーターの詳細については、カスタム設定 を参照してください。
カスタム設定
より詳細な制御を行うには、カスタム辞書の指定、分割モードの選択、Hidden Markov Model (HMM) の有効化・無効化が可能なカスタム設定を利用できます。例:
- Python
- Java
- NodeJS
- Go
- cURL
- C++
# Custom configuration with user-defined settings
analyzer_params = {
"tokenizer": {
"type": "jieba", # Fixed tokenizer type
"dict": ["customDictionary"], # Custom dictionary list; replace with your own terms
"mode": "exact", # Use exact mode (non-overlapping tokens)
"hmm": False # Disable HMM; unmatched text will be split into individual characters
}
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", new HashMap<String, Object>() {{
put("type", "jieba");
put("dict", Arrays.asList("customDictionary"));
put("mode", "exact");
put("hmm", false);
}});
// javascript
analyzerParams := map[string]interface{}{
"tokenizer": map[string]interface{}{
"type": "jieba",
"dict": []string{"customDictionary"},
"mode": "exact",
"hmm": false,
},
}
# restful
nlohmann::json analyzerParams = {
{"tokenizer", {
{"type", "jieba"},
{"dict", {"customDictionary"}},
{"mode", "exact"},
{"hmm", false}
}}
};
パラメーター | 説明 | デフォルト値 |
|---|---|---|
| トークナイザーの種類です。 |
|
| アナライザーが語彙ソースとして読み込む辞書のリストです。組み込みオプションは以下のとおりです。
組み込み辞書と任意の数のカスタム辞書を併用することも可能です。例: |
|
| 分割モードです。指定可能な値は以下のとおりです。
詳しくは、Jieba GitHub Project を参照してください。 |
|
| 辞書に登録されていない単語に対して Hidden Markov Model (HMM) による確率的な分割を有効にするかどうかを示すブール値フラグです。 |
|
analyzer_params を定義したら、コレクションスキーマの定義時に VARCHAR フィールドへ適用できます。これにより、Zilliz Cloud が指定されたアナライザーを使って当該フィールドのテキストを処理し、効率的なトークン化とフィルタリングを行えるようになります。詳細については、使用例 を参照してください。
例
コレクションスキーマにアナライザー設定を適用する前に、run_analyzer メソッドを使って動作を確認してください。
アナライザー設定
- Python
- Java
- NodeJS
- Go
- cURL
- C++
analyzer_params = {
"tokenizer": {
"type": "jieba",
"dict": ["结巴分词器"],
"mode": "exact",
"hmm": False
}
}
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", new HashMap<String, Object>() {{
put("type", "jieba");
put("dict", Arrays.asList("结巴分词器"));
put("mode", "exact");
put("hmm", false);
}});
// javascript
analyzerParams := map[string]interface{}{
"tokenizer": map[string]interface{}{
"type": "jieba",
"dict": []string{"结巴分词器"},
"mode": "exact",
"hmm": false,
},
}
# restful
nlohmann::json analyzerParams = {
{"tokenizer", {
{"type", "jieba"},
{"dict", {"结巴分词器"}},
{"mode", "exact"},
{"hmm", false}
}}
};
run_analyzer を使った検証
- Python
- Java
- NodeJS
- Go
- cURL
- C++
from pymilvus import (
MilvusClient,
)
client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN"
)
# Sample text to analyze
sample_text = "milvus结巴分词器中文测试"
# 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("milvus结巴分词器中文测试");
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: "YOUR_CLUSTER_ENDPOINT",
APIKey: "YOUR_CLUSTER_TOKEN",
})
if err != nil {
fmt.Println(err.Error())
// handle error
}
bs, _ := json.Marshal(analyzerParams)
texts := []string{"milvus结巴分词器中文测试"}
option := milvusclient.NewRunAnalyzerOption(texts).
WithAnalyzerParams(string(bs))
result, err := client.RunAnalyzer(ctx, option)
if err != nil {
fmt.Println(err.Error())
// handle error
}
# restful
#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 = "milvus结巴分词器中文测试";
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;
}
期待される出力
['milvus', '结巴分词器', '中', '文', '测', '试']