Word repetition ratio calculator - Text feature extraction based on N-gram phrase repetition ratio
Input column name | Description |
|---|---|
texts | The text column to be processed; elements must be of string type |
Word repetition ratio column, elements are floating-point numbers representing the proportion of repeated phrases
If a parameter does not have a default value, it is required
Parameter name | Type | Default value | Description |
|---|---|---|---|
repetition | int | 5 | Phrase length Description: Number of words to concatenate into a phrase Default value: 5 |
lang | str | zh | Language Description: The language of the text; supports zh (Chinese) and en (English) Default value: "zh" |
tokenization | bool | True | Whether to use a tokenizer Description: Whether to use the sentencepiece model for tokenization; must be enabled for Chinese Default value: True |
model_path | str | /opt/las/models | Path to the model files Default value: "/opt/las/models" |
model_name | str | kenlm/wikipedia | Model name Default value: "kenlm/wikipedia" |
The following code demonstrates how to use daft to run the operator to calculate the N-gram-based phrase repetition ratio in text.
from __future__ import annotations import os import daft from daft import col from daft.las.functions.text.word_repetition_calculator import WordRepetitionCalculator 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": [ "", "", None, ] } repetition = 2 lang = "zh" tokenization = True ds = daft.from_pydict(samples) ds = ds.with_column( "repetition_ratio", las_udf( WordRepetitionCalculator, construct_args={ "repetition": repetition, "lang": lang, "tokenization": tokenization, "model_path": os.getenv("MODEL_PATH", "/opt/las/models"), "model_name": "kenlm/wikipedia", }, )(col("text")), ) ds.show() # ╭─────────────────────────────────────────────────────────────┬─────────────────────╮ # │ text ┆ repetition_ratio │ # │ --- ┆ --- │ # │ String ┆ Float64 │ # ╞═════════════════════════════════════════════════════════════╪═════════════════════╡ # │ Artificial intelligence technology is developing rapidly, and the application of artificial intelligence technology in various fields is becoming more and more... ┆ 0.14285714285714285 │ # ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ # │ This is a sample text without repeated words; each word appears only once. ┆ 0 │ # ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤ # │ None ┆ None │ # ╰─────────────────────────────────────────────────────────────┴─────────────────────╯