跳转到主要内容

目标语言识别器,基于FastText和Hunspell。

项目描述

FastSpell

PyPI - Python Version GitHub Workflow Status

目标语言识别器,基于FastText和Hunspell。

工作原理

FastSpell将尝试通过使用FastText来确定句子的语言。

如果检测到的语言与目标语言非常相似(即FastText检测到西班牙语,而目标语言为加利西亚语),则将使用Hunspell进行额外的检查,以更精确地确定语言。

引用

如果您觉得FastSpell很有用,请考虑引用以下论文

Marta Bañón,Gema Ramírez-Sánchez,Jaume Zaragoza-Bernabeu,和Sergio Ortiz Rojas,
"FastSpell: The LangId Magic Spell",
2024年联合国际计算语言学、语言资源和评估会议(LREC-COLING 2024)论文集 中,
第 7133–7140 页,意大利都灵。ELRA 和 ICCL。

@inproceedings{banon-etal-2024-fastspell-langid,
    title = "{F}ast{S}pell: The {L}ang{I}d Magic Spell",
    author = "Ba{\~n}{\'o}n, Marta  and
      Ram{\'\i}rez-S{\'a}nchez, Gema  and
      Zaragoza-Bernabeu, Jaume  and
      Ortiz Rojas, Sergio",
    editor = "Calzolari, Nicoletta  and
      Kan, Min-Yen  and
      Hoste, Veronique  and
      Lenci, Alessandro  and
      Sakti, Sakriani  and
      Xue, Nianwen",
    booktitle = "Proceedings of the 2024 Joint International Conference on Computational Linguistics, Language Resources and Evaluation (LREC-COLING 2024)",
    month = may,
    year = "2024",
    address = "Torino, Italia",
    publisher = "ELRA and ICCL",
    url = "https://aclanthology.org/2024.lrec-main.626",
    pages = "7133--7140",
    abstract = "Language identification is a crucial component in the automated production of language resources, particularly in multilingual and big data contexts. However, commonly used language identifiers struggle to differentiate between similar or closely-related languages. This paper introduces FastSpell, a language identifier that combines fastText (a pre-trained language identifier tool) and Hunspell (a spell checker) with the aim of having a refined second-opinion before deciding which language should be assigned to a text. We provide a description of the FastSpell algorithm along with an explanation on how to use and configure it. To that end, we motivate the need of such a tool and present a benchmark including some popular language identifiers evaluated during the development of FastSpell. We show how FastSpell is useful not only to improve identification of similar languages, but also to identify new ones ignored by other tools.",
}

需求与安装

FastSpell 可以从 PyPI 安装

pip install fastspell

或直接从源代码安装

pip install .

注意:它需要 Python3.8 或更高版本,以及 python3-dev

sudo apt-get install python3-dev

重要:在某些情况下(例如,使用 Python 3.10),cyhunspell 版本 2.0.2 的安装将失败。如果是这种情况,您需要先安装 cyhunspell==2.0.3,然后再安装 fastspell

首先确保已安装构建依赖项

sudo apt install build-essential autoconf autopoint libtool

然后安装 pip 包

pip install git+https://github.com/MSeal/cython_hunspell@2.0.3

模型下载

在运行 fastspell 之前触发 FastText 模型下载,请运行

fastspell-download

从版本 0.7 开始,所有字典都由 pip 自动安装,无需执行其他操作。有关配置工作原理的进一步说明,请见下文。

Conda

您还可以安装 conda 包

conda install -c conda-forge -c bitextor fastspell

自动测试

提供了一些自动测试来检查安装是否成功。要检查它,请进入 /tests 目录并运行

python3 -m unittest discover

如果您事先未安装,可能需要使用 pip 安装 unittest 包。

配置

fastspell/config 目录下提供了一些配置文件。如果您需要更改默认配置,可以使用 -c/--config 或环境变量 FASTSPELL_CONFIG 提供您的配置目录路径。

similar.yaml

在这个类似字典的文件中,存储了相似语言。这些是在使用 FastText 识别后将要与 Hunspell 进行“双重检查”的语言。例如,请参阅 gl: [es, pt, gl] 这一行。这意味着,当目标语言是加利西亚语时,如果 FastText 将某个句子识别为西班牙语、葡萄牙语或加利西亚语,将使用 Hunspell 进行额外的检查以确认这三个相似语言中哪一个更适合该句子。

请注意,您需要此文件中所有语言的 Hunspell 字典(如果您使用 fastspell-download 命令,则无需执行其他操作)。您可以修改此文件以删除不感兴趣的语言或您没有 Hunspell 字典的语言,或添加新的相似或目标语言。

hunspell.yaml

在此文件中存储了字典的名称。所有相似语言都必须在此列表中才能正常工作。

例如,hunspell_codes 的第一个条目是 ca: ca_ES,字典路径是 ~/.local/share/fastspell/。这意味着加泰罗尼亚语的 Hunspell 文件是 ~/.local/share/fastspell/ca_ES.dic~/.local/share/fastspell/ca_ES.aff

默认情况下 dicpath 为空,这意味着 FastSpell 将在这些目录中查找字典

fastspell_dictionaries.__path__[0]
~/.local/share/fastspell
~/.local/share/hunspell
$VIRTUAL_ENV/share/hunspell
/usr/share/hunspell

要使用自定义路径,请将其放入 dicpath 中,它将是第一个搜索的。

用法

模块

要使用 FastSpell 作为 Python 模块,只需安装并导入它

from fastspell import FastSpell

构建 FastSpell 对象,例如

fsobj = FastSpell.FastSpell("en", mode="cons")

(在下面的部分中了解更多关于模式的信息)

然后使用 getlang 函数识别您想要识别的句子,例如

fsobj.getlang("Hello, world")
#'en'
fsobj.getlang("Hola, mundo")
#'es'

命令行界面(CLI)

iusage: fastspell [-h] [--aggr] [--cons] [--hbs] [-q] [--debug]
                 [--logfile LOGFILE] [-v]
                 lang [input] [output]

positional arguments:
  lang
  input              Input sentences. (default: <_io.TextIOWrapper
                     name='<stdin>' encoding='UTF-8'>)
  output             Output of the language identification. (default:
                     <_io.TextIOWrapper name='<stdout>' mode='w'
                     encoding='UTF-8'>)

optional arguments:
  -h, --help         show this help message and exit
  --aggr             Aggressive strategy (more positives) (default: False)
  --cons             Conservative strategy (less positives) (default: False)
  --hbs              Return all Serbo-Croatian variants as 'hbs' (default:
                     False)

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 this script and exit

激进型与保守型

FastSpell 有两种版本:激进型和保守型。

激进型不太犹豫将句子标记为目标语言,并且从不犹豫。另一方面,保守型更不愿意将句子标记为目标语言,并在有疑问的情况下使用 unk(未知) 标签(当目标语言与其他语言存在分歧时,例如)

基准测试

基准数据:[https://github.com/mbanon/benchmarks](https://github.com/mbanon/benchmarks)

结果:https://docs.google.com/spreadsheets/d/158ZRWMgRH5TptlFWpKyh5uRL5jTkKW1d4KGJg1AZf7A/edit?usp=sharing

使用示例

输入文本

19-01-2011 47 comentarios 7o Xornadas de Xardinería de Galicia (RE)PLANTEAR
• Proceso de valoración de idoneidade: entrevistas psicosociais e visita domiciliaria e aplicación de test psicolóxicos, se é o caso.
- Chrome e Firefox en MacOS non son compatibles (unicamente Safari é compatible con MacOS), pero invocarase PSAL ao intentar empregar Chrome ou Firefox.
Mago da luz / Maga da luz
Celebrada a homenaxe a Xosé Manuel Seivane Rivas
A instalación eléctrica en teletraballo
Saltar á navegación Navegación INICIO
Julio Freire, competidor da FGA, invitado polo Kennel club de Inglaterra, para participar nos Crufts 2014 (Birmingham, 6 - 9 de marzo).
25 de xullo - Truong Tan Sang toma posesión como presidente de Vietnam
Quen pode solicitar o dito financiamento?

命令

fastspell  --aggr lang inputtext
fastspell  --cons lang inputtext

激进输出

19-01-2011 47 comentarios 7o Xornadas de Xardinería de Galicia (RE)PLANTEAR     gl
• Proceso de valoración de idoneidade: entrevistas psicosociais e visita domiciliaria e aplicación de test psicolóxicos, se é o caso.   gl
- Chrome e Firefox en MacOS non son compatibles (unicamente Safari é compatible con MacOS), pero invocarase PSAL ao intentar empregar Chrome ou Firefox.        gl
Mago da luz / Maga da luz       gl
Celebrada a homenaxe a Xosé Manuel Seivane Rivas        gl
A instalación eléctrica en teletraballo gl
Saltar á navegación Navegación INICIO   gl
Julio Freire, competidor da FGA, invitado polo Kennel club de Inglaterra, para participar nos Crufts 2014 (Birmingham, 6 - 9 de marzo). es
25 de xullo - Truong Tan Sang toma posesión como presidente de Vietnam  gl
Quen pode solicitar o dito financiamento?       gl

保守输出

19-01-2011 47 comentarios 7o Xornadas de Xardinería de Galicia (RE)PLANTEAR     unk
• Proceso de valoración de idoneidade: entrevistas psicosociais e visita domiciliaria e aplicación de test psicolóxicos, se é o caso.   gl
- Chrome e Firefox en MacOS non son compatibles (unicamente Safari é compatible con MacOS), pero invocarase PSAL ao intentar empregar Chrome ou Firefox.        gl
Mago da luz / Maga da luz       unk
Celebrada a homenaxe a Xosé Manuel Seivane Rivas        gl
A instalación eléctrica en teletraballo unk
Saltar á navegación Navegación INICIO   gl
Julio Freire, competidor da FGA, invitado polo Kennel club de Inglaterra, para participar nos Crufts 2014 (Birmingham, 6 - 9 de marzo). es
25 de xullo - Truong Tan Sang toma posesión como presidente de Vietnam  gl
Quen pode solicitar o dito financiamento?       gl

获取统计数据

cat inputtext | fastspell --aggr $L | cut -f2 | sort | uniq -c | sort -nr
cat inputtext | fastspell --cons $L | cut -f2 | sort | uniq -c | sort -nr

激进

9 gl
1 es

保守

6 gl
3 unk
1 es

Connecting Europe Facility

本存储库中包含的所有文档和软件仅反映作者的观点。欧洲联盟创新和网络执行机构不对其中包含的信息的任何使用负责。

项目详情


下载文件

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

源分布

fastspell-0.11.1.tar.gz (56.1 kB 查看哈希值)

上传时间 源代码

构建分布

fastspell-0.11.1-py3-none-any.whl (40.6 kB 查看哈希值)

上传时间 Python 3

由以下机构支持