文本长度计算器
更新时间:2026-05-18
文本长度计算器
简介
文本长度计算器,计算文本字符串的字符数(字节/Unicode 字符数),用于数据质量过滤。
功能描述
- 计算文本的 Unicode 字符总数
- 空字符串或 None 输入返回 None
- 无外部依赖,处理速度极快
算子参数
输入
| 输入 | 含义 |
|---|---|
| text | 文本字符串数组 |
输出
| 输出 | 含义 |
|---|---|
| text_length | 文本字符数(int64),空文本返回 None |
参数
无
调用示例
Plain Text
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.text_length_calculator import TextLengthCalculator
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 samples = {"text": ["Hello World", "你好世界", "", None, "a" * 1000]}
19 ds = daft.from_pydict(samples)
20 ds = ds.with_column(
21 "text_length",
22 aihc_udf(
23 TextLengthCalculator,
24 num_cpus=1,
25 concurrency=4,
26 batch_size=1024,
27 )(col("text")),
28 )
29 ds = ds.where(col("text_length") > 50)
30 ds.show()
评价此篇文章
