10

Is it possible to use spacy to translate this sentence into some other language, for e.g. french?

import spacy
nlp = spacy.load('en')
doc = nlp(u'This is a sentence.')

If spacy is not the right tool for this, then which (Free and open source) python library can translate text?

shantanuo
  • 31,689
  • 78
  • 245
  • 403
  • 1
    I have not seen spacy do translation, but your question could have an answer here: https://stackoverflow.com/questions/1316386/translating-human-languages-in-python The best I have seen are those library that communicates with Google Translate API – Oluwatobi Samuel Omisakin Jul 23 '18 at 09:16

2 Answers2

9

The comment to your question is correct. You cannot use spaCy to translate text. A good open-source solution could be this library. Sample code:

from translate import Translator
translator = Translator(from_lang='el', to_lang='en')
translation = translator.translate("Ο όμορφος άντρας")
'''

You can the use spacy to perform comon NLP tasks, such as tokenization and
lemmatization in your desired language.

'''
import spacy
nlp = spacy.load('en')
doc = nlp(translation)
for token in doc:
    print(token, token.lemma_)

Output:

The the

handsome handsome

man man

Hope it helps!

Community
  • 1
  • 1
gdaras
  • 9,401
  • 2
  • 23
  • 39
  • __translate__ library has a limit so i prefer to advise using google translate. You can use; `#import library from googletrans import Translator, constants # init the Google API translator translator = Translator() # translate a Turkish text to English text="Python, nesne yönelimli, yorumlamalı, birimsel (modüler) ve etkileşimli yüksek seviyeli bir programlama dilidir.Girintilere dayalı basit sözdizimi, dilin öğrenilmesini ve akılda kalmasını kolaylaştırır. " translation = translator.translate(content, src='tr', des='en') print(translation.text)` – msklc Sep 08 '20 at 10:38
  • 1
    But googletrans is not official library, it bypass google token mechanism they have written in documentation "please use this library if you don’t care about stability.If you get HTTP 5xx error or errors like #6, it’s probably because Google has banned your client IP address. " have a look https://py-googletrans.readthedocs.io/en/latest/ – Tomato Master Feb 24 '21 at 14:42
3

Spacy is not for translation. Spacy is for NER(Named Entity Recognition). you can use the python library called translate. You can find a sample project here

Darkknight
  • 1,716
  • 10
  • 23