Skip to content

reynoldsnlp/hfst-python

Repository files navigation

HFST for python

CI/CD CI - Test, Build, and Publish
Package PyPI - Version PyPI - Downloads PyPI - Python Version
Meta CI Buildwheel Badge linting - Ruff PyPI - License

Package description

Package hfst contains python bindings for the Helsinki Finite-State Technology (HFST) C++ library. HFST toolkit is intended for processing natural language morphologies. The toolkit is demonstrated by wide-coverage implementations of a number of languages of varying morphological complexity.

Installation

For most users, simply run...

$ python3 -m pip install hfst

On some OSes, you can install hfst python bindings by running install_nightly.sh.

We compile wheels using cibuildwheel which enables us to publish wheels for CPython and PyPy on a large variety of OS/architecture combinations. If wheels for your platform are not available, open an issue! (Windows support coming soon!)

Usage

C++ side functions and classes are wrapped with SWIG under module 'libhfst'. It is possible to use this module directly, but there is a package named 'hfst' which encapsulates the libhfst module in a more user-friendly manner. The structure of the package is

  • hfst
    • hfst.exceptions
    • hfst.sfst_rules
    • hfst.xerox_rules

The module hfst.exceptions contains HfstException and its subclasses. The modules hfst.sfst_rules and hfst.xerox_rules contain functions that create transducers implementing replace and two-level rules. All other functions and classes are in module hfst.

For documentation and examples, see https://hfst.github.io/python/index.html.

Requirements

Compiling hfst from source requires at least C++ compiler (tested with gcc 5.4.0), readline and getline libraries and setuptools package for python (tested with version 28.8.0). Swig is no longer needed as pre-generated files are included in source distribution.

Compiling from scratch

This repository has a submodule with the underlying C++ code. The first time you clone this repository, run $ git submodule init to initialize the submodule. Thereafter, every time that you want to pull in the latest changes from the C++ hfst repository, run $ git submodule update --remote or $ git pull --recurse-submodules. See the manylinux build script for an example of how to compile the underlying C++ library.

Once the library is available, the package can be installed by running...

python3 -m pip install .

...in the root directory of the repository.

Running tests

Tests are contained in the test/ directory. To run tests, you must first install pytest using python3 -m pip install pytest. Then, in the root directory of this repository, run python3 -m pytest.

Documentation

See wiki-based package documentation on our Github pages. In python, you can also use dir and help commands, e.g.:

dir(hfst)

help(hfst.HfstTransducer)

License

HFST is licensed under Gnu GPL version 3.0.

Troubleshooting

Pip starts to compile from source although there is a wheel available

Try upgrading pip with

python3 -m pip install --upgrade pip

Another reason for this can be that the source package on PyPI is newer (i.e. has a higher version number) than the corresponding wheel for the given environment. Report this via our issue tracker so a fresh wheel can be created.

Error message "command ... failed with error code ..."

Try rerunning pip in verbose mode with

python3 -m pip install --verbose [--upgrade] hfst

to get more information.

TypeError: catching classes that do not inherit from BaseException is not allowed

Some version combinations of SWIG and Python make HFST exception classes subclasses of Python's object instead of Exception. Then you will get the error above. If this is the case, run...

sed -i 's/class HfstException(_object):/class HfstException(Exception):/' libhfst.py

...after build/installation to be able to use HfstException and its subclasses in Python.

Links

HFST project main page: more information about the project

Github issue tracker: for comments, feature requests and bug reports