fastspell


Namefastspell JSON
Version 0.11 PyPI version JSON
download
home_page
SummaryTargetted language identifier, based on FastText and Hunspell.
upload_time2023-10-23 10:28:06
maintainer
docs_urlNone
author
requires_python>=3.8
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # FastSpell
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/fastspell)
![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/mbanon/fastspell/release.yaml)

Targetted language identifier, based on FastText and Hunspell.

## How it works 

FastSpell will try to determine the language of a sentence by using **[FastText](https://fasttext.cc/)**.

If the language detected is very similar to the target language (i.e. FastText detected Spanish, while the targetted language is Galician), extra checks are performed with **[Hunspell](http://hunspell.github.io/)** to determine the language more precisely.


## Requirements & Installation

**FastSpell** can be installed from PyPI
```
pip install fastspell
```
or directly from source:
```
pip install .
```
Note that it requires Python3.8 or higher, and the `python3-dev` package:

```
sudo apt-get install python3-dev
```

**IMPORTANT**:
In some cases (for example, when using Python 3.10), the `cyhunspell` version 2.0.2 installation will fail. If that's the case, you need to install `cyhunspell==2.0.3` before installing `fastspell`:

First make sure build dependencies are installed
```
sudo apt install build-essential autoconf autopoint libtool
```
then install the pip package
```
pip install git+https://github.com/MSeal/cython_hunspell@2.0.3
```

### Model download

To trigger the FastText model download before running fastspell, run:
```
fastspell-download
```
Since version 0.7 all the dictionaries are installed automatically with pip and there is no need to do anything else.
For further explanation about how configuration works, [see below](#configuration).

### Conda
Also, you can install the conda package:
```
conda install -c conda-forge -c bitextor fastspell
```

### Automatic testing
Some automatic tests are provided to check that the installation went fine. In order to check it, go to the `/tests` directory and run:
```
python3 -m unittest discover
```
You might need to istall the `unittest` package with `pip`, in  case you don't have it installed beforehand.

## Configuration

A few configuration files are provided under the `fastspell/config` directory.
If you need to change default configuration, you can provide the path to your config directory with `-c`/`--config` or with the environment variable `FASTSPELL_CONFIG`.

#### similar.yaml

In this dictionary-like file, similar languages are stored. These are the languages that are going to be "double-checked" with Hunspell after being identified with FastText. For example, see the line `gl: [es, pt, gl] `. This means that, when the targetted language is Galician, and FastText identifies a given sentence as Spanish, Portuguese or Galician, extra checks will be performed with Hunspell to confirm which of the three similar languages is more suitable for the sentence.

Please note that you need Hunspell dictionaries for all the languages in this file (if you use the `fastspell-download` command, there is nothing else to do). This file can be modified to remove a language you are not interested in, or a language for which you don't have Hunspell dictionaries, or to add new similar or target languages.

#### hunspell.yaml

In this file, the names of the dictionaries are stored. All similar languages must be in this list in order to properly work.

For example, the first entry in the `hunspell_codes` is ` ca: ca_ES`, and the dictionary path is `~/.local/share/fastspell/`. That means that the Hunspell files for Catalan are  `~/.local/share/fastspell/ca_ES.dic` and `~/.local/share/fastspell/ca_ES.aff`.

By default `dicpath` is empty, which means FastSpell will look in these directories for the dictionaries:
```python
fastspell_dictionaries.__path__[0]
```
```
~/.local/share/fastspell
~/.local/share/hunspell
$VIRTUAL_ENV/share/hunspell
/usr/share/hunspell
```
To use a custom path, put it in `dicpath` and will be the first one to search.


## Usage

### Module:
In order to use **FastSpell** as a Python module, just install and import it :
```
from fastspell import FastSpell
```
Build a FastSpell object, like:
```
fsobj = FastSpell.FastSpell("en", mode="cons")
```
(learn more about modes in the section below)

And then use the `getlang` function with the sentences you want to identify, for example:
```
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
```


## Aggressive vs Conservative

FastSpell comes in two flavours: Aggressive and Conservative.

The **Aggressive** mode is less hesitant to tag a sentence with the target language, and never has doubts. The **Conservative** version, on the other hand, is more reluctant to tag a sentence with the target language and will use the `unk`(unknown) tag in case of doubt (when there is a tie between the target language and other language, for example)

## Benchmark 

![comparative.png](comparative.png)


## Usage example

Input text:
```
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?
```
Command:
```
fastspell  --aggr lang inputtext
fastspell  --cons lang inputtext
```
Aggressive output:
```
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
```

Conservative output:
```
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
```
Getting stats:
```
cat inputtext | fastspell --aggr $L | cut -f2 | sort | uniq -c | sort -nr
cat inputtext | fastspell --cons $L | cut -f2 | sort | uniq -c | sort -nr
```
Aggressive:
```
9 gl
1 es
```
Conservative:
```
6 gl
3 unk
1 es
```


---

![Connecting Europe Facility](https://www.paracrawl.eu/images/logo_en_cef273x39.png)

All documents and software contained in this repository reflect only the authors' view. The Innovation and Networks Executive Agency of the European Union is not responsible for any use that may be made of the information it contains.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "fastspell",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "Marta Ba\u00f1on <mbanon@prompsit.com>, Jaume Zaragoza <jzaragoza@prompsit.com>",
    "keywords": "",
    "author": "",
    "author_email": "Prompsit Language Engineering <info@prompsit.com>",
    "download_url": "https://files.pythonhosted.org/packages/c8/d6/68a183fdc05f690566154ab3557bb18cdf4876b2badf2fb1d708896abab7/fastspell-0.11.tar.gz",
    "platform": null,
    "description": "# FastSpell\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/fastspell)\n![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/mbanon/fastspell/release.yaml)\n\nTargetted language identifier, based on FastText and Hunspell.\n\n## How it works \n\nFastSpell will try to determine the language of a sentence by using **[FastText](https://fasttext.cc/)**.\n\nIf the language detected is very similar to the target language (i.e. FastText detected Spanish, while the targetted language is Galician), extra checks are performed with **[Hunspell](http://hunspell.github.io/)** to determine the language more precisely.\n\n\n## Requirements & Installation\n\n**FastSpell** can be installed from PyPI\n```\npip install fastspell\n```\nor directly from source:\n```\npip install .\n```\nNote that it requires Python3.8 or higher, and the `python3-dev` package:\n\n```\nsudo apt-get install python3-dev\n```\n\n**IMPORTANT**:\nIn some cases (for example, when using Python 3.10), the `cyhunspell` version 2.0.2 installation will fail. If that's the case, you need to install `cyhunspell==2.0.3` before installing `fastspell`:\n\nFirst make sure build dependencies are installed\n```\nsudo apt install build-essential autoconf autopoint libtool\n```\nthen install the pip package\n```\npip install git+https://github.com/MSeal/cython_hunspell@2.0.3\n```\n\n### Model download\n\nTo trigger the FastText model download before running fastspell, run:\n```\nfastspell-download\n```\nSince version 0.7 all the dictionaries are installed automatically with pip and there is no need to do anything else.\nFor further explanation about how configuration works, [see below](#configuration).\n\n### Conda\nAlso, you can install the conda package:\n```\nconda install -c conda-forge -c bitextor fastspell\n```\n\n### Automatic testing\nSome automatic tests are provided to check that the installation went fine. In order to check it, go to the `/tests` directory and run:\n```\npython3 -m unittest discover\n```\nYou might need to istall the `unittest` package with `pip`, in  case you don't have it installed beforehand.\n\n## Configuration\n\nA few configuration files are provided under the `fastspell/config` directory.\nIf you need to change default configuration, you can provide the path to your config directory with `-c`/`--config` or with the environment variable `FASTSPELL_CONFIG`.\n\n#### similar.yaml\n\nIn this dictionary-like file, similar languages are stored. These are the languages that are going to be \"double-checked\" with Hunspell after being identified with FastText. For example, see the line `gl: [es, pt, gl] `. This means that, when the targetted language is Galician, and FastText identifies a given sentence as Spanish, Portuguese or Galician, extra checks will be performed with Hunspell to confirm which of the three similar languages is more suitable for the sentence.\n\nPlease note that you need Hunspell dictionaries for all the languages in this file (if you use the `fastspell-download` command, there is nothing else to do). This file can be modified to remove a language you are not interested in, or a language for which you don't have Hunspell dictionaries, or to add new similar or target languages.\n\n#### hunspell.yaml\n\nIn this file, the names of the dictionaries are stored. All similar languages must be in this list in order to properly work.\n\nFor example, the first entry in the `hunspell_codes` is ` ca: ca_ES`, and the dictionary path is `~/.local/share/fastspell/`. That means that the Hunspell files for Catalan are  `~/.local/share/fastspell/ca_ES.dic` and `~/.local/share/fastspell/ca_ES.aff`.\n\nBy default `dicpath` is empty, which means FastSpell will look in these directories for the dictionaries:\n```python\nfastspell_dictionaries.__path__[0]\n```\n```\n~/.local/share/fastspell\n~/.local/share/hunspell\n$VIRTUAL_ENV/share/hunspell\n/usr/share/hunspell\n```\nTo use a custom path, put it in `dicpath` and will be the first one to search.\n\n\n## Usage\n\n### Module:\nIn order to use **FastSpell** as a Python module, just install and import it :\n```\nfrom fastspell import FastSpell\n```\nBuild a FastSpell object, like:\n```\nfsobj = FastSpell.FastSpell(\"en\", mode=\"cons\")\n```\n(learn more about modes in the section below)\n\nAnd then use the `getlang` function with the sentences you want to identify, for example:\n```\nfsobj.getlang(\"Hello, world\")\n#'en'\nfsobj.getlang(\"Hola, mundo\")\n#'es'\n\n```\n\n### CLI:\n```\niusage: fastspell [-h] [--aggr] [--cons] [--hbs] [-q] [--debug]\n                 [--logfile LOGFILE] [-v]\n                 lang [input] [output]\n\npositional arguments:\n  lang\n  input              Input sentences. (default: <_io.TextIOWrapper\n                     name='<stdin>' encoding='UTF-8'>)\n  output             Output of the language identification. (default:\n                     <_io.TextIOWrapper name='<stdout>' mode='w'\n                     encoding='UTF-8'>)\n\noptional arguments:\n  -h, --help         show this help message and exit\n  --aggr             Aggressive strategy (more positives) (default: False)\n  --cons             Conservative strategy (less positives) (default: False)\n  --hbs              Return all Serbo-Croatian variants as 'hbs' (default:\n                     False)\n\nLogging:\n  -q, --quiet        Silent logging mode (default: False)\n  --debug            Debug logging mode (default: False)\n  --logfile LOGFILE  Store log to a file (default: <_io.TextIOWrapper\n                     name='<stderr>' mode='w' encoding='UTF-8'>)\n  -v, --version      show version of this script and exit\n```\n\n\n## Aggressive vs Conservative\n\nFastSpell comes in two flavours: Aggressive and Conservative.\n\nThe **Aggressive** mode is less hesitant to tag a sentence with the target language, and never has doubts. The **Conservative** version, on the other hand, is more reluctant to tag a sentence with the target language and will use the `unk`(unknown) tag in case of doubt (when there is a tie between the target language and other language, for example)\n\n## Benchmark \n\n![comparative.png](comparative.png)\n\n\n## Usage example\n\nInput text:\n```\n19-01-2011 47 comentarios 7o Xornadas de Xardiner\u00eda de Galicia (RE)PLANTEAR\n\u2022 Proceso de valoraci\u00f3n de idoneidade: entrevistas psicosociais e visita domiciliaria e aplicaci\u00f3n de test psicol\u00f3xicos, se \u00e9 o caso.\n- Chrome e Firefox en MacOS non son compatibles (unicamente Safari \u00e9 compatible con MacOS), pero invocarase PSAL ao intentar empregar Chrome ou Firefox.\nMago da luz / Maga da luz\nCelebrada a homenaxe a Xos\u00e9 Manuel Seivane Rivas\nA instalaci\u00f3n el\u00e9ctrica en teletraballo\nSaltar \u00e1 navegaci\u00f3n Navegaci\u00f3n INICIO\nJulio Freire, competidor da FGA, invitado polo Kennel club de Inglaterra, para participar nos Crufts 2014 (Birmingham, 6 - 9 de marzo).\n25 de xullo - Truong Tan Sang toma posesi\u00f3n como presidente de Vietnam\nQuen pode solicitar o dito financiamento?\n```\nCommand:\n```\nfastspell  --aggr lang inputtext\nfastspell  --cons lang inputtext\n```\nAggressive output:\n```\n19-01-2011 47 comentarios 7o Xornadas de Xardiner\u00eda de Galicia (RE)PLANTEAR     gl\n\u2022 Proceso de valoraci\u00f3n de idoneidade: entrevistas psicosociais e visita domiciliaria e aplicaci\u00f3n de test psicol\u00f3xicos, se \u00e9 o caso.   gl\n- Chrome e Firefox en MacOS non son compatibles (unicamente Safari \u00e9 compatible con MacOS), pero invocarase PSAL ao intentar empregar Chrome ou Firefox.        gl\nMago da luz / Maga da luz       gl\nCelebrada a homenaxe a Xos\u00e9 Manuel Seivane Rivas        gl\nA instalaci\u00f3n el\u00e9ctrica en teletraballo gl\nSaltar \u00e1 navegaci\u00f3n Navegaci\u00f3n INICIO   gl\nJulio Freire, competidor da FGA, invitado polo Kennel club de Inglaterra, para participar nos Crufts 2014 (Birmingham, 6 - 9 de marzo). es\n25 de xullo - Truong Tan Sang toma posesi\u00f3n como presidente de Vietnam  gl\nQuen pode solicitar o dito financiamento?       gl\n```\n\nConservative output:\n```\n19-01-2011 47 comentarios 7o Xornadas de Xardiner\u00eda de Galicia (RE)PLANTEAR     unk\n\u2022 Proceso de valoraci\u00f3n de idoneidade: entrevistas psicosociais e visita domiciliaria e aplicaci\u00f3n de test psicol\u00f3xicos, se \u00e9 o caso.   gl\n- Chrome e Firefox en MacOS non son compatibles (unicamente Safari \u00e9 compatible con MacOS), pero invocarase PSAL ao intentar empregar Chrome ou Firefox.        gl\nMago da luz / Maga da luz       unk\nCelebrada a homenaxe a Xos\u00e9 Manuel Seivane Rivas        gl\nA instalaci\u00f3n el\u00e9ctrica en teletraballo unk\nSaltar \u00e1 navegaci\u00f3n Navegaci\u00f3n INICIO   gl\nJulio Freire, competidor da FGA, invitado polo Kennel club de Inglaterra, para participar nos Crufts 2014 (Birmingham, 6 - 9 de marzo). es\n25 de xullo - Truong Tan Sang toma posesi\u00f3n como presidente de Vietnam  gl\nQuen pode solicitar o dito financiamento?       gl\n```\nGetting stats:\n```\ncat inputtext | fastspell --aggr $L | cut -f2 | sort | uniq -c | sort -nr\ncat inputtext | fastspell --cons $L | cut -f2 | sort | uniq -c | sort -nr\n```\nAggressive:\n```\n9 gl\n1 es\n```\nConservative:\n```\n6 gl\n3 unk\n1 es\n```\n\n\n---\n\n![Connecting Europe Facility](https://www.paracrawl.eu/images/logo_en_cef273x39.png)\n\nAll documents and software contained in this repository reflect only the authors' view. The Innovation and Networks Executive Agency of the European Union is not responsible for any use that may be made of the information it contains.\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Targetted language identifier, based on FastText and Hunspell.",
    "version": "0.11",
    "project_urls": {
        "FastSpell on GitHub": "https://github.com/mbanon/fastspell",
        "HPLT Project": "https://hplt-project.org",
        "Homepage": "https://github.com/mbanon/fastspell",
        "MaCoCu": "https://macocu.eu",
        "Paracrawl": "https://paracrawl.eu/",
        "Prompsit Language Engineering": "http://www.prompsit.com"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9523e837768b8bd548e1b3b607198fd8d179ffe2f9d68ad867192185cf1ae7b4",
                "md5": "7b7d93997b51da31a0290c167eeb563c",
                "sha256": "ba63fe40425e30e0a387f90ff449dc943d30a4a4760ae8fbf85fb572867d076b"
            },
            "downloads": -1,
            "filename": "fastspell-0.11-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7b7d93997b51da31a0290c167eeb563c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 15139,
            "upload_time": "2023-10-23T10:28:05",
            "upload_time_iso_8601": "2023-10-23T10:28:05.195561Z",
            "url": "https://files.pythonhosted.org/packages/95/23/e837768b8bd548e1b3b607198fd8d179ffe2f9d68ad867192185cf1ae7b4/fastspell-0.11-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c8d668a183fdc05f690566154ab3557bb18cdf4876b2badf2fb1d708896abab7",
                "md5": "2b31cef2d1daab0426fa8ec0fcb30398",
                "sha256": "080fc28b9049bc4d0a3059296603ef1698ccb626fa839647016f4b28c3c11623"
            },
            "downloads": -1,
            "filename": "fastspell-0.11.tar.gz",
            "has_sig": false,
            "md5_digest": "2b31cef2d1daab0426fa8ec0fcb30398",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 17663,
            "upload_time": "2023-10-23T10:28:06",
            "upload_time_iso_8601": "2023-10-23T10:28:06.843677Z",
            "url": "https://files.pythonhosted.org/packages/c8/d6/68a183fdc05f690566154ab3557bb18cdf4876b2badf2fb1d708896abab7/fastspell-0.11.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-23 10:28:06",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "mbanon",
    "github_project": "fastspell",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "fastspell"
}
        
Elapsed time: 0.18305s