跳转到主要内容

并行语料库分类器,指示一对句子是否为相互翻译的可能性(神经版本)

项目描述

Bicleaner AI

License

Bicleaner AI (bicleaner-ai-classify) 是一个Python工具,旨在检测平行语料库中的噪声句子对。它指示一对句子是否为相互翻译的可能性(值接近1)或不是(值接近0)。认为非常噪声的句子对得分是0。

尽管提供了一个训练工具(bicleaner-ai-train),但您可能想使用可用的现成语言包。请使用bicleaner-ai-download下载最新的语言包,或访问Github发行版获取轻量级模型,以及从v2.0版本起访问Hugging Face Hub获取完整模型。请访问我们的文档,了解Bicleaner训练的详细示例。

如果您觉得Bicleaner AI很有用,请考虑引用我们

新功能是什么?

v3.0.0 提升多语言支持!

新的改进型多语言模型用于零样本分类。

以前的新闻

v2.0.0,2023年3月10日

模型准确度改进和HF集成!请参阅变更日志

v1.0.0,2021年6月6日

Bicleaner AI是一个Bicleaner分支,它使用神经网络。它包含两种类型的模型:用于快速评分的轻量级模型和用于高性能的完整模型。轻量级模型使用自然语言推理的可分解注意力模型(Parikh等人)。完整模型使用微调后的XLMRoberta(《大规模无监督跨语言表示学习》)。

使用XLMRoberta和1:10的积极与消极比例受到了WMT20并行语料库过滤任务冠军论文(《使用代理任务学习的transformer进行噪声并行语料库过滤》)的启发。

安装与要求

  • Python >= 3.8
  • PIP >= 23.0
  • CUDA >=11.2(用于训练和推理完整模型)

Bicleaner AI是用Python编写的,可以使用pip安装。它还要求使用支持7-gram语言模型的KenLM Python绑定。Hardrules使用FastSpell,需要手动安装cyhunspell。您可以通过运行以下命令轻松安装所有要求

pip install bicleaner-ai git+https://github.com/MSeal/cython_hunspell@2.0.3
pip install --config-settings="--build-option=--max_order=7" https://github.com/kpu/kenlm/archive/master.zip

安装后,三个二进制文件(bicleaner-ai-trainbicleaner-ai-classifybicleaner-ai-download)将位于您的python/installation/prefix/bin目录中。这通常是$HOME/.local/bin/usr/local/bin/

TensorFlow

TensorFlow 2将作为依赖项安装,并且需要GPU支持以进行训练。pip将安装最新版本的TensorFlow,但支持旧版本>=2.6.5,如果您的机器不满足TensorFlow CUDA要求,则可以安装。请参阅此表格以了解CUDA和TensorFlow版本兼容性。如果您想要不同的TensorFlow版本,可以使用以下命令降级

pip install tensorflow==2.6.5

默认情况下,TensorFlow日志消息被抑制,如果您想查看它们,必须显式设置TF_CPP_MIN_LOG_LEVEL环境变量。例如

TF_CPP_MIN_LOG_LEVEL=0 bicleaner-ai-classify

警告:如果您发现Bicleaner AI没有在GPU上运行而速度变慢,您应该检查这些日志,看看TensorFlow是否正确加载了所有库。

可选要求

对于塞尔维亚-克罗地亚语,模型与转写结合使用效果更好。要能够评分转写文本,安装可选依赖

pip install bicleaner-ai[transliterate]

请注意,这不会转写输出文本,它只用于评分。

清理

入门

bicleaner-ai-classify旨在检测平行语料库中的噪声句子对。它表示一对句子是相互翻译的可能性(值接近1)或不是(值接近0)。非常噪声的句子对评分将为0。

默认情况下,输入文件(要分类的平行语料库)至少需要四列,分别是

  • 列1:URL 1
  • 列2:URL 2
  • 列3:源句子
  • 列4:目标句子

但可以通过使用 --scol--tcol 标志自定义源句子和目标句子的列索引。URL 不是必须的。

生成的输出文件将包含与原始输入文件相同的行和列,并添加一个包含 Bicleaner AI 分类器得分的额外列。

下载模型

Bicleaner AI 有两种类型的模型,完整模型和轻量模型。建议使用完整模型,因为它们提供了更高的质量。如果您对速度有严格的限制,轻量模型可能是一个选择(请参阅速度 比较)。

在此处查看可用的完整模型 这里 和可用的轻量模型 这里

您可以使用以下方式下载模型

bicleaner-ai-download en fr full

这将从 HuggingFace 下载 bitextor/bicleaner-ai-full-en-fr 模型并将其存储在缓存目录中。

