{"id":150,"date":"2023-03-17T11:55:51","date_gmt":"2023-03-17T11:55:51","guid":{"rendered":"https:\/\/smartsource.com.sg\/blog\/?p=150"},"modified":"2023-03-17T11:55:51","modified_gmt":"2023-03-17T11:55:51","slug":"back-translation-of-a-sample-dataset-using-googletrans","status":"publish","type":"post","link":"https:\/\/smartsource.com.sg\/blog\/index.php\/2023\/03\/17\/back-translation-of-a-sample-dataset-using-googletrans\/","title":{"rendered":"Back Translation of a sample dataset using googletrans"},"content":{"rendered":"\n<p>Here&#8217;s an example Python code for Back Translation of a sample dataset using the <code>googletrans<\/code> library:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>from googletrans import Translator\nimport random\n\ntranslator = Translator()\n\ndef back_translate(sentence, lang):\n    \"\"\"\n    Translates a sentence to the given language and back to the original language\n    \"\"\"\n    try:\n        # Translate to the given language\n        translation = translator.translate(sentence, dest=lang)\n        # Translate back to the original language\n        back_translation = translator.translate(translation.text, dest='en')\n        return back_translation.text\n    except:\n        # Return the original sentence if there's an error with translation\n        return sentence\n\n# Example usage\ndataset = [\"The quick brown fox jumps over the lazy dog\",\n           \"I love to code in Python\",\n           \"Artificial Intelligence is the future\"]\n\nback_translated = []\n\nfor sentence in dataset:\n    # Randomly select a language other than English\n    lang = random.choice(['fr', 'es', 'de', 'ru', 'ja', 'ko'])\n    # Back translate the sentence to English\n    back_translated_sentence = back_translate(sentence, lang)\n    back_translated.append(back_translated_sentence)\n\nprint(\"Original Dataset: \", dataset)\nprint(\"Back Translated Dataset: \", back_translated)\n<\/code><\/pre>\n\n\n\n<p>This code uses the <code>googletrans<\/code> library to translate the sentences to a random language other than English and then back to English to create a new variation of the sentence. The <code>back_translate()<\/code> function takes in a sentence and a language code as input, translates the sentence to the given language, and then translates it back to English. The <code>random.choice()<\/code> method is used to randomly select a language from a list of possible languages. The back translated sentences are stored in a new list <code>back_translated<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s an example Python code for Back Translation of a sample dataset using the googletrans library: from googletrans import Translator import random translator = Translator() def back_translate(sentence, lang): &#8220;&#8221;&#8221; Translates&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[109,68,115,64],"class_list":["post-150","post","type-post","status-publish","format-standard","hentry","category-tutorials","tag-back-translation","tag-dataset","tag-googletrans","tag-python"],"_links":{"self":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts\/150","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=150"}],"version-history":[{"count":1,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts\/150\/revisions"}],"predecessor-version":[{"id":151,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts\/150\/revisions\/151"}],"wp:attachment":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}