句相似度语义切分
更新时间:2026-06-15
简介
句相似度语义切分 - 基于句子相似度的智能文本切分解决方案
功能描述
- 语义分块策略
- 基于句子相似度的智能切分
- 结合语义与语法规则
- 重叠优化保持上下文连贯性
- 支持中英文混合文本
- 支持中英文字符和标点
- 智能识别句子边界
- 分块引擎
LlamaIndex:使用语义分割节点解析器HuggingFace:使用预训练嵌入模型计算句子相似度- 分块算法
- 基于句子相似度的断点检测
- 支持自定义断点阈值
算子参数
输入
| 输入 | 含义 |
|---|---|
| texts | 待处理的文本数组,要求元素类型为字符串。 |
输出
| 输出 | 含义 |
|---|---|
| result | pyarrow.Array: 切分后的文本块,元素为List[str]类型。 |
参数
| 参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| chunk_size | int | 500 | chunk长度 描述:chunk的长度,单位为字符 默认值:500 |
| chunk_overlap | int | 50 | chunk重叠长度 描述:切分文本时chunk之间重叠的最大长度 默认值:50 |
| model_path | str | '/opt/aihc/models' | 模型路径 描述:嵌入模型的根目录路径 默认值:"/opt/aihc/models" |
| embedding_model_name | str | 'BAAI/bge-m3' | 嵌入模型名称 描述:用于计算句子相似度的嵌入模型名称 默认值:"BAAI/bge-m3" |
| breakpoint_percentile_threshold | int | 80 | 断点百分位阈值 描述:确定语义断点的百分位阈值,值越高,切分越保守 默认值:80 |
调用示例
Python
1from __future__ import annotations
2
3import os
4
5import daft
6from daft import col
7
8from daft.aihc.common.udf import aihc_udf
9from daft.aihc.functions.text.chunk_text_sentence_similarity import ChunkTextSentenceSimilarity
10
11if __name__ == "__main__":
12 if os.getenv("DAFT_RUNNER", "native") == "ray":
13 import ray
14 ray.init(dashboard_host="0.0.0.0", ignore_reinit_error=True)
15 daft.set_runner_ray()
16 daft.set_execution_config(actor_udf_ready_timeout=6000, min_cpu_per_task=0)
17
18 # TODO: 根据实际场景准备样本数据
19 samples = {"texts": [...]}
20 ds = daft.from_pydict(samples)
21 constructor_kwargs = {
22 "chunk_size": 500,
23 "chunk_overlap": 50,
24 "model_path": '/opt/aihc/models',
25 "embedding_model_name": 'BAAI/bge-m3',
26 "breakpoint_percentile_threshold": 80,
27 }
28 ds = ds.with_column(
29 "result",
30 aihc_udf(
31 ChunkTextSentenceSimilarity,
32 construct_args=constructor_kwargs,
33 num_cpus=1,
34 concurrency=4,
35 batch_size=8,
36 )(col("texts")),
37 )
38 ds.show()
评价此篇文章
