Remove Punct
removepunct 过滤器会从词项流中移除包含标点符号或空白字符的词项。当您希望进行更干净的文本处理、专注于有意义的内容词而非标点时,可以使用该过滤器。
说明
-
该过滤器在
jieba、lindera和icutokenizer 中最有效,因为这些 tokenizer 会将标点保留为独立的 token(例如"Hello!"→["Hello", "!"])。 -
standard分词器会在分词阶段丢弃标点符号。whitespace分词器会保留标点符号,包括词项内部的标点。与whitespace配合使用时,如果词项包含标点符号或空白字符,removepunct会删除整个词项,而不是仅移除其中的个别字符。
配置
removepunct 过滤器已内置在 Zilliz Cloud 中。要使用它,只需在 analyzer_params 的 filter 部分指定其名称即可。
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
python
analyzer_params = {
"tokenizer": "jieba",
"filter": ["removepunct"]
}
java
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", "jieba");
analyzerParams.put("filter", Collections.singletonList("removepunct"));
go
analyzerParams := map[string]any{"tokenizer": "jieba", "filter": []any{"removepunct"}}
rust
use serde_json::json;
let analyzer_params = json!({
"tokenizer": "jieba",
"filter": ["removepunct"]
});
c++
nlohmann::json analyzer_params = {
{"tokenizer", "jieba"},
{"filter", {"removepunct"}}
};
javascript
const analyzer_params = {
"tokenizer": "jieba",
"filter": ["removepunct"],
};
bash
# restful
analyzerParams='{
"tokenizer": "jieba",
"filter": [
"removepunct"
]
}'
removepunct 过滤器作用于分词器生成的词项,因此必须与分词器结合使用。
定义 analyzer_params 后,您可以在定义 Collection Schema 时将其应用于 VARCHAR 字段。这使得 Zilliz Cloud 能够使用指定的分析器处理该字段中的文本,以实现高效的分词和过滤。更多信息,请参阅使用示例。
示例输出
在完成 Analyzer 配置后,您可以使用 run_analyzer 方法来验证分词效果是否符合预期。
Analyzer 配置
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
python
analyzer_params = {
"tokenizer": "icu",
"filter": ["removepunct"]
}
java
Map<String, Object> analyzerParams = new HashMap<>();
analyzerParams.put("tokenizer", "icu");
analyzerParams.put("filter", Collections.singletonList("removepunct"));
go
analyzerParams := map[string]any{"tokenizer": "icu", "filter": []string{"removepunct"}}
rust
use serde_json::json;
let analyzer_params = json!({
"tokenizer": "icu",
"filter": ["removepunct"]
});
c++
nlohmann::json analyzer_params = {
{"tokenizer", "icu"},
{"filter", {"removepunct"}}
};
javascript
const analyzer_params = {
"tokenizer": "icu",
"filter": ["removepunct"],
};
bash
# restful
analyzerParams='{
"tokenizer": "icu",
"filter": [
"removepunct"
]
}'
使用 run_analyzer 验证效果
- Python
- Java
- Go
- Rust
- C++
- NodeJS
- cURL
python
from pymilvus import (
MilvusClient,
)
client = MilvusClient(uri="YOUR_CLUSTER_ENDPOINT")
# Sample text to analyze
sample_text = "Привет! Как дела?"
# Run the standard analyzer with the defined configuration
result = client.run_analyzer(sample_text, analyzer_params)
print("Standard analyzer output:", result)
java
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")
.build();
MilvusClientV2 client = new MilvusClientV2(config);
List<String> texts = new ArrayList<>();
texts.add("Привет! Как дела?");
RunAnalyzerResp resp = client.runAnalyzer(RunAnalyzerReq.builder()
.texts(texts)
.analyzerParams(analyzerParams)
.build());
List<RunAnalyzerResp.AnalyzerResult> results = resp.getResults();
go
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{"Привет! Как дела?"}
option := milvusclient.NewRunAnalyzerOption(texts...).
WithAnalyzerParams(analyzerParams)
result, err := client.RunAnalyzer(ctx, option)
if err != nil {
fmt.Println(err.Error())
// handle error
}
rust
use milvus::v2::prelude::*;
use serde_json::json;
let text = "Привет! Как дела?";
let response = client
.run_analyzer(
RunAnalyzerRequest::builder()
.texts([text])
.analyzer_params(json!({"tokenizer": "icu", "filter": ["removepunct"]}))
.build()?,
)
.await?;
for result in response.results() {
for token in result.get_tokens() {
println!("{}", token.get_text());
}
}
c++
#include "milvus/MilvusClientV2.h"
auto client = milvus::MilvusClientV2::Create();
milvus::ConnectParam connect_param{"YOUR_CLUSTER_ENDPOINT"};
auto status = client->Connect(connect_param);
if (!status.IsOk()) {
std::cout << status.Message() << std::endl;
}
std::string text = "Привет! Как дела?";
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;
}
javascript
import { MilvusClient } from "@zilliz/milvus2-sdk-node";
const sampleText = "Привет! Как дела?";
const client = new MilvusClient({
address: "YOUR_CLUSTER_ENDPOINT",
});
const result = await client.runAnalyzer({
analyzer_params: analyzerParams,
text: sampleText,
});
bash
# restful
curl -X POST "YOUR_CLUSTER_ENDPOINT/v2/vectordb/common/run_analyzer" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_CLUSTER_TOKEN" \
-d '{
"analyzerParams": "{\"tokenizer\": \"icu\", \"filter\": [\"removepunct\"]}",
"text": ["Привет! Как дела?"]
}'
预期结果
python
['Привет', 'Как', 'дела']