或者您可以使用以下方式下载轻量模型

bicleaner-ai-download en fr lite ./bicleaner-models

这将下载并将 en-fr 轻量模型存储在 ./bicleaner-models/en-fr

从 2.3.0 版本开始,完整模型也接受本地路径进行下载,而不是 HF 缓存目录。在这种情况下,要使用模型,请提供本地路径而不是 HF 标识符。

有关 HF 缓存如何工作的更多信息,请参阅 官方文档

分类

要分类一个包含英语句子在第一列和法语句子在第二列的制表符分隔文件,请使用

bicleaner-ai-classify  \
        --scol 1 --tcol 2
        corpus.en-fr.tsv  \
        corpus.en-fr.classifed.tsv  \
        bitextor/bicleaner-ai-full-en-fr

其中 --scol--tcol 指示源句子和目标句子的位置,corpus.en-fr.tsv 是输入文件,corpus.en-fr.classified.tsv 是输出文件,bitextor/bicleaner-ai-en-fr 是 HuggingFace 模型名称。新文件的每一行都将包含与输入文件相同的内容,并添加一个包含 Bicleaner AI 分类器给出的得分的列。

请注意,要使用轻量模型,您需要提供您本地文件系统中的模型路径,而不是 HuggingFace 模型名称。

多语言模型

有可用的多语言完整模型 这里。它们可以与 XLMR 支持 的任何潜在语言(目前仅与英语配对)一起使用。有关如何训练多语言模型或我们的模型表现如何的更多解释,请参阅 这里这里

警告:多语言模型将禁用期望语言参数的硬规则。但是,您可以在分类期间使用 -s/--source_lang-t/--target_lang 选项覆盖模型配置中的语言代码。例如,在评分英语-冰岛语数据时,请使用

bicleaner-ai-classify \
    --scol 1 --tcol 2 \
    -t is \
    corpus.en-is.tsv \
    corpus.en-is.classified.tsv \
    bitextor/bicleaner-ai-full-en-xx

用法

命令行参数的完整描述
usage: bicleaner-ai-classify [-h] [-s SOURCE_LANG] [-t TARGET_LANG] [-S SOURCE_TOKENIZER_COMMAND] [-T TARGET_TOKENIZER_COMMAND] [--header] [--scol SCOL] [--tcol TCOL] [-b BLOCK_SIZE] [-p PROCESSES] [--batch_size BATCH_SIZE]
                             [--tmp_dir TMP_DIR] [--score_only] [--calibrated] [--raw_output] [--lm_threshold LM_THRESHOLD] [--disable_hardrules] [--disable_lm_filter] [--disable_porn_removal] [--disable_minimal_length]
                             [--run_all_rules] [--rules_config RULES_CONFIG] [--offline] [--auth_token AUTH_TOKEN] [-q] [--debug] [--logfile LOGFILE] [-v]
                             input [output] model

positional arguments:
  input                 Tab-separated files to be classified
  output                Output of the classification (default: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)
  model                 Path to model directory or HuggingFace Hub model identifier (such as 'bitextor/bicleaner-ai-full-en-fr')

options:
  -h, --help            show this help message and exit

Optional:
  -s SOURCE_LANG, --source_lang SOURCE_LANG
                        Overwrite model config source language (default: None)
  -t TARGET_LANG, --target_lang TARGET_LANG
                        Overwrite model config target language (default: None)
  -S SOURCE_TOKENIZER_COMMAND, --source_tokenizer_command SOURCE_TOKENIZER_COMMAND
                        Source language (SL) tokenizer full command (default: None)
  -T TARGET_TOKENIZER_COMMAND, --target_tokenizer_command TARGET_TOKENIZER_COMMAND
                        Target language (TL) tokenizer full command (default: None)
  --header              Input file will be expected to have a header, and the output will have a header as well (default: False)
  --scol SCOL           Source sentence column (starting in 1). The name of the field is expected instead of the position if --header is set (default: 3)
  --tcol TCOL           Target sentence column (starting in 1). The name of the field is expected instead of the position if --header is set (default: 4)
  -b BLOCK_SIZE, --block_size BLOCK_SIZE
                        Sentence pairs per block (default: 10000)
  -p PROCESSES, --processes PROCESSES
                        Option no longer available, please set BICLEANER_AI_THREADS environment variable (default: None)
  --batch_size BATCH_SIZE
                        Sentence pairs per block (default: 32)
  --tmp_dir TMP_DIR     Temporary directory where creating the temporary files of this program (default: /tmp)
  --score_only          Only output one column which is the bicleaner score (default: False)
  --calibrated          Output calibrated scores (default: False)
  --raw_output          Return raw output without computing positive class probability. (default: False)
  --lm_threshold LM_THRESHOLD
                        Threshold for language model fluency scoring. All TUs whose LM fluency score falls below the threshold will are removed (classifier score set to 0), unless the option --keep_lm_result set. (default: 0.5)
  --disable_hardrules   Disables the bicleaner_hardrules filtering (only bicleaner_classify is applied) (default: False)
  --disable_lm_filter   Disables LM filtering (default: False)
  --disable_porn_removal
                        Don't apply porn removal (default: False)
  --disable_minimal_length
                        Don't apply minimal length rule (default: False)
  --run_all_rules       Run all rules of Hardrules instead of stopping at first discard (default: False)
  --rules_config RULES_CONFIG
                        Hardrules configuration file (default: None)
  --offline             Don't try to download the model, instead try directly to load from local storage (default: False)
  --auth_token AUTH_TOKEN
                        Auth token for the Hugging Face Hub (default: None)

