Apertium + Python

Contents

Usage

Analysis

Performing Morphological Analysis

Method 1: One can create Analyzer objects on which the analyze() method can be run.

In [1]: import apertium
In [2]: a = apertium.Analyzer('en')
In [3]: a.analyze('cats')
Out[3]: [cats/cat<n><pl>, ./.<sent>]

Method 2: Alternatively, the library provides an option to directly run the analyze() method.

In [1]: import apertium
In [2]: apertium.analyze('en', 'cats')
Out[2]: cats/cat<n><pl>

Generation

Performing Morphological Generation

Method 1: Just like the Analyzer, One can create Generator objects on which the generate() method can be run::

In [1]: import apertium
In [2]: g = apertium.Generator('en')
In [3]: g.generate('-cat<n><pl>$')
Out[3]: 'cats'

Method 2: Running generate() directly::

In [1]: import apertium
In [2]: apertium.generate('en', '-cat<n><pl>$')
Out[2]: 'cats'

Installing more modes from other language data

One can also install modes by providing the path to the lang-data using this simple function::

In [1]: import apertium
In [2]: apertium.append_pair_path('..')

Translation

Performing Translations::

In [1]: import apertium
In [2]: t = apertium.Translator('eng', 'spa')
In [3]: t.translate('cats')
Out[3]: 'Gatos'

About

Introduction

  • The code-base is in development for the GSoC ‘18 project called Apertium API in Python.
  • The Apertium core modules are written in C++.
  • This project is an attempt to make the Apertium modules available in python, which because of it’s simplicity is more appealing to users.

About the Existing Code Base

  • The existing code base has the subprocess implementation of the basic functions of Apertium.
  • A branch called windows has the implementation for the windows support and will soon be available on master. Detailed instructions can be found here

Apertium

exception apertium.ModeNotInstalled[source]
apertium.append_pair_path(pair_path)[source]
Parameters:pair_path (str) –

Analysis

class apertium.analysis.Analyzer(lang)[source]
analyzer_cmds

Dict[str, List[List[str]]]

lang

str

analyze(in_text, formatting='txt')[source]

Runs apertium to analyze the input

Parameters:
  • in_text (str) –
  • formatting (str) –
Returns:

List[LexicalUnit]

apertium.analysis.analyze(lang, in_text, formatting='txt')[source]
Parameters:
  • lang (str) –
  • in_text (str) –
  • formatting (str) –
Returns:

List[LexicalUnit]

Generation

class apertium.generation.Generator(lang)[source]
generation_cmds

Dict[str, List[List[str]]]

lang

str

generate(in_text, formatting='none')[source]
Parameters:
  • in_text (str) –
  • formatting (str) –
Returns:

Union[str, List[str]]

apertium.generation.generate(lang, in_text, formatting='none')[source]
Parameters:
  • lang (str) –
  • in_text (str) –
  • formatting (str) –
Returns:

Union[str, List[str]]

Translation

class apertium.translation.Translator(l1, l2)[source]
translation_cmds

Dict[Tuple[str, str], List[List[str]]]

l1

str

l2

str

translate(text, mark_unknown=False, format=None, deformat='txt', reformat='txt')[source]
Parameters:
  • text (str) –
  • mark_unknown (bool) –
  • format (Optional[str]) –
  • deformat (str) –
  • reformat (str) –
Returns:

str

apertium.translation.translate(l1, l2, text, mark_unknown=False, format=None, deformat='txt', reformat='txt')[source]
Parameters:
  • text (str) –
  • mark_unknown (bool) –
  • format (Optional[str]) –
  • deformat (str) –
  • reformat (str) –
Returns:

str

Indices and tables