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

Lake AI Service

Copy page
Download PDF
Text processing
Character ratio calculator
Copy page
Download PDF
Character ratio calculator

Operator introduction

Description

Character ratio calculator - Text feature extraction based on the ratio of alphabetic and numeric characters

Key features

  • Character ratio calculation: Accurately calculates the proportion of alphabetic and numeric characters in text
  • Tokenization mode support: Allows selection between token-based or character-level ratio calculation
  • Multilingual support: Supports character recognition for English, Chinese, Japanese, Korean, and many other languages

Application scenarios

  • Multilingual text quality assessment
  • Data cleaning and preprocessing
  • Text classification feature extraction
  • Content safety detection
  • Multilingual text analysis

Technical characteristics

  • Supports two calculation modes:
    • Character mode: The proportion of alphabetic and numeric characters to the total number of characters
    • Tokenization mode: The proportion of alphabetic characters to the total number of tokens
  • Supports multilingual Unicode character recognition, including but not limited to:
    • English alphabet (a-z, A-Z)
    • Chinese characters
    • Japanese characters (Hiragana, Katakana, Kanji)
    • Korean characters (Hangul)
    • Numeric characters (0-9)

Daft invocation

Operator parameters

Input

Input column name

Note

texts

The text column to be processed; elements must be strings

Output

Ratio result column, with elements as floating-point numbers representing the proportion of alphabetic and numeric characters

Parameters

If a parameter does not have a default value, it is required

Parameter name

Type

Default value

Description

tokenization

bool

False

Tokenization: Whether to use tokenization mode for ratio calculation. Default value: False

model_path

str

/opt/las/models

Path to the model files. Default value: "/opt/las/models"

model_name

str

pythia-6.9b-deduped

Model name. Default value: "pythia-6.9b-deduped"

Examples

The following code demonstrates how to use daft to run the operator and calculate the proportion of alphabetic and numeric characters in text.

from __future__ import annotations

import os

import daft
from daft import col
from daft.las.functions.text.alphanumeric_ratio_calculator import AlphanumericRatioCalculator
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": [
            "HelloWorld123",
            "Hello, world!",
            "!!!@@@###$$$",
            "Test 123! Is it working?",
            "你好Hello123",
        ]
    }

    ds = daft.from_pydict(samples)
    ds = ds.with_column(
        "alphanumeric_ratio",
        las_udf(
            AlphanumericRatioCalculator,
            construct_args={"tokenization": False},
        )(col("text")),
    )

    ds.show()
    # ╭─────────────────────────┬─────────────────────╮
    # │ text                    ┆ alphanumeric_ratio  │
    # │ ---                     ┆ ---                 │
    # │ Utf8                    ┆ Float64             │
    # ╞═════════════════════════╪═════════════════════╡
    # │ HelloWorld123           ┆ 1.0                 │
    # │ Hello, world! ┆ 0.7692307692307693  │
    # │ !!!@@@###$$$            ┆ 0.0                 │
    # │ Test 123! Is it work…   ┆ 0.75                │
    # │ 你好Hello123             ┆ 1.0                 │
    # ╰─────────────────────────┴─────────────────────╯
Last updated: 2026.05.12 19:06:38