Logging:
  -q, --quiet           Silent logging mode (default: False)
  --debug               Debug logging mode (default: False)
  --logfile LOGFILE     Store log to a file (default: <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>)
  -v, --version         Show version of the package and exit

训练模型

Bicleaner AI 提供了一个命令行工具来训练自己的模型,以防现有模型不符合您的需求。有关快速入门和更多详细信息,请参阅我们的 训练文档

设置线程数

要设置训练或分类期间要使用的最大线程/进程数,--processes 选项不再可用。您需要将 BICLEANER_AI_THREADS 环境变量设置为所需值。例如

BICLEANER_AI_THREADS=12 bicleaner-ai-classify ...

如果未设置变量,则程序将使用所有可用的 CPU 内核。

速度

不同类型模型和硬件之间每秒句子数量的速度比较

模型 速度 CPUx1 速度 GPUx1
完整 1.78 行/秒 200 行/秒
轻量 600 行/秒 10,000 行/秒
  • CPU:Intel Core i9-9960X 单核(轻量模型批量 16,完整模型批量 1)
  • GPU:Nvidia V100(轻量模型批量 2048,完整模型批量 16)

引用

J. Zaragoza-Bernabeu, M. Bañón, G. Ramírez-Sánchez, S. Ortiz-Rojas,
"Bicleaner AI: Bicleaner Goes Neural",
《第13届语言资源与评估会议论文集》。
法国马赛:语言资源与评估会议,2022年6月

@inproceedings{zaragoza-bernabeu-etal-2022-bicleaner,
    title = {"Bicleaner {AI}: Bicleaner Goes Neural"},
    author = {"Zaragoza-Bernabeu, Jaume  and
      Ram{\'\i}rez-S{\'a}nchez, Gema  and
      Ba{\~n}{\'o}n, Marta  and
      Ortiz Rojas, Sergio"},
    booktitle = {"Proceedings of the Thirteenth Language Resources and Evaluation Conference"},
    month = jun,
    year = {"2022"},
    address = {"Marseille, France"},
    publisher = {"European Language Resources Association"},
    url = {"https://aclanthology.org/2022.lrec-1.87"},
    pages = {"824--831"},
    abstract = {"This paper describes the experiments carried out during the development of the latest version of Bicleaner, named Bicleaner AI, a tool that aims at detecting noisy sentences in parallel corpora. The tool, which now implements a new neural classifier, uses state-of-the-art techniques based on pre-trained transformer-based language models fine-tuned on a binary classification task. After that, parallel corpus filtering is performed, discarding the sentences that have lower probability of being mutual translations. Our experiments, based on the training of neural machine translation (NMT) with corpora filtered using Bicleaner AI for two different scenarios, show significant improvements in translation quality compared to the previous version of the tool which implemented a classifier based on Extremely Randomized Trees."},
}

Connecting Europe Facility

本存储库中包含的所有文档和软件仅反映作者的观点。欧盟创新和网络署不对其中包含的信息的任何使用承担责任。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

bicleaner_ai-3.1.0.tar.gz (82.0 kB 查看哈希值)

上传时间

构建分发

bicleaner_ai-3.1.0-py3-none-any.whl (74.6 kB 查看哈希值)

上传时间 Python 3

由以下组织支持

AWSAWS云计算和安全赞助商DatadogDatadog监控FastlyFastlyCDNGoogleGoogle下载分析MicrosoftMicrosoftPSF赞助商PingdomPingdom监控SentrySentry错误日志StatusPageStatusPage状态页面