詞霸

jieba tokenizer 將中文文字分解成字元來處理。

jieba tokenizer 在輸出中保留標點符號作為獨立的標記。例如,"你好!世界。" 變成["你好", "!", "世界", "。"] 。若要移除這些獨立的標點符號,請使用 removepunct過濾器。

配置

Milvus 支援兩種jieba tokenizer 的設定方法:簡單設定和自訂設定。

簡單配置

使用簡單配置,您只需要將 tokenizer 設定為"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

tokenizer 的類型。固定為"jieba"

"jieba"

dict

分析器將載入作為詞彙來源的詞典清單。內建選項:

  • "_default_":載入引擎內建的簡體中文字典。詳情請參閱dict.txt

  • "_extend_default_":載入"_default_" 中的所有內容,外加額外的繁體補充。詳情請參閱dict.txt.big

    您也可以將內建字典與任意數目的自訂字典混合使用。範例:["_default_", "结巴分词器"]

["_default_"]

mode

分割模式。可能的值:

  • "exact":嘗試以最精確的方式分割句子,非常適合文字分析。

  • "search":在精確模式的基礎上,進一步分割長字詞以提高召回率,使其適用於搜尋引擎標記化。

    如需詳細資訊,請參閱Jieba GitHub 專案

"search"

hmm

一個布林標誌,表示是否啟用隱馬可夫模型 (HMM) 來對字典中找不到的字進行概率分割。

true

定義analyzer_params 之後,您可以在定義集合模式時將它們套用到VARCHAR 欄位。這可讓 Milvus 使用指定的分析器來處理該欄位中的文字,以進行有效的標記化和過濾。詳情請參閱範例使用

範例

在應用分析器配置到您的收集模式之前,請使用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_analyzerCompatible with Milvus 2.5.11+

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', '结巴分词器', '中', '文', '测', '试']

免費嘗試托管的 Milvus

Zilliz Cloud 無縫接入,由 Milvus 提供動力,速度提升 10 倍。

開始使用
反饋

這個頁面有幫助嗎?