Skip to content

Machine Translation

Mika Hämäläinen edited this page Nov 28, 2024 · 8 revisions

Introduction

UralicNLP provides functionality to connect to different translation services over their APIs. The main supported services are Apertium and Yandex

The translator API

Translator objects have two methods get_languages() and translate(text, source, target). Get_languages gives a dictionary consisting of language codes of the supported languages. The keys indicate the supported source language and the values the supported target languages.

translator.get_languages()
>> {u'est': [u'est_estspell', u'est_estgram'], u'nob': [u'fao'], u'smn': [u'fin', u'sme'], u'fao': [u'nno', u'fao_faospell', u'fao_faogram', u'nob'], u'sme': [u'fin', u'sme_smespell', u'sma', u'sme_smegram', u'nob', u'smj', u'smn'], u'fin': [u'sme', u'smn', u'fin_fingram', u'fin_finspell'], u'sma': [u'sme', u'sma_smagram', u'nob', u'sma_smaspell'], u'smj': [u'nob', u'sme', u'smj_smjgram', u'smj_smjspell']}

TartuNLP

TartuNLP API is one of the supported translation tools. The constructor takes an optional argument domain which defaults to domain="general".

from uralicNLP.translate import *
t = TartuTranslator()
t.translate("Hello, how are you?", "eng", "fin")
>> "Hei, miten voit?"

Apertium

UralicNLP supports the API of Apertium based translators

from uralicNLP.translate import *
translator = ApertiumGiellateknoTranslator()
translator.translate("kissa juoksee kovaa", "fin","sme")
>> "gáhttu viehká garrasa"

The previous example uses the Giellatekno Apertium, other possible translators are ApertiumBetaTranslator(), ApertiumStableTranslator() and ApertiumLocalhostTranslator() for a local installation of Apertium. It is also possible to initialize an ApertiumTranslator with a custom URL ApertiumTranslator("http://yourapertium.com/apy/").

Yandex

Yandex translator requires a Yandex API key to function.

from uralicNLP.translate import *
api_key = "your-api-key"
translator = YandexTranslator(api_key)
translator.translate("kissa juoksee puuhun", "fi", "ru")
Clone this wiki locally