Jieba

jieba トークン化器は、中国語テキストを構成語に分解して処理します。

jieba トー クナイザは、句読点を別のトークンとして出力に保持します。たとえば、"你好!世界。"["你好", "!", "世界", "。"] になります。これらの独立した句読点トークンを削除するには removepunctフィルタを使用してください。

設定

Milvusはjieba トークナイザーに対して、シンプルコンフィギュレーションとカスタムコンフィギュレーションの2つのコンフィギュレーションアプローチをサポートしています。

シンプル設定

シンプルな設定では、トークナイザを"jieba" に設定するだけです。例えば

# 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"
}'

この単純な設定は、以下のカスタム設定と同等です:

# 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

パラメータの詳細については、カスタム構成を参照してください。

カスタム設定

より詳細に制御するには、カスタム辞書を指定し、セグメンテーションモードを選択し、隠れマルコフモデル(HMM)を有効または無効にするカスタム構成を提供できます。例えば

# 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

パラメータ

説明

デフォルト値

type

トークナイザーのタイプ。これは"jieba" に固定されている。

"jieba"

dict

解析器が語彙ソースとして読み込む辞書のリスト。組み込みオプション:

  • "_default_":エンジン内蔵の簡体字中国語辞書をロードします。詳細はdict.txt を参照してください。

  • "_extend_default_":"_default_" のすべてに加え、繁体字中国語の補足を読み込む。詳細はdict.txt.bigを参照。

    内蔵辞書と任意の数のカスタム辞書を混在させることもできます。例:["_default_", "结巴分词器"].

["_default_"]

mode

セグメンテーション・モード。可能な値:

  • "exact":最も正確な方法でセグメンテーションしようとするため、テキスト分析に最適です。

  • "search":検索エンジンのトークン化に適している。

    詳細はJieba GitHub Projectを参照。

"search"

hmm

辞書にない単語の確率的セグメンテーションのために隠れマルコフモデル(HMM)を有効にするかどうかを示すブーリアン・フラグ。

true

大規模なカスタム語彙をdict 経由でインライン化する代わりに外部ファイルからロードするには、後述の「辞書ファ イルを使用したカスタム構成」を参照してください。

analyzer_params を定義した後、コレクション・スキーマを定義するときに、それらをVARCHAR フィールドに適用できます。これにより、Milvusは指定された解析器を使用してそのフィールドのテキストを処理し、効率的なトークン化とフィルタリングを行うことができます。詳細については、使用例を参照してください。

辞書ファイルによるカスタム構成Compatible with Milvus 3.0.x

ドメイン用語集、製品用語集、固有名詞リストなど、大規模なカスタム語彙の場合は、語彙をファイルに格納し、そのファイルをリモート ファイル リソースとして登録します。その後、extra_dict_file パラメータを使用してトークナイザから参照します。解析器は、これらの単語を組み込み辞書の上にある語彙にロードします。

ファイルはプレーンな UTF-8 テキストで、1 行に 1 つの単語が含まれます。たとえば、次のようにします:

结巴分词器
向量数据库

ファイルをMilvusクラスタが使用するように設定されているオブジェクトストアにアップロードし、登録します:

from pymilvus import MilvusClient

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

# Register the uploaded file under a name you'll reference from analyzer configs.
client.add_file_resource(
    name="zh_terms",
    path="file/zh_terms.txt",    # full S3 object key, including the rootPath prefix
)
// java
// nodejs
// go
# restful

extra_dict_file を介してトークナイザで登録されたリソースを参照します:

analyzer_params = {
    "tokenizer": {
        "type": "jieba",
        "dict": ["_default_"],             # keep the built-in dictionary
        "mode": "exact",
        "hmm": False,
        "extra_dict_file": {
            "type": "remote",
            "resource_name": "zh_terms",
            "file_name": "zh_terms.txt",
        },
    },
}

client.run_analyzer(["milvus结巴分词器中文测试"], analyzer_params)
# → [['milvus', '结巴', '分词器', '中文', '测试']]
// java
// nodejs
// go
# restful

extra_dict_file パラメータは以下のフィールドを持つオブジェクトを受け入れます:

フィールド

フィールド 説明

type

リソース・タイプ。add_file_resource を介して登録されたファイルには"remote" を使用します。セルフホスト配備で使用される"local" バリアントについては、「ファイルリソースの管理」を参照してください。

resource_name

ファイルがadd_file_resource で登録されたときに使用された名前。

file_name

登録 さ れてい る リ ソ ース のオブジ ェ ク ト ・ ス ト ア ・ パスの フ ァ イ ル名部分 (た と えば、 リ ソ ース がpath="file/zh_terms.txt" で登録 さ れてい る 場合は"zh_terms.txt" )。

extra_dict_file 経由で追加された単語は、組み込み辞書にマージされるため、jiebaのセグメンテーション・ アルゴリズムでは、既存のエントリと一緒に認識される。向量数据库 のような長いカスタム用語も、短いエントリが組み込み辞書で高い頻度を持つ場合、向量 +数据库 に分割されることがあります。

アナライザの設定をコレクション・スキーマに適用する前に、run_analyzer メソッドを使用して動作を確認してください。

アナライザ構成

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

を使用した検証run_analyzer

from pymilvus import (
    MilvusClient,
)

client = MilvusClient(
    uri="http://localhost:19530",
    token="root:Milvus"
)

# 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("http://localhost:19530")
        .token("root:Milvus")
        .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: "localhost:19530",
    APIKey:  "root:Milvus",
})
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

期待される出力

['milvus', '结巴分词器', '中', '文', '测', '试']

Try Managed Milvus for Free

Zilliz Cloud is hassle-free, powered by Milvus and 10x faster.

Get Started
フィードバック

このページは役に立ちましたか ?