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

AI 数据湖服务

复制全文
文本分类
文本语种识别
复制全文
文本语种识别

算子介绍

描述

文本语种识别算子 - 基于FastText模型提供多语言识别能力

核心功能

  • 支持识别176种语言(基于fasttext/lid.176系列模型)
  • 批量推理优化,适合处理大规模文本数据
  • 支持同时输出语种标签及置信度分数
  • 支持本地路径和TOS路径的模型文件

应用场景

  • 多语言文本分类
  • 语种过滤和筛选
  • 多语言数据集构建

建议

  • 输入文本长度建议不少于10个字符以提高识别准确率
  • 需要至少4GB内存以进行模型批式推理

Daft 调用

算子参数

输入

输入列名

说明

texts

原始字符串列,要求元素类型为字符串

输出

包含语种标签和置信度分数的结构体列
每个元素包含 language 和 confidence 字段

参数

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

参数名称

类型

默认值

描述

model_path

str

/opt/las/models

模型文件所在的路径 默认值:"/opt/las/models"

model_name

str

fasttext/lid.176.bin

模型文件名,支持 "fasttext/lid.176.bin" 或 "fasttext/lid.176.ftz"

batch_size

int

1000

批量处理大小,较大的batch_size可提升吐吐但增加内存消耗

调用示例

下面的代码展示了如何使用 daft 运行算子识别文本的语种。

from __future__ import annotations

import os

import daft
from daft import col
from daft.las.functions.text.language_recognition import LanguageRecognitionOperator
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": [
            "这是一行测试内容。",
            "This is a test content.",
            "This is a test content.这是一行测试内容。",
            "こんにちは",
            "안녕하세요",
        ]
    }

    ds = daft.from_pydict(samples)
    ds = ds.with_column(
        "language_result",
        las_udf(
            LanguageRecognitionOperator,
            construct_args={
                "model_path": os.getenv("MODEL_PATH", "/opt/las/models"),
                "model_name": "fasttext/lid.176.bin",
                "batch_size": 1000,
            },
        )(col("text")),
    )

    ds.show()
    # ╭──────────────────────────────────────┬─────────────────────────────────────────────╮
    # │ text                                 ┆ language_result                             │
    # │ ---                                  ┆ ---                                         │
    # │ Utf8                                 ┆ Struct[language: Utf8, confidence: Float64] │
    # ╞══════════════════════════════════════╪═════════════════════════════════════════════╡
    # │ 这是一行测试内容。                   ┆ {language: zh, confidence: 1.000048279762268} │
    # │ This is a test content.              ┆ {language: en, confidence: 0.9209088683128357} │
    # │ This is a test content.这是一行测试… ┆ {language: zh, confidence: 0.6892356276512146} │
    # │ こんにちは                           ┆ {language: ja, confidence: 1.0000269412994385} │
    # │ 안녕하세요                           ┆ {language: ko, confidence: 0.9996028542518616} │
    # ╰──────────────────────────────────────┴─────────────────────────────────────────────╯
最近更新时间:2026.01.08 19:14:24
这个页面对您有帮助吗?
有用
有用
无用
无用