Jieba
jieba トークナイザーは、中国語のテキストを構成する単語に分割して処理します。
jieba トークナイザーは、出力内で句読点を個別のトークンとして保持します。たとえば、"你好!世界。" は ["你好", "!", "世界", "。"] になります。これらの単独の句読点トークンを削除するには、removepunct フィルターを使用してください。
構成
Zilliz Cloud は、jieba トークナイザーの構成方法として、シンプル構成とカスタム構成の2つをサポートしています。
シンプル構成
シンプル構成では、トークナイザーを "jieba" に設定するだけで済みます。例:
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- CSharp
- cURL
# 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");
analyzerParams := map[string]any{"tokenizer": "jieba"}
use serde_json::json;
let analyzer_params = json!({"tokenizer": "jieba"});
nlohmann::json analyzer_params = {
{"tokenizer", "jieba"}
};
const analyzer_params = {
"tokenizer": "jieba",
};
// Simple configuration: only specifying the tokenizer name
var analyzerParams = new Dictionary<string, object>
{
["tokenizer"] = "jieba" // Use the default settings: dict=["_default_"], mode="search", hmm=True
};
# restful
analyzerParams='{
"tokenizer": "jieba"
}'
このシンプル構成は、次のカスタム構成と同等です:
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- CSharp
- cURL
# Custom configuration equivalent to the simple configuration above
analyzer_params = {
"tokenizer": { # Tokenizer configuration
"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("tokenizer", new HashMap<String, Object>() {{
put("type", "jieba");
put("dict", Collections.singletonList("_default_"));
put("mode", "search");
put("hmm", true);
}});
analyzerParams := map[string]any{
"tokenizer": map[string]any{
"type": "jieba",
"dict": []any{"_default_"},
"mode": "search",
"hmm": true,
},
}
use serde_json::json;
let analyzer_params = json!({
"tokenizer": {
"type": "jieba",
"dict": ["_default_"],
"mode": "search",
"hmm": true
}
});
nlohmann::json analyzer_params = {
{"tokenizer", {
{"type", "jieba"},
{"dict", {"_default_"}},
{"mode", "search"},
{"hmm", true}
}}
};
const analyzer_params = {
tokenizer: {
type: "jieba",
dict: ["_default_"],
mode: "search",
hmm: true,
},
};
// Custom configuration equivalent to the simple configuration above
var analyzerParams = new Dictionary<string, object>
{
["tokenizer"] = new Dictionary<string, object> // Tokenizer configuration
{
["type"] = "jieba", // Tokenizer type, fixed as "jieba"
["dict"] = new[] { "_default_" }, // Use the default dictionary
["mode"] = "search", // Use search mode for improved recall (see mode details below)
["hmm"] = true // Enable HMM for probabilistic segmentation
}
};
# restful
analyzerParams='{
"tokenizer": {
"type": "jieba",
"dict": ["_default_"],
"mode": "search",
"hmm": true
}
}'
パラメーターの詳細については、カスタム構成 を参照してください。
カスタム構成
より細かく制御するには、カスタム構成を指定できます。カスタム構成では、カスタム辞書の指定、分割モードの選択、および隠れマルコフモデル(HMM)の有効化または無効化が可能です。例:
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- CSharp
- cURL
# 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);
}});
analyzerParams := map[string]any{
"tokenizer": map[string]any{
"type": "jieba",
"dict": []string{"customDictionary"},
"mode": "exact",
"hmm": false,
},
}
use serde_json::json;
let analyzer_params = json!({
"tokenizer": {
"type": "jieba",
"dict": ["customDictionary"],
"mode": "exact",
"hmm": false
}
});
nlohmann::json analyzer_params = {
{"tokenizer", {
{"type", "jieba"},
{"dict", {"customDictionary"}},
{"mode", "exact"},
{"hmm", false}
}}
};
const analyzer_params = {
tokenizer: {
type: "jieba",
dict: ["customDictionary"],
mode: "exact",
hmm: false,
},
};
// Custom configuration with user-defined settings
var analyzerParams = new Dictionary<string, object>
{
["tokenizer"] = new Dictionary<string, object>
{
["type"] = "jieba", // Fixed tokenizer type
["dict"] = new[] { "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
}
};
# restful
analyzerParams='{
"tokenizer": {
"type": "jieba",
"dict": ["customDictionary"],
"mode": "exact",
"hmm": false
}
}'
パラメーター | 説明 | デフォルト値 |
|---|---|---|
| トークナイザーのタイプです。これは |
|
| アナライザーが語彙ソースとして読み込む辞書のリストです。組み込みオプション:
|
|
| 分割モードです。使用可能な値は次のとおりです。
|
|
| 辞書にない単語を確率的に分割するために、隠れマルコフモデル(HMM)を有効にするかどうかを示すブール値のフラグです。 |
|
analyzer_params を定義した後、コレクションスキーマを定義する際に、それを VARCHAR フィールドに適用できます。これにより、Zilliz Cloud は指定したアナライザーを使用してそのフィールド内のテキストを処理し、効率的なトークン化とフィルタリングを実行できます。詳細については、使用例 を参照してください。
例
アナライザー構成をコレクションスキーマに適用する前に、run_analyzer メソッドを使用してその動作を確認してください。
アナライザー構成
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- CSharp
- cURL
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);
}});
analyzerParams := map[string]any{
"tokenizer": map[string]any{
"type": "jieba",
"dict": []string{"结巴分词器"},
"mode": "exact",
"hmm": false,
},
}
use serde_json::json;
let analyzer_params = json!({
"tokenizer": {
"type": "jieba",
"dict": ["结巴分词器"],
"mode": "exact",
"hmm": false
}
});
nlohmann::json analyzer_params = {
{"tokenizer", {
{"type", "jieba"},
{"dict", {"结巴分词器"}},
{"mode", "exact"},
{"hmm", false}
}}
};
const analyzer_params = {
tokenizer: {
type: "jieba",
dict: ["结巴分词器"],
mode: "exact",
hmm: false,
},
};
var analyzerParams = new Dictionary<string, object>
{
["tokenizer"] = new Dictionary<string, object>
{
["type"] = "jieba",
["dict"] = new[] { "结巴分词器" },
["mode"] = "exact",
["hmm"] = false
}
};
# restful
analyzerParams='{
"tokenizer": {
"type": "jieba",
"dict": ["结巴分词器"],
"mode": "exact",
"hmm": false
}
}'
run_analyzer を使用した確認
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- CSharp
- cURL
from pymilvus import (
MilvusClient,
)
client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN"
)
# Sample text to analyze
sample_text = "milvus结巴分词器中文测试"
# Run the jieba tokenizer with the defined configuration
result = client.run_analyzer(sample_text, analyzer_params)
print("Jieba analyzer output:", result)
import java.util.*;
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();
for (RunAnalyzerResp.AnalyzerResult result : results) {
for (RunAnalyzerResp.AnalyzerToken token : result.getTokens()) {
System.out.println("Jieba analyzer output: " + token.getToken());
}
}
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{"milvus结巴分词器中文测试"}
option := milvusclient.NewRunAnalyzerOption(texts...).
WithAnalyzerParams(analyzerParams)
result, err := client.RunAnalyzer(ctx, option)
if err != nil {
fmt.Println(err.Error())
// handle error
}
for _, r := range result {
for _, token := range r.Tokens {
fmt.Println("Jieba analyzer output:", token.Text)
}
}
use milvus::v2::prelude::*;
let config = ConnectConfig::new().uri("YOUR_CLUSTER_ENDPOINT").token("YOUR_CLUSTER_TOKEN");
let client = ClientV2::new(&config).await?;
let response = client
.run_analyzer(
RunAnalyzerRequest::builder()
.texts(["milvus结巴分词器中文测试"])
.analyzer_params(analyzer_params)
.build()?,
)
.await?;
for result in response.results() {
for token in result.get_tokens() {
println!("Jieba analyzer output: {}", token.get_text());
}
}
#include "milvus/MilvusClientV2.h"
#include <iostream>
#include <string>
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;
}
for (const auto& result : response.Results()) {
for (const auto& token : result.Tokens()) {
std::cout << "Jieba analyzer output: " << token.token_ << std::endl;
}
}
import { MilvusClient } from '@zilliz/milvus2-sdk-node';
const client = new MilvusClient({
address: 'YOUR_CLUSTER_ENDPOINT',
token: 'YOUR_CLUSTER_TOKEN',
});
const sample_text = 'milvus结巴分词器中文测试';
const result = await client.runAnalyzer({
text: sample_text,
analyzer_params,
});
const tokens = result.results.flatMap(r => r.tokens.map(t => t.token));
console.log('Jieba analyzer output:', tokens);
using Milvus.Client.V2;
using Milvus.Client.V2.Requests.Utility;
using Milvus.Client.V2.Responses.Utility;
using Milvus.Client.V2.Types;
using var client = new MilvusClientV2(new ConnectConfig
{
Uri = "YOUR_CLUSTER_ENDPOINT",
ApiKey = "YOUR_CLUSTER_TOKEN"
});
await client.ConnectAsync();
// Sample text to analyze
var sampleText = "milvus结巴分词器中文测试";
// Run the jieba tokenizer with the defined configuration
RunAnalyzerResp result = await client.RunAnalyzerAsync(new RunAnalyzerReq
{
AnalyzerParams = analyzerParams,
Texts = new[] { sampleText }
});
foreach (AnalyzerResult r in result.Results)
{
foreach (AnalyzerToken t in r.Tokens)
{
Console.WriteLine($"Jieba analyzer output: {t.Token}");
}
}
curl --request POST \
--url "YOUR_CLUSTER_ENDPOINT/v2/vectordb/common/run_analyzer" \
--header "Authorization: Bearer YOUR_CLUSTER_TOKEN" \
--header "Content-Type: application/json" \
--data-raw '{
"text": ["milvus结巴分词器中文测试"],
"analyzerParams": "{\"tokenizer\":{\"type\":\"jieba\",\"dict\":[\"结巴分词器\"],\"mode\":\"exact\",\"hmm\":false}}"
}'
期待される出力
サンプルテキストに対して、アナライザーは次のトークンを順番に生成します。SDK のレスポンスオブジェクトと出力の書式は言語によって異なります。REST レスポンスでは、これらのトークンは data.results[].tokens[].token に含まれます。
['milvus', '结巴分词器', '中', '文', '测', '试']