Skip to content

Commit

Permalink
🎉 translation api init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Achraf KHAZRI authored and Achraf KHAZRI committed Oct 3, 2019
1 parent b382bd2 commit c8fbc74
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions translate_api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## Translate API
## :books: Documentation links
- Translate python lib : this library is not free, you can use it for a few examples in a period of time per day. [Link](https://pypi.org/project/translate/)
## Licence
GuideMeGlasses
:eyeglasses:
Binary file not shown.
37 changes: 37 additions & 0 deletions translate_api/translate_engine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from translate import Translator
from nltk.tokenize import sent_tokenize
import unicodedata


class TranslateEngine():

def __init__(self, language):

# Translator class instance
self.translator = Translator(to_lang=language)

def strip_accents(self, text):
return "".join(char for char in
unicodedata.normalize('NFKD', text)
if unicodedata.category(char) != 'Mn')

def translate(self, text):

# Output translated text variable
translated = ""

# Subdevise text into sentences because the translate can translate only 500 char per step
sentences = sent_tokenize(text)

# Loop over sentences
for s in sentences:

# Translate sentence
translation = self.translator.translate(s)

# Append all sentences to get all the text
translated = translated + " " + translation

# Remove accents, in french lang
output = self.strip_accents(translated)
return output

0 comments on commit c8fbc74

Please sign in to comment.