You need to enable JavaScript to run this app.
AI 数据湖服务

AI 数据湖服务

复制全文
文本处理
文本链接移除
复制全文
文本链接移除

算子介绍

描述

超链接移除算子 - 文本链接正则替换

核心功能

  • 识别协议链接、www 链接、域名 + 路径等通用形式的超链接
  • 将命中的超链接替换为指定字符串(repl),默认替换为""
  • 批量处理字符串,异常项返回 None

技术实现

  • 正则匹配:默认内置表达式与 LAS 版本语义等价
  • 兼容传入字符串的 r'...' / r"..." 包裹形式(去除前缀 r 与引号)
  • 预编译正则并启用 DOTALL 语义,覆盖跨行文本
  • 日志记录:输出默认/自定义正则与处理统计,便于排查

Daft 调用

算子参数

输入

输入列名

说明

texts

字符串数组,每个元素为文本内容

输出

处理后的字符串数组;异常项返回 None

参数

如参数没有默认值,则为必填参数

参数名称

类型

默认值

描述

pattern

str

超链接定位正则。如果为空则使用算子内置表达式;兼容传入 r'...'r"..." 的包裹形式。

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                 │
    # ╰────────────────────────────────┴──────────────────────╯
最近更新时间:2026.01.08 19:14:22
这个页面对您有帮助吗?
有用
有用
无用
无用