You need to enable JavaScript to run this app.
Lake AI Service

Lake AI Service

Copy page
Download PDF
Text processing
Text length calculator
Copy page
Download PDF
Text length calculator

Operator introduction

Description

Text length calculator - calculates the character length of text

Key features

  • Text length calculation: calculates the number of characters in the input text
  • Batch processing: supports batch text length calculation
  • Numeric output: returns the length value as an integer

Application scenarios

  • Text length statistics and analysis
  • Data quality check
  • Text preprocessing and filtering

Technical characteristics

  • Accurate calculation: uses Python's built-in len() function to calculate the number of characters
  • Type safety: ensures the input is of string type

Daft invocation

Operator parameters

Input

Input column name

Note

texts

The text column to be processed; the element type must be string

Output

Text length column, element type is integer

Examples

The following code demonstrates how to use daft to run the operator and calculate the character length of text.

from __future__ import annotations

import os

import daft
from daft import col
from daft.las.functions.text.text_length_calculator import TextLengthCalculator
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.set_runner_ray()
    daft.set_execution_config(actor_udf_ready_timeout=600)
    daft.set_execution_config(min_cpu_per_task=0)

    samples = {
        "text": [
            "Hello World",
            "你好世界",
            "Python编程 is fun",
            None,
        ]
    }

    ds = daft.from_pydict(samples)
    ds = ds.with_column(
        "length",
        las_udf(
            TextLengthCalculator,
            construct_args={},
        )(col("text")),
    )

    ds.show()
    # ╭───────────────────┬────────╮
    # │ text              ┆ length │
    # │ ---               ┆ ---    │
    # │ String            ┆ Int64  │
    # ╞═══════════════════╪════════╡
    # │ Hello World       ┆ 11     │
    # ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
    # │ 你好世界          ┆ 4      │
    # ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
    # │ Python编程 is fun ┆ 15     │
    # ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
    # │ None              ┆ None   │
    # ╰───────────────────┴────────╯
Last updated: 2026.05.24 18:11:40