diff --git a/docs/googletrans.rst b/docs/googletrans.rst index e995c2f..9c51c90 100644 --- a/docs/googletrans.rst +++ b/docs/googletrans.rst @@ -4,10 +4,18 @@ googletrans package Submodules ---------- -googletrans.translated module +googletrans.conversion module ----------------------------- -.. automodule:: googletrans.translated +.. automodule:: googletrans.conversion + :members: + :undoc-members: + :show-inheritance: + +googletrans.response module +--------------------------- + +.. automodule:: googletrans.response :members: :undoc-members: :show-inheritance: diff --git a/docs/modules.rst b/docs/modules.rst index 2c946b5..e2ad7d4 100644 --- a/docs/modules.rst +++ b/docs/modules.rst @@ -6,3 +6,4 @@ googletrans setup + tests diff --git a/docs/tests.rst b/docs/tests.rst new file mode 100644 index 0000000..03031c4 --- /dev/null +++ b/docs/tests.rst @@ -0,0 +1,7 @@ +tests module +============ + +.. automodule:: tests + :members: + :undoc-members: + :show-inheritance: diff --git a/googletrans/conversion.py b/googletrans/conversion.py new file mode 100644 index 0000000..1778176 --- /dev/null +++ b/googletrans/conversion.py @@ -0,0 +1,31 @@ +"""A conversion module for googletrans""" +import re +import json + +def format_json(text): + # save state + states = [] + for i, pos in enumerate(re.finditer('"', text)): + p = pos.start() + 1 + if i % 2 == 0: + nxt = text.find('"', p) + states.append((p, text[p:nxt])) + + # replace all weired characters in text + while text.find(',,') > -1: + text = text.replace(',,', ',null,') + while text.find('[,') > -1: + text = text.replace('[,', '[null,') + + # recover state + for i, pos in enumerate(re.finditer('"', text)): + p = pos.start() + 1 + if i % 2 == 0: + j = int(i / 2) + nxt = text.find('"', p) + # replacing a portion of a string + # use slicing to extract those parts of the original string to be kept + text = text[:p] + states[j][1] + text[nxt:] + + converted = json.loads(text) + return converted diff --git a/googletrans/translated.py b/googletrans/translated.py deleted file mode 100644 index dc0279f..0000000 --- a/googletrans/translated.py +++ /dev/null @@ -1,23 +0,0 @@ -class Translated: - """ - The Translated object, which contains Google Translator's result. - - :param src: source langauge (default: auto) - :param dest: destination language (default: en) - :param origin: original text - :param text: translated text - :param pronunciation: the pronunciation provided by Google Translator - """ - def __init__(self, src, dest, origin, text, pronunciation): - self.src = src - self.dest = dest - self.origin = origin - self.text = text - self.pronunciation = pronunciation - - def __str__(self): - return self.__unicode__() - - def __unicode__(self): - return ''.format( - src=self.src, dest=self.dest, text=self.text, pronunciation=self.pronunciation) \ No newline at end of file