Skip to content

Instantly share code, notes, and snippets.

@theLivin
Created February 17, 2020 04:53
Show Gist options
  • Save theLivin/6172ed79173cb0be0012156aea40eec9 to your computer and use it in GitHub Desktop.
Save theLivin/6172ed79173cb0be0012156aea40eec9 to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://cognitiveclass.ai/\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Ad/CCLog.png\" width=\"200\" align=\"center\">\n",
"</a>\n",
"\n",
"<h1 align=\"center\"><font size=\"5\"><b>Application Programming Interface</b></font></h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>In this notebook, you will learn to convert an audio file of an English speaker to text using a Speech to Text API. Then you will translate the English version to a Spanish version using a Language Translator API. <b>Note:</b> You must obtain the API keys and enpoints to complete the lab.</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <a href=\"https://cocl.us/topNotebooksPython101Coursera\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Ad/TopAd.png\" width=\"750\" align=\"center\">\n",
" </a>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
"<h2>Table of Contents</h2>\n",
"<ul>\n",
" <li><a href=\"#ref0\">Speech To Text</a></li>\n",
" <li><a href=\"#ref1\">Language Translator</a></li>\n",
" <li><a href=\"#ref2\">Exercise</a></li>\n",
"</ul>\n",
"<br>\n",
"<p>Estimated Time Needed: <strong>25 min</strong></p>\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting ibm_watson\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/68/b8/b0cbd12e1813a8cc936619e88e1c54a3475dcb38aff7595b339ddf75fd39/ibm-watson-4.3.0.tar.gz (360kB)\n",
"\u001b[K |████████████████████████████████| 368kB 27.6MB/s eta 0:00:01\n",
"\u001b[?25hCollecting wget\n",
" Downloading https://files.pythonhosted.org/packages/47/6a/62e288da7bcda82b935ff0c6cfe542970f04e29c756b0e147251b2fb251f/wget-3.2.zip\n",
"Requirement already satisfied: requests<3.0,>=2.0 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from ibm_watson) (2.22.0)\n",
"Requirement already satisfied: python_dateutil>=2.5.3 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from ibm_watson) (2.8.1)\n",
"Collecting websocket-client==0.48.0 (from ibm_watson)\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/8a/a1/72ef9aa26cfe1a75cee09fc1957e4723add9de098c15719416a1ee89386b/websocket_client-0.48.0-py2.py3-none-any.whl (198kB)\n",
"\u001b[K |████████████████████████████████| 204kB 31.7MB/s eta 0:00:01\n",
"\u001b[?25hCollecting ibm_cloud_sdk_core==1.5.1 (from ibm_watson)\n",
" Downloading https://files.pythonhosted.org/packages/b7/f6/10d5271c807d73d236e6ae07b68035fed78b28b5ab836704d34097af3986/ibm-cloud-sdk-core-1.5.1.tar.gz\n",
"Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests<3.0,>=2.0->ibm_watson) (1.25.7)\n",
"Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests<3.0,>=2.0->ibm_watson) (3.0.4)\n",
"Requirement already satisfied: idna<2.9,>=2.5 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests<3.0,>=2.0->ibm_watson) (2.8)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests<3.0,>=2.0->ibm_watson) (2019.9.11)\n",
"Requirement already satisfied: six>=1.5 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from python_dateutil>=2.5.3->ibm_watson) (1.13.0)\n",
"Collecting PyJWT>=1.7.1 (from ibm_cloud_sdk_core==1.5.1->ibm_watson)\n",
" Downloading https://files.pythonhosted.org/packages/87/8b/6a9f14b5f781697e51259d81657e6048fd31a113229cf346880bb7545565/PyJWT-1.7.1-py2.py3-none-any.whl\n",
"Building wheels for collected packages: ibm-watson, wget, ibm-cloud-sdk-core\n",
" Building wheel for ibm-watson (setup.py) ... \u001b[?25ldone\n",
"\u001b[?25h Stored in directory: /home/jupyterlab/.cache/pip/wheels/b7/10/b4/509307276e9d558a6b06d8e69f8faf6cfbdf2d1b83abb6b8d4\n",
" Building wheel for wget (setup.py) ... \u001b[?25ldone\n",
"\u001b[?25h Stored in directory: /home/jupyterlab/.cache/pip/wheels/40/15/30/7d8f7cea2902b4db79e3fea550d7d7b85ecb27ef992b618f3f\n",
" Building wheel for ibm-cloud-sdk-core (setup.py) ... \u001b[?25ldone\n",
"\u001b[?25h Stored in directory: /home/jupyterlab/.cache/pip/wheels/6a/42/50/f96888116b329578304f9dda4693cef6f3e76e18272d22cb6c\n",
"Successfully built ibm-watson wget ibm-cloud-sdk-core\n",
"Installing collected packages: websocket-client, PyJWT, ibm-cloud-sdk-core, ibm-watson, wget\n",
"Successfully installed PyJWT-1.7.1 ibm-cloud-sdk-core-1.5.1 ibm-watson-4.3.0 websocket-client-0.48.0 wget-3.2\n"
]
}
],
"source": [
"#you will need the following library \n",
"!pip install ibm_watson wget"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"ref0\">Speech to Text</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>First we import <code>SpeechToTextV1</code> from <code>ibm_watson</code>.For more information on the API, please click on this <a href=\"https://cloud.ibm.com/apidocs/speech-to-text?code=python\">link</a></p>"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"from ibm_watson import SpeechToTextV1 \n",
"import json\n",
"from ibm_cloud_sdk_core.authenticators import IAMAuthenticator"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>The service endpoint is based on the location of the service instance, we store the information in the variable URL. To find out which URL to use, view the service credentials.</p>"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [],
"source": [
"url_s2t = \"https://stream.watsonplatform.net/speech-to-text/api\"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>You require an API key, and you can obtain the key on the <a href=\"https://cloud.ibm.com/resources\">Dashboard </a>.</p>"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"iam_apikey_s2t = \"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>You create a <a href=\"http://watson-developer-cloud.github.io/python-sdk/v0.25.0/apis/watson_developer_cloud.speech_to_text_v1.html\">Speech To Text Adapter object</a> the parameters are the endpoint and API key.</p>"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<ibm_watson.speech_to_text_v1_adapter.SpeechToTextV1Adapter at 0x7fd0cda8fcc0>"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"authenticator = IAMAuthenticator(iam_apikey_s2t)\n",
"s2t = SpeechToTextV1(authenticator=authenticator)\n",
"s2t.set_service_url(url_s2t)\n",
"s2t"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>Lets download the audio file that we will use to convert into text.</p>"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--2020-02-17 04:12:33-- https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/labs/PolynomialRegressionandPipelines.mp3\n",
"Resolving s3-api.us-geo.objectstorage.softlayer.net (s3-api.us-geo.objectstorage.softlayer.net)... 67.228.254.196\n",
"Connecting to s3-api.us-geo.objectstorage.softlayer.net (s3-api.us-geo.objectstorage.softlayer.net)|67.228.254.196|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 4234179 (4.0M) [audio/mpeg]\n",
"Saving to: ‘PolynomialRegressionandPipelines.mp3’\n",
"\n",
"PolynomialRegressio 100%[===================>] 4.04M 6.01MB/s in 0.7s \n",
"\n",
"2020-02-17 04:12:34 (6.01 MB/s) - ‘PolynomialRegressionandPipelines.mp3’ saved [4234179/4234179]\n",
"\n"
]
}
],
"source": [
"!wget -O PolynomialRegressionandPipelines.mp3 https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/labs/PolynomialRegressionandPipelines.mp3\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>We have the path of the wav file we would like to convert to text</p>"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"filename='PolynomialRegressionandPipelines.mp3'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>We create the file object <code>wav</code> with the wav file using <code>open</code> ; we set the <code>mode</code> to \"rb\" , this is similar to read mode, but it ensures the file is in binary mode.We use the method <code>recognize</code> to return the recognized text. The parameter audio is the file object <code>wav</code>, the parameter <code>content_type</code> is the format of the audio file.</p>"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"with open(filename, mode=\"rb\") as wav:\n",
" response = s2t.recognize(audio=wav, content_type='audio/mp3')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>The attribute result contains a dictionary that includes the translation:</p>"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'results': [{'alternatives': [{'confidence': 0.92,\n",
" 'transcript': 'in this video we will cover polynomial regression and pipelines '}],\n",
" 'final': True},\n",
" {'alternatives': [{'confidence': 0.9,\n",
" 'transcript': \"what do we do when a linear model is not the best fit for our data let's look into another type of regression model the polynomial regression we transform our data into a polynomial then use linear regression to fit the parameters that we will discuss pipelines hi plains are way to simplify your code \"}],\n",
" 'final': True},\n",
" {'alternatives': [{'confidence': 0.95,\n",
" 'transcript': \"polynomial regression is a special case of the general linear regression this method is beneficial for describing curvilinear relationships what is a curvilinear relationship it's what you get by squaring or setting higher order terms of the predictor variables in the model transforming the data the model can be quadratic which means the predictor variable in the model is squared we use a bracket to indicated as an exponent this is the second order polynomial regression but the figure representing the function \"}],\n",
" 'final': True},\n",
" {'alternatives': [{'confidence': 0.95,\n",
" 'transcript': 'the model can be cubic which means the predictor variable is cute this is the third order polynomial regression we see by examining the figure that the function has more variation '}],\n",
" 'final': True},\n",
" {'alternatives': [{'confidence': 0.9,\n",
" 'transcript': \"there also exists higher order polynomial regressions when a good fit hasn't been achieved by second or third order we can see in figure is how much the graphs change when we change the order of the polynomial regression the degree of the regression makes a big difference and can result in a better fit if you pick the right value in all cases the relationship between the variable in the parameter is always linear \"}],\n",
" 'final': True},\n",
" {'alternatives': [{'confidence': 0.93,\n",
" 'transcript': \"let's look at an example from our data we generate a polynomial regression model \"}],\n",
" 'final': True},\n",
" {'alternatives': [{'confidence': 0.93,\n",
" 'transcript': 'in python we do this by using the poly fifth function in this example we develop a third order polynomial regression model base we can print out the model symbolic form for the model is given by the following expression '}],\n",
" 'final': True},\n",
" {'alternatives': [{'confidence': 0.91,\n",
" 'transcript': \"negative one point five five seven X. one cute plus two hundred four point eight X. one squared plus eight thousand nine hundred sixty five X. one plus one point three seven times ten to the power of five we can also have multi dimensional polynomial linear regression the expression can get complicated here are just some of the terms for two dimensional second order polynomial none pies poly fit function cannot perform this type of regression we use the pre processing librarian sidekick learn to create a polynomial feature object the constructor takes the degree of the polynomial as a parameter then we transform the features into a polynomial feature with the fit underscore transform method let's do a more intuitive example \"}],\n",
" 'final': True},\n",
" {'alternatives': [{'confidence': 0.89,\n",
" 'transcript': 'consider the feature shown here applying the method we transform the data we now have a new set of features that are transformed version of our original features as that I mention it the data gets larger we may want to normalize multiple features %HESITATION so I could learn instead we can use the pre processing module to simplify many tasks for example we can standardize each feature simultaneously we import standard scalar we train the object fit the scale object then transform the data into a new data frame honore X. underscore scale there are more normalization methods available in the pre processing library as well as other transformations we can simplify your code by using a pipeline library there are many steps to getting a prediction for example normalization polynomial transform and linear regression we simplify the process using a pipeline '}],\n",
" 'final': True},\n",
" {'alternatives': [{'confidence': 0.9,\n",
" 'transcript': 'pipeline sequentially perform a series of transformations the last step carries out a prediction first we import all the modules we need them we import the library pipeline we create a list of topples the first element in the topple contains the name of the estimator model the second element contains model constructor we input the list in the pipeline constructor we now have a pipeline object we can train the pipeline by applying the train method to the pipeline object we can also produce a prediction as well '}],\n",
" 'final': True},\n",
" {'alternatives': [{'confidence': 0.86,\n",
" 'transcript': 'the method normalizes the data performs a polynomial transform then outputs of prediction '}],\n",
" 'final': True}],\n",
" 'result_index': 0}"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response.result"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>confidence</th>\n",
" <th>transcript</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.92</td>\n",
" <td>in this video we will cover polynomial regress...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.90</td>\n",
" <td>what do we do when a linear model is not the b...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.95</td>\n",
" <td>polynomial regression is a special case of the...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.95</td>\n",
" <td>the model can be cubic which means the predict...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0.90</td>\n",
" <td>there also exists higher order polynomial regr...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>0.93</td>\n",
" <td>let's look at an example from our data we gene...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>0.93</td>\n",
" <td>in python we do this by using the poly fifth f...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>0.91</td>\n",
" <td>negative one point five five seven X. one cute...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>0.89</td>\n",
" <td>consider the feature shown here applying the m...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>0.90</td>\n",
" <td>pipeline sequentially perform a series of tran...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>0.86</td>\n",
" <td>the method normalizes the data performs a poly...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" confidence transcript\n",
"0 0.92 in this video we will cover polynomial regress...\n",
"1 0.90 what do we do when a linear model is not the b...\n",
"2 0.95 polynomial regression is a special case of the...\n",
"3 0.95 the model can be cubic which means the predict...\n",
"4 0.90 there also exists higher order polynomial regr...\n",
"5 0.93 let's look at an example from our data we gene...\n",
"6 0.93 in python we do this by using the poly fifth f...\n",
"7 0.91 negative one point five five seven X. one cute...\n",
"8 0.89 consider the feature shown here applying the m...\n",
"9 0.90 pipeline sequentially perform a series of tran...\n",
"10 0.86 the method normalizes the data performs a poly..."
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from pandas.io.json import json_normalize\n",
"\n",
"json_normalize(response.result['results'],\"alternatives\")"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<ibm_cloud_sdk_core.detailed_response.DetailedResponse at 0x7fd0cda97550>"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>We can obtain the recognized text and assign it to the variable <code>recognized_text</code>:</p>"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"recognized_text=response.result['results'][0][\"alternatives\"][0][\"transcript\"]\n",
"type(recognized_text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"ref1\">Language Translator</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>First we import <code>LanguageTranslatorV3</code> from ibm_watson. For more information on the API click <a href=\"https://cloud.ibm.com/apidocs/speech-to-text?code=python\"> here</a></p>"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [],
"source": [
"from ibm_watson import LanguageTranslatorV3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>The service endpoint is based on the location of the service instance, we store the information in the variable URL. To find out which URL to use, view the service credentials.</p>"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"url_lt='https://gateway.watsonplatform.net/language-translator/api'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>You require an API key, and you can obtain the key on the <a href=\"https://cloud.ibm.com/resources\">Dashboard</a>.</p>"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"apikey_lt=''"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>API requests require a version parameter that takes a date in the format version=YYYY-MM-DD. This lab describes the current version of Language Translator, 2018-05-01</p>"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"version_lt='2018-05-01'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>we create a Language Translator object <code>language_translator</code>:</p>"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"<ibm_watson.language_translator_v3.LanguageTranslatorV3 at 0x7fd0cda97278>"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"authenticator = IAMAuthenticator(apikey_lt)\n",
"language_translator = LanguageTranslatorV3(version=version_lt,authenticator=authenticator)\n",
"language_translator.set_service_url(url_lt)\n",
"language_translator"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>We can get a Lists the languages that the service can identify.\n",
"The method Returns the language code. For example English (en) to Spanis (es) and name of each language.</p>"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>language</th>\n",
" <th>name</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>af</td>\n",
" <td>Afrikaans</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>ar</td>\n",
" <td>Arabic</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>az</td>\n",
" <td>Azerbaijani</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>ba</td>\n",
" <td>Bashkir</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>be</td>\n",
" <td>Belarusian</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>63</th>\n",
" <td>uk</td>\n",
" <td>Ukrainian</td>\n",
" </tr>\n",
" <tr>\n",
" <th>64</th>\n",
" <td>ur</td>\n",
" <td>Urdu</td>\n",
" </tr>\n",
" <tr>\n",
" <th>65</th>\n",
" <td>vi</td>\n",
" <td>Vietnamese</td>\n",
" </tr>\n",
" <tr>\n",
" <th>66</th>\n",
" <td>zh</td>\n",
" <td>Simplified Chinese</td>\n",
" </tr>\n",
" <tr>\n",
" <th>67</th>\n",
" <td>zh-TW</td>\n",
" <td>Traditional Chinese</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>68 rows × 2 columns</p>\n",
"</div>"
],
"text/plain": [
" language name\n",
"0 af Afrikaans\n",
"1 ar Arabic\n",
"2 az Azerbaijani\n",
"3 ba Bashkir\n",
"4 be Belarusian\n",
".. ... ...\n",
"63 uk Ukrainian\n",
"64 ur Urdu\n",
"65 vi Vietnamese\n",
"66 zh Simplified Chinese\n",
"67 zh-TW Traditional Chinese\n",
"\n",
"[68 rows x 2 columns]"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from pandas.io.json import json_normalize\n",
"\n",
"json_normalize(language_translator.list_identifiable_languages().get_result(), \"languages\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>We can use the method <code>translate</code> this will translate the text. The parameter text is the text. Model_id is the type of model we would like to use use we use list the the langwich . In this case, we set it to 'en-es' or English to Spanish. We get a Detailed Response object translation_response</p>"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"<ibm_cloud_sdk_core.detailed_response.DetailedResponse at 0x7fd0cd9fa978>"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"translation_response = language_translator.translate(\\\n",
" text=recognized_text, model_id='en-es')\n",
"translation_response"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>The result is a dictionary.</p>"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/plain": [
"{'translations': [{'translation': 'en este video cubriremos la regresión polinómica y los oleoductos '}],\n",
" 'word_count': 10,\n",
" 'character_count': 64}"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"translation=translation_response.get_result()\n",
"translation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>We can obtain the actual translation as a string as follows:</p>"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'en este video cubriremos la regresión polinómica y los oleoductos '"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spanish_translation =translation['translations'][0]['translation']\n",
"spanish_translation "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>We can translate back to English</p>"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [],
"source": [
"translation_new = language_translator.translate(text=spanish_translation ,model_id='es-en').get_result()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>We can obtain the actual translation as a string as follows:</p>"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'in this video, we will cover the polynomial regression and the pipelines '"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"translation_eng=translation_new['translations'][0]['translation']\n",
"translation_eng"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>We can convert it to french as well:</p>"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [],
"source": [
"French_translation=language_translator.translate(\n",
" text=translation_eng , model_id='en-fr').get_result()"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Dans cette vidéo, nous couvrons la régression polynomiale et les pipelines '"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"French_translation['translations'][0]['translation']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Language Translator</h3>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a href=\"http://cocl.us/NotebooksPython101bottom\"><img src=\"https://ibm.box.com/shared/static/irypdxea2q4th88zu1o1tsd06dya10go.png\" width=\"750\" align=\"center\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>References</b>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://cloud.ibm.com/apidocs/speech-to-text?code=python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://cloud.ibm.com/apidocs/language-translator?code=python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h4>About the Author:</h4>\n",
"<p><a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\">Joseph Santarcangelo</a> has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Other contributor: <a href=\"https://www.linkedin.com/in/fanjiang0619/\">Fan Jiang</a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Copyright &copy; 2019 [cognitiveclass.ai](https:cognitiveclass.ai). This notebook and its source code are released under the terms of the [MIT License](cognitiveclass.ai)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment