Skip to content

Commit c8fbc74

Browse files
Achraf KHAZRIAchraf KHAZRI
authored andcommitted
🎉 translation api init commit
1 parent b382bd2 commit c8fbc74

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

translate_api/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Translate API
2+
## :books: Documentation links
3+
- 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/)
4+
## Licence
5+
GuideMeGlasses
6+
:eyeglasses:
Binary file not shown.

translate_api/translate_engine.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from translate import Translator
2+
from nltk.tokenize import sent_tokenize
3+
import unicodedata
4+
5+
6+
class TranslateEngine():
7+
8+
def __init__(self, language):
9+
10+
# Translator class instance
11+
self.translator = Translator(to_lang=language)
12+
13+
def strip_accents(self, text):
14+
return "".join(char for char in
15+
unicodedata.normalize('NFKD', text)
16+
if unicodedata.category(char) != 'Mn')
17+
18+
def translate(self, text):
19+
20+
# Output translated text variable
21+
translated = ""
22+
23+
# Subdevise text into sentences because the translate can translate only 500 char per step
24+
sentences = sent_tokenize(text)
25+
26+
# Loop over sentences
27+
for s in sentences:
28+
29+
# Translate sentence
30+
translation = self.translator.translate(s)
31+
32+
# Append all sentences to get all the text
33+
translated = translated + " " + translation
34+
35+
# Remove accents, in french lang
36+
output = self.strip_accents(translated)
37+
return output

0 commit comments

Comments
 (0)