Babelfish is an online language translation API provided by Yahoo. With it, you can translate text in a source language to a target language. NLTK comes with a simple interface for using it.
Be sure you are connected to the internet first. The
babelfish.translate()
function requires access to Yahoo's online API in order to work.
To translate your text, you first need to know two things:
The language of your text or source language.
The language you want to translate to or target language.
Language detection is outside the scope of this recipe, so we will assume you already know the source and target languages.
>>> from nltk.misc import babelfish >>> babelfish.translate('cookbook', 'english', 'spanish') 'libro de cocina' >>> babelfish.translate('libro de cocina', 'spanish', 'english') 'kitchen book' >>> babelfish.translate('cookbook', 'english', 'german') 'Kochbuch' >>> babelfish.translate...