Skip to content

Instantly share code, notes, and snippets.

@chetanambi
Created August 9, 2020 17:43
Show Gist options
  • Save chetanambi/9a019f8ce62eb7709dc1464944b9c67b to your computer and use it in GitHub Desktop.
Save chetanambi/9a019f8ce62eb7709dc1464944b9c67b to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "googletrans_example.ipynb",
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "code",
"metadata": {
"id": "jXN3xI6EKmyk",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 759
},
"outputId": "fb9341b5-887d-463f-87da-d4a35251b3ad"
},
"source": [
"!pip install googletrans"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": [
"Collecting googletrans\n",
" Downloading https://files.pythonhosted.org/packages/71/3a/3b19effdd4c03958b90f40fe01c93de6d5280e03843cc5adf6956bfc9512/googletrans-3.0.0.tar.gz\n",
"Collecting httpx==0.13.3\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/54/b4/698b284c6aed4d7c2b4fe3ba5df1fcf6093612423797e76fbb24890dd22f/httpx-0.13.3-py3-none-any.whl (55kB)\n",
"\u001b[K |████████████████████████████████| 61kB 2.9MB/s \n",
"\u001b[?25hRequirement already satisfied: chardet==3.* in /usr/local/lib/python3.6/dist-packages (from httpx==0.13.3->googletrans) (3.0.4)\n",
"Collecting rfc3986<2,>=1.3\n",
" Downloading https://files.pythonhosted.org/packages/78/be/7b8b99fd74ff5684225f50dd0e865393d2265656ef3b4ba9eaaaffe622b8/rfc3986-1.4.0-py2.py3-none-any.whl\n",
"Collecting hstspreload\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/45/c1/732c4f4dd2f1340388e36d6fd9ae601013e93595890497d3854f533307c6/hstspreload-2020.8.8-py3-none-any.whl (930kB)\n",
"\u001b[K |████████████████████████████████| 931kB 7.9MB/s \n",
"\u001b[?25hCollecting httpcore==0.9.*\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/dd/d5/e4ff9318693ac6101a2095e580908b591838c6f33df8d3ee8dd953ba96a8/httpcore-0.9.1-py3-none-any.whl (42kB)\n",
"\u001b[K |████████████████████████████████| 51kB 7.4MB/s \n",
"\u001b[?25hRequirement already satisfied: certifi in /usr/local/lib/python3.6/dist-packages (from httpx==0.13.3->googletrans) (2020.6.20)\n",
"Collecting sniffio\n",
" Downloading https://files.pythonhosted.org/packages/b3/82/4bd4b7d9c0d1dc0fbfbc2a1e00138e7f3ab85bc239358fe9b78aa2ab586d/sniffio-1.1.0-py3-none-any.whl\n",
"Requirement already satisfied: idna==2.* in /usr/local/lib/python3.6/dist-packages (from httpx==0.13.3->googletrans) (2.10)\n",
"Collecting h11<0.10,>=0.8\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/5a/fd/3dad730b0f95e78aeeb742f96fa7bbecbdd56a58e405d3da440d5bfb90c6/h11-0.9.0-py2.py3-none-any.whl (53kB)\n",
"\u001b[K |████████████████████████████████| 61kB 7.5MB/s \n",
"\u001b[?25hCollecting h2==3.*\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/25/de/da019bcc539eeab02f6d45836f23858ac467f584bfec7a526ef200242afe/h2-3.2.0-py2.py3-none-any.whl (65kB)\n",
"\u001b[K |████████████████████████████████| 71kB 8.3MB/s \n",
"\u001b[?25hCollecting contextvars>=2.1; python_version < \"3.7\"\n",
" Downloading https://files.pythonhosted.org/packages/83/96/55b82d9f13763be9d672622e1b8106c85acb83edd7cc2fa5bc67cd9877e9/contextvars-2.4.tar.gz\n",
"Collecting hpack<4,>=3.0\n",
" Downloading https://files.pythonhosted.org/packages/8a/cc/e53517f4a1e13f74776ca93271caef378dadec14d71c61c949d759d3db69/hpack-3.0.0-py2.py3-none-any.whl\n",
"Collecting hyperframe<6,>=5.2.0\n",
" Downloading https://files.pythonhosted.org/packages/19/0c/bf88182bcb5dce3094e2f3e4fe20db28a9928cb7bd5b08024030e4b140db/hyperframe-5.2.0-py2.py3-none-any.whl\n",
"Collecting immutables>=0.9\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/99/e0/ea6fd4697120327d26773b5a84853f897a68e33d3f9376b00a8ff96e4f63/immutables-0.14-cp36-cp36m-manylinux1_x86_64.whl (98kB)\n",
"\u001b[K |████████████████████████████████| 102kB 9.1MB/s \n",
"\u001b[?25hBuilding wheels for collected packages: googletrans, contextvars\n",
" Building wheel for googletrans (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Created wheel for googletrans: filename=googletrans-3.0.0-cp36-none-any.whl size=15736 sha256=ff7b2a4e87aa0b1a99d73d7fc65f520eda57c43d965be132208c22d2ed63b9b8\n",
" Stored in directory: /root/.cache/pip/wheels/28/1a/a7/eaf4d7a3417a0c65796c547cff4deb6d79c7d14c2abd29273e\n",
" Building wheel for contextvars (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Created wheel for contextvars: filename=contextvars-2.4-cp36-none-any.whl size=7666 sha256=20946ee05dbf98289686f0fd61c06e941485fac64829c40a417616d44bc2efae\n",
" Stored in directory: /root/.cache/pip/wheels/a5/7d/68/1ebae2668bda2228686e3c1cf16f2c2384cea6e9334ad5f6de\n",
"Successfully built googletrans contextvars\n",
"Installing collected packages: rfc3986, hstspreload, immutables, contextvars, sniffio, h11, hpack, hyperframe, h2, httpcore, httpx, googletrans\n",
"Successfully installed contextvars-2.4 googletrans-3.0.0 h11-0.9.0 h2-3.2.0 hpack-3.0.0 hstspreload-2020.8.8 httpcore-0.9.1 httpx-0.13.3 hyperframe-5.2.0 immutables-0.14 rfc3986-1.4.0 sniffio-1.1.0\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Ev0ieGEuLOqj",
"colab_type": "code",
"colab": {}
},
"source": [
"from googletrans import Translator\n",
"translator = Translator()"
],
"execution_count": 2,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "Hrca2SoHSxc2",
"colab_type": "text"
},
"source": [
"Example 1:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "S6Qq9goiQyIy",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "23d21528-e168-4239-faed-e1fa9337f98c"
},
"source": [
"origin_text = \"Leadership requires two things: a vision of the world that does not yet exist and the ability to communicate it.\"\n",
"print(origin_text)"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"Leadership requires two things: a vision of the world that does not yet exist and the ability to communicate it.\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "lDYLlpxELSfp",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "4fd26501-8644-48a5-9409-ba4296520193"
},
"source": [
"# translate from English to Italian\n",
"text_trans = translator.translate(text=origin_text, dest='it').text\n",
"print(text_trans)"
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": [
"La leadership richiede due cose: una visione del mondo che ancora non esiste e la capacità di comunicarlo.\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "9cEaZ6nkMU27",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "24499b01-c46e-4b28-a0b8-150f3d3f208c"
},
"source": [
"# translate back to Englisht from Italian\n",
"translator.translate(text=text_trans, dest='en').text"
],
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'Leadership requires two things: a view of the world that does not yet exist and the ability to communicate it.'"
]
},
"metadata": {
"tags": []
},
"execution_count": 5
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BHlE4tneS3wl",
"colab_type": "text"
},
"source": [
"Example 2:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "KPhm09-VRx9p",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "c1662a8a-646e-43d0-dbf7-6405c188cbc7"
},
"source": [
"origin_text = \"The role of a leader is not to come up with all the great ideas. The role of a leader is to create an environment in which great ideas can happen\"\n",
"print(origin_text)"
],
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": [
"The role of a leader is not to come up with all the great ideas. The role of a leader is to create an environment in which great ideas can happen\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "XfYK6CFDTANw",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "1f1dd65f-e5c5-446d-f081-142979f1b353"
},
"source": [
"# translate from English to Italian\n",
"text_trans = translator.translate(text=origin_text, dest='it').text\n",
"print(text_trans)"
],
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": [
"Il ruolo di un leader non è quello di trovare tutte le grandi idee. Il ruolo di un leader è creare un ambiente in cui possano nascere grandi idee\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "WCvjNsFdTBz7",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "8de457c8-4cb6-40e1-cd4c-27c6cd5195fe"
},
"source": [
"# translate back to English from Italian\n",
"translator.translate(text=text_trans, dest='en').text"
],
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"data": {
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
},
"text/plain": [
"'The role of a leader is not to find all the great ideas. The role of a leader is to create an environment where great ideas can arise'"
]
},
"metadata": {
"tags": []
},
"execution_count": 8
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment