Skip to content

Instantly share code, notes, and snippets.

@EsmailELBoBDev2
Created June 21, 2020 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EsmailELBoBDev2/2e6613a5a74406412febf41b886b4bc6 to your computer and use it in GitHub Desktop.
Save EsmailELBoBDev2/2e6613a5a74406412febf41b886b4bc6 to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# This is demo for watson translation API"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Make IBM cloud account, next you can search it at search bar \"`Language Translator`\" or by going to this [link](https://cloud.ibm.com/catalog/services/language-translator) \n",
"2. After you finish you will get something like [this](https://i.ibb.co/4WfscrB/Screenshot-2020-06-21-15-24-01.png)(*photo*) you need to go to \"`Manage`\" Tab (*above \"`Getting Started`\"*) and you need to copy the API key and the URL and save them (*or just keep the tab open, lol*)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already up-to-date: ibm-watson>=4.4.0 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (4.5.0)\n",
"Requirement already satisfied, skipping upgrade: websocket-client==0.48.0 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from ibm-watson>=4.4.0) (0.48.0)\n",
"Requirement already satisfied, skipping upgrade: python-dateutil>=2.5.3 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from ibm-watson>=4.4.0) (2.8.1)\n",
"Requirement already satisfied, skipping upgrade: requests<3.0,>=2.0 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from ibm-watson>=4.4.0) (2.23.0)\n",
"Requirement already satisfied, skipping upgrade: ibm-cloud-sdk-core==1.5.1 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from ibm-watson>=4.4.0) (1.5.1)\n",
"Requirement already satisfied, skipping upgrade: six in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from websocket-client==0.48.0->ibm-watson>=4.4.0) (1.15.0)\n",
"Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests<3.0,>=2.0->ibm-watson>=4.4.0) (2020.4.5.2)\n",
"Requirement already satisfied, skipping upgrade: chardet<4,>=3.0.2 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests<3.0,>=2.0->ibm-watson>=4.4.0) (3.0.4)\n",
"Requirement already satisfied, skipping upgrade: 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>=4.4.0) (1.25.9)\n",
"Requirement already satisfied, skipping upgrade: idna<3,>=2.5 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from requests<3.0,>=2.0->ibm-watson>=4.4.0) (2.9)\n",
"Requirement already satisfied, skipping upgrade: PyJWT>=1.7.1 in /home/jupyterlab/conda/envs/python/lib/python3.6/site-packages (from ibm-cloud-sdk-core==1.5.1->ibm-watson>=4.4.0) (1.7.1)\n"
]
}
],
"source": [
"#Install watson python package. the \"!\" before cmd it tells the notebook to use the bash\n",
"! pip install --upgrade \"ibm-watson>=4.4.0\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#import packages we need to work with\n",
"import json #to print the response from watson\n",
"from ibm_watson import LanguageTranslatorV3 #Watson package\n",
"from ibm_cloud_sdk_core.authenticators import IAMAuthenticator #To login to watson via APIkey\n",
"from pandas import json_normalize #to print the response into table because no one loves json look!"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"#Here add your API key, URL and version\n",
"\n",
"## Add your APIkey here\n",
"APIKEY= \"\"\n",
"\n",
"## Add Version Here, see https://cloud.ibm.com/apidocs/language-translator?code=python#versioning\n",
"VERSION= \"2018-05-01\"\n",
"\n",
"## Add URL here\n",
"URL=\"\"\n",
"\n",
"#LOGIN TO IBM, BOOOOOOM\n",
"authenticator = IAMAuthenticator(APIKEY)\n",
"language_translator = LanguageTranslatorV3(\n",
" version=VERSION,\n",
" authenticator=authenticator\n",
")\n",
"# USES THE URL, AS SORT OF LOGIN SO YEAH. BOOOOOM\n",
"language_translator.set_service_url(URL)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"How many words you wanna to translate: 2\n",
"Add Language code you are typing in: EN\n",
"Add Language code you want to translate to: Ar\n",
"Add your words here: Hey, How are you today?\n",
"Add your words here: Hey - Bye- Test- Lol\n"
]
}
],
"source": [
"#Ask user how many words he wants to translate. we used \"int()\" to convert user input into number\n",
"how_many = int(input(\"How many words you wanna to translate: \"))\n",
"\n",
"# #Ask user to add the language they are typing in\n",
"lang = input(\"Add Language code you are typing in: \")\n",
"\n",
"# #Ask user to add the language that they want to translte\n",
"langto= input(\"Add Language code you want to translate to: \")\n",
"\n",
"#an empty list to send words to watson\n",
"translation_words = []\n",
"\n",
"#A for loop to append all words into the list\n",
"for num in range(how_many):\n",
" # Ask the user for words\n",
" translate_words = input(\"Add your words here: \")\n",
" #Add those words into the list\n",
" translation_words.append(translate_words)\n",
" \n",
"\n",
" \n",
"## Those for testing to print what in the list \n",
"# for line in limerick_line:\n",
"# print(line)\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"#take results from \"translation_words\" list and send them to watson\n",
"resuls_json_tr = language_translator.translate(\n",
" #\"translation_words\" is the list of words\n",
" text=translation_words,\n",
" #\"lang\" and \"langto\" where we asked to make user add the language codes. the \"lower()\" it makes the user input into lowercase no matter how user typed them and of course we are using the + sign to connect all strings together as one string\n",
" model_id=lang.lower()+\"-\"+langto.lower()).get_result()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"translations\": [\n",
" {\n",
" \"translation\": \"مرحبا ، كيف حالك اليوم ؟\"\n",
" },\n",
" {\n",
" \"translation\": \"هاي-وداعا-اختبار-لول-\"\n",
" }\n",
" ],\n",
" \"word_count\": 12,\n",
" \"character_count\": 43\n",
"}\n"
]
}
],
"source": [
"#prints watson reply in json format but we all hate it so go to next cell\n",
"print(json.dumps(resuls_json_tr, indent=2, ensure_ascii=False))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"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>translation</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>مرحبا ، كيف حالك اليوم ؟</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>هاي-وداعا-اختبار-لول-</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" translation\n",
"0 مرحبا ، كيف حالك اليوم ؟\n",
"1 هاي-وداعا-اختبار-لول-"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Takes json and prints it to table\n",
"json_classes = resuls_json_tr['translations']\n",
"df = json_normalize(json_classes).sort_values('translation', ascending=True).sort_index()\n",
"\n",
"#prints it\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook made by [@esmailelbobdev2](https://github.com/esmailelbobdev2)\n",
"<br>\n",
"Here is the full documentation: https://cloud.ibm.com/apidocs/language-translator?code=python#versioning\n",
"<br>\n",
"<br>\n",
"**Hope you have learned something today :)** — *if you got any problems, feel free to tell me in comments* (*and mention me too to notice, lol*)"
]
}
],
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment