Skip to content

update for current ibm-watson SDK libs #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 36 additions & 46 deletions tutorials/Watson Language Translator.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"### About this notebook\n",
"You will learn how to translate text into different languages in this exercise. \n",
" \n",
"**Estimated Time needed**: 15 min"
"**Estimated Time needed**: 10-15 min"
]
},
{
Expand All @@ -38,50 +38,41 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"#### Video tutorial (4 min 32 sec)"
"### Step 1 of 3: Initiate the translator service using Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Click on the code cell below and press Shift+Enter to run the cell and load the video tutorial."
"Press Shift+Enter to execute these cells."
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import HTML\n",
"\n",
"HTML(\"\"\"\n",
"<div><video width=\"80%\" controls>\n",
" <source src=\"https://ibm.box.com/shared/static/2uczdudjhyrnksh2miqkdfsntz26iexb.mp4 type=\"video/mp4\">\n",
"</video></div>\n",
"<a href=\"https://cocl.us/watson_translator_cclabs\">Click here to create your API key for Watson Language Translator on IBM Cloud.</a>\n",
"\"\"\")\n"
"First, install the current IBM Watson SDK packages for Python."
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "subslide"
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"### Step 1 of 3: Initiate the translator service using Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Press Shift+Enter to execute these cells."
"!pip install ibm_cloud_sdk_core \n",
"!pip install ibm-watson"
]
},
{
Expand All @@ -94,7 +85,7 @@
},
"outputs": [],
"source": [
"from watson_developer_cloud import LanguageTranslatorV3"
"from ibm_watson import LanguageTranslatorV3"
]
},
{
Expand All @@ -114,10 +105,16 @@
},
"outputs": [],
"source": [
"from ibm_cloud_sdk_core.authenticators import IAMAuthenticator\n",
"\n",
"authenticator = IAMAuthenticator('apikey12345')\n",
"\n",
"language_translator = LanguageTranslatorV3(\n",
" version='2018-05-01',\n",
" iam_api_key='apikey12345' #replace with your API key. Keep the quotation marks!\n",
")"
" version='2018-05-01'\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're missing a comma here

" authenticator=authenticator\n",
")\n",
"\n",
"language_translator.set_service_url('https://gateway.watsonplatform.net/language-translator/api')"
]
},
{
Expand All @@ -141,7 +138,7 @@
},
"outputs": [],
"source": [
"text_to_translate = \"Polong is excellent at table tennis\""
"text_to_translate = \"Who is excellent at table tennis?\""
]
},
{
Expand Down Expand Up @@ -169,7 +166,7 @@
"\n",
"translation = language_translator.translate(\n",
" text = text_to_translate,\n",
" model_id = 'en-fr')\n",
" model_id = 'en-fr').get_result()\n",
"\n",
"print(json.dumps(translation, indent=2, ensure_ascii=False))"
]
Expand Down Expand Up @@ -211,7 +208,7 @@
"translation = language_translator.translate(\n",
" text = text_to_translate,\n",
" model_id = 'en-ja' #English to Japanese\n",
") \n",
").get_result()\n",
"\n",
"print(json.dumps(translation, indent=2, ensure_ascii=False))\n"
]
Expand Down Expand Up @@ -241,9 +238,10 @@
"import pandas as pd\n",
"from pandas.io.json import json_normalize\n",
"\n",
"models = language_translator.list_models()\n",
"models = language_translator.list_models().get_result()\n",
"\n",
"model_df = pd.io.json.json_normalize(models[\"models\"])[[\"model_id\", \"source\", \"target\"]]\n",
"\n",
"model_df.sort_values(\"source\").reset_index(drop=True)"
]
},
Expand All @@ -261,13 +259,13 @@
"metadata": {},
"outputs": [],
"source": [
"text_to_translate = \"Polong is excellent at table tennis\"\n",
"text_to_translate = \"Who is excellent at table tennis?\"\n",
"\n",
"chosen_model_id = 'en-es'\n",
"\n",
"translation = language_translator.translate(\n",
" text = text_to_translate,\n",
" model_id = chosen_model_id)\n",
" model_id = chosen_model_id).get_result()\n",
"\n",
"print(json.dumps(translation, indent=2, ensure_ascii=False))"
]
Expand Down Expand Up @@ -308,8 +306,7 @@
"outputs": [],
"source": [
"identification = language_translator.identify(\n",
" text = text_to_identify\n",
")\n",
" text = text_to_identify).get_result()\n",
"\n",
"print(json.dumps(identification, indent=2, \n",
" ensure_ascii=False))\n"
Expand Down Expand Up @@ -359,7 +356,7 @@
"translation = language_translator.translate(\n",
" text = text_to_identify,\n",
" model_id = result_lang.language[0]+\"-en\" \n",
") \n",
").get_result()\n",
"\n",
"print(json.dumps(translation, indent=2, ensure_ascii=False))\n"
]
Expand Down Expand Up @@ -397,14 +394,7 @@
"- [API Documentation](https://www.ibm.com/watson/developercloud/language-translator/api/v3/python.html?cm_mmc=Email_External-_-Developer_Community-_-WW_WW-_-CognitiveClass-cclabs-jupyterlab-watsontranslator-tutorial-WA0201&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.External.M12345678&cvo_campaign=000026UJ)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### About the Author: \n",
"Hi! It's [Polong Lin](https://ca.linkedin.com/in/polonglin), the author of this notebook. I hope you found the Watson Language Translator service easy to learn!"
]
},

{
"cell_type": "markdown",
"metadata": {},
Expand Down