超链接移除算子 - 文本链接正则替换
repl),默认替换为""Noner'...' / r"..." 包裹形式(去除前缀 r 与引号)DOTALL 语义,覆盖跨行文本输入列名 | 说明 |
|---|---|
texts | 字符串数组,每个元素为文本内容 |
处理后的字符串数组;异常项返回 None
如参数没有默认值,则为必填参数
参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
pattern | str | 超链接定位正则。如果为空则使用算子内置表达式;兼容传入 | |
repl | str | 替换命中超链接的字符串,默认空字符串。 |
下面的代码展示了如何使用 daft 运行算子移除文本中的链接。
from __future__ import annotations import logging import os import ray import daft from daft import col from daft.las.functions.text.remove_links import RemoveLinks from daft.las.functions.udf import las_udf if __name__ == "__main__": if os.getenv("DAFT_RUNNER", "native") == "ray": import logging import ray def configure_logging(): logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S.%s".format(), ) logging.getLogger("tracing.span").setLevel(logging.WARNING) logging.getLogger("daft_io.stats").setLevel(logging.WARNING) logging.getLogger("DaftStatisticsManager").setLevel(logging.WARNING) logging.getLogger("DaftFlotillaScheduler").setLevel(logging.WARNING) logging.getLogger("DaftFlotillaDispatcher").setLevel(logging.WARNING) ray.init(dashboard_host="0.0.0.0", runtime_env={"worker_process_setup_hook": configure_logging}) daft.context.set_runner_ray() daft.set_execution_config(actor_udf_ready_timeout=600) daft.set_execution_config(min_cpu_per_task=0) samples = { "text": [ "prefix https://example.com/path suffix", None, ] } pattern = "" repl = "[LINK]" ds = daft.from_pydict(samples) ds = ds.with_column( "cleaned_text", las_udf( RemoveLinks, construct_args={"pattern": pattern, "repl": repl}, )(col("text")), ) ds.show() # ╭────────────────────────────────┬──────────────────────╮ # │ text ┆ cleaned_text │ # │ --- ┆ --- │ # │ Utf8 ┆ Utf8 │ # ╞════════════════════════════════╪══════════════════════╡ # │ prefix https://example.com/pa… ┆ prefix [LINK] suffix │ # ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ # │ None ┆ None │ # ╰────────────────────────────────┴──────────────────────╯