{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Para instalar usaremos:\n",
    "\n",
    "    pip install --upgrade pocketsphinx\n",
    "    pip install pyaudio\n",
    "    pip install SpeechRecognition\n",
    "    pip install --upgrade google-api-python-client"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "r\n",
      "one\n",
      "choose it\n",
      "two\n",
      "four\n",
      "proven\n",
      "porn\n",
      "quinn\n",
      "for you\n",
      "the surveys\n",
      "farm chick\n",
      "move for you or\n",
      "right\n",
      "and\n",
      "to the\n",
      "guam\n",
      "what op\n",
      "color\n",
      "horror\n",
      "yep\n",
      "my cat\n",
      "if the\n",
      "\n",
      "i had to hit\n",
      "four\n",
      "at the\n",
      "what could we could\n",
      "\n",
      "and what happened\n",
      "\n",
      "two\n",
      "huh\n",
      "\n",
      "they know\n",
      "though while\n"
     ]
    }
   ],
   "source": [
    "from pocketsphinx import LiveSpeech\n",
    "for phrase in LiveSpeech(): print(phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'recognize_sphinx' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-8-cc5af35b2dd4>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;31m# recognize speech using Sphinx\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Sphinx thinks you said \"\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mrecognize_sphinx\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maudio\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      6\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0msr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mUnknownValueError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Sphinx could not understand audio\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'recognize_sphinx' is not defined"
     ]
    }
   ],
   "source": [
    "import speech_recognition as sr\n",
    "\n",
    "# recognize speech using Sphinx\n",
    "try:\n",
    "    print(\"Sphinx thinks you said \" + r.recognize_sphinx(audio))\n",
    "except sr.UnknownValueError:\n",
    "    print(\"Sphinx could not understand audio\")\n",
    "except sr.RequestError as e:\n",
    "    print(\"Sphinx error; {0}\".format(e))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Todas las opciones posibles:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python3\n",
    "\n",
    "# NOTE: this example requires PyAudio because it uses the Microphone class\n",
    "\n",
    "import speech_recognition as sr\n",
    "\n",
    "# obtain audio from the microphone\n",
    "r = sr.Recognizer()\n",
    "with sr.Microphone() as source:\n",
    "    print(\"Say something!\")\n",
    "    audio = r.listen(source)\n",
    "\n",
    "# recognize speech using Sphinx\n",
    "try:\n",
    "    print(\"Sphinx thinks you said \" + r.recognize_sphinx(audio))\n",
    "except sr.UnknownValueError:\n",
    "    print(\"Sphinx could not understand audio\")\n",
    "except sr.RequestError as e:\n",
    "    print(\"Sphinx error; {0}\".format(e))\n",
    "\n",
    "# recognize speech using Google Speech Recognition\n",
    "try:\n",
    "    # for testing purposes, we're just using the default API key\n",
    "    # to use another API key, use `r.recognize_google(audio, key=\"GOOGLE_SPEECH_RECOGNITION_API_KEY\")`\n",
    "    # instead of `r.recognize_google(audio)`\n",
    "    print(\"Google Speech Recognition thinks you said \" + r.recognize_google(audio))\n",
    "except sr.UnknownValueError:\n",
    "    print(\"Google Speech Recognition could not understand audio\")\n",
    "except sr.RequestError as e:\n",
    "    print(\"Could not request results from Google Speech Recognition service; {0}\".format(e))\n",
    "\n",
    "# recognize speech using Google Cloud Speech\n",
    "GOOGLE_CLOUD_SPEECH_CREDENTIALS = r\"\"\"INSERT THE CONTENTS OF THE GOOGLE CLOUD SPEECH JSON CREDENTIALS FILE HERE\"\"\"\n",
    "try:\n",
    "    print(\"Google Cloud Speech thinks you said \" + r.recognize_google_cloud(audio, credentials_json=GOOGLE_CLOUD_SPEECH_CREDENTIALS))\n",
    "except sr.UnknownValueError:\n",
    "    print(\"Google Cloud Speech could not understand audio\")\n",
    "except sr.RequestError as e:\n",
    "    print(\"Could not request results from Google Cloud Speech service; {0}\".format(e))\n",
    "\n",
    "# recognize speech using Wit.ai\n",
    "WIT_AI_KEY = \"INSERT WIT.AI API KEY HERE\"  # Wit.ai keys are 32-character uppercase alphanumeric strings\n",
    "try:\n",
    "    print(\"Wit.ai thinks you said \" + r.recognize_wit(audio, key=WIT_AI_KEY))\n",
    "except sr.UnknownValueError:\n",
    "    print(\"Wit.ai could not understand audio\")\n",
    "except sr.RequestError as e:\n",
    "    print(\"Could not request results from Wit.ai service; {0}\".format(e))\n",
    "\n",
    "# recognize speech using Microsoft Bing Voice Recognition\n",
    "BING_KEY = \"INSERT BING API KEY HERE\"  # Microsoft Bing Voice Recognition API keys 32-character lowercase hexadecimal strings\n",
    "try:\n",
    "    print(\"Microsoft Bing Voice Recognition thinks you said \" + r.recognize_bing(audio, key=BING_KEY))\n",
    "except sr.UnknownValueError:\n",
    "    print(\"Microsoft Bing Voice Recognition could not understand audio\")\n",
    "except sr.RequestError as e:\n",
    "    print(\"Could not request results from Microsoft Bing Voice Recognition service; {0}\".format(e))\n",
    "\n",
    "# recognize speech using Houndify\n",
    "HOUNDIFY_CLIENT_ID = \"INSERT HOUNDIFY CLIENT ID HERE\"  # Houndify client IDs are Base64-encoded strings\n",
    "HOUNDIFY_CLIENT_KEY = \"INSERT HOUNDIFY CLIENT KEY HERE\"  # Houndify client keys are Base64-encoded strings\n",
    "try:\n",
    "    print(\"Houndify thinks you said \" + r.recognize_houndify(audio, client_id=HOUNDIFY_CLIENT_ID, client_key=HOUNDIFY_CLIENT_KEY))\n",
    "except sr.UnknownValueError:\n",
    "    print(\"Houndify could not understand audio\")\n",
    "except sr.RequestError as e:\n",
    "    print(\"Could not request results from Houndify service; {0}\".format(e))\n",
    "\n",
    "# recognize speech using IBM Speech to Text\n",
    "IBM_USERNAME = \"INSERT IBM SPEECH TO TEXT USERNAME HERE\"  # IBM Speech to Text usernames are strings of the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX\n",
    "IBM_PASSWORD = \"INSERT IBM SPEECH TO TEXT PASSWORD HERE\"  # IBM Speech to Text passwords are mixed-case alphanumeric strings\n",
    "try:\n",
    "    print(\"IBM Speech to Text thinks you said \" + r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD))\n",
    "except sr.UnknownValueError:\n",
    "    print(\"IBM Speech to Text could not understand audio\")\n",
    "except sr.RequestError as e:\n",
    "    print(\"Could not request results from IBM Speech to Text service; {0}\".format(e))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## sphinx\n",
    "\n",
    "Solo funciona en inglés, y aunque es gratuito, el tiempo de procesado es demasiado largo."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Say something!\n",
      "Sphinx thinks you said hello\n"
     ]
    }
   ],
   "source": [
    "import speech_recognition as sr\n",
    "\n",
    "# obtain audio from the microphone\n",
    "r = sr.Recognizer()\n",
    "with sr.Microphone() as source:\n",
    "    print(\"Say something!\")\n",
    "    audio = r.listen(source)\n",
    "\n",
    "    # recognize speech using Sphinx\n",
    "try:\n",
    "    print(\"Sphinx thinks you said \" + r.recognize_sphinx(audio))\n",
    "except sr.UnknownValueError:\n",
    "    print(\"Sphinx could not understand audio\")\n",
    "except sr.RequestError as e:\n",
    "    print(\"Sphinx error; {0}\".format(e))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Google Speech Recognition DEFAULT API Key\n",
    "\n",
    "En principio es el mejor, y no requiere de mucho tiempo de procesado. El problema radica en que esta versión dejará de funcionar de un momento a otro, por la versión de **Google Cloud Platform**.\n",
    "\n",
    "Podemos ir implementando esta opción, previamente a saber como funciona definitivamente la opción Cloud."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Say something!\n",
      "Google Speech Recognition thinks you said hola\n"
     ]
    }
   ],
   "source": [
    "import speech_recognition as sr\n",
    "\n",
    "# obtain audio from the microphone\n",
    "r = sr.Recognizer()\n",
    "with sr.Microphone() as source:\n",
    "    print(\"Say something!\")\n",
    "    audio = r.listen(source)\n",
    "\n",
    "# recognize speech using Google Speech Recognition\n",
    "try:\n",
    "    # for testing purposes, we're just using the default API key\n",
    "    # to use another API key, use `r.recognize_google(audio, key=\"GOOGLE_SPEECH_RECOGNITION_API_KEY\")`\n",
    "    # instead of `r.recognize_google(audio)`\n",
    "    print(\"Google Speech Recognition thinks you said \" + r.recognize_google(audio, language = \"es-ES\"))\n",
    "except sr.UnknownValueError:\n",
    "    print(\"Google Speech Recognition could not understand audio\")\n",
    "except sr.RequestError as e:\n",
    "    print(\"Could not request results from Google Speech Recognition service; {0}\".format(e))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Google Cloud Speech"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Say something!\n",
      "Google Cloud Speech could not understand audio\n"
     ]
    }
   ],
   "source": [
    "import speech_recognition as sr\n",
    "\n",
    "# obtain audio from the microphone\n",
    "r = sr.Recognizer()\n",
    "with sr.Microphone() as source:\n",
    "    print(\"Say something!\")\n",
    "    audio = r.listen(source)\n",
    "\n",
    "# recognize speech using Google Cloud Speech\n",
    "GOOGLE_CLOUD_SPEECH_CREDENTIALS = r\"\"\"{\n",
    "  \"type\": \"service_account\",\n",
    "  \"project_id\": \"airvant-experimentacion\",\n",
    "  \"private_key_id\": \"221ddc0fe8af899abc52777956d60c60d2376897\",\n",
    "  \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDD3dAotpzG5srm\\ne/71UW1+moIXJdIDdmWXuDNBDy+/OFMQQrTJiitdyFfoJ+hC2cN65Hn3JeoT3v31\\nBwfJZRRwWKE7965a5TeLa8L6m1+O7RBuShW1sXZxKC2dh0CqzNuC+Xb1qYZgbexX\\nfcUcsbwIwL1W5tI6KuhJ8AzWTnKtvRHnUGRp9pYuDja4RVvmv7Ya3JE8JzgwulOO\\nJGZcSGM8wZb5FZRK+XaizoPL6O4LemgjsT8rvUONQf/DSU9BvMF2fXZIOcKGWrS+\\nGxe/bEHiaugSxR6bmuP3iJ1YlpO1/ygTbh69X9qbUU9amFXeUICoCpqao4bG7U0F\\naUXZVitZAgMBAAECggEASP43bozLdWaaM/X+yJ6dbyKWm5jJIOg1KSsI0tSdhyYu\\n8m7hoAAhdDRO6mJA8PTloD1YZK4ZA2nFe9SwZrIIQWwOPmJp9E7a+dbhlXJk1Mdt\\nKaTrXUhy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BsNgCCEH1qEDGCIdVTlePnO1xULoQr2jNxbyVhsQq9b\\nqyLumJELL/J0CJeiFs1FEw==\\n-----END PRIVATE KEY-----\\n\",\n",
    "  \"client_email\": \"definir-ubicaci-n@airvant-experimentacion.iam.gserviceaccount.com\",\n",
    "  \"client_id\": \"114996435871567233179\",\n",
    "  \"auth_uri\": \"https://accounts.google.com/o/oauth2/auth\",\n",
    "  \"token_uri\": \"https://accounts.google.com/o/oauth2/token\",\n",
    "  \"auth_provider_x509_cert_url\": \"https://www.googleapis.com/oauth2/v1/certs\",\n",
    "  \"client_x509_cert_url\": \"https://www.googleapis.com/robot/v1/metadata/x509/definir-ubicaci-n%40airvant-experimentacion.iam.gserviceaccount.com\"\n",
    "}\"\"\"\n",
    "try:\n",
    "    instruccion = r.recognize_google_cloud(audio, language = \"es-ES\", credentials_json=GOOGLE_CLOUD_SPEECH_CREDENTIALS)\n",
    "    print(\"Google Cloud Speech thinks you said \" + str(instruccion))\n",
    "except sr.UnknownValueError:\n",
    "    print(\"Google Cloud Speech could not understand audio\")\n",
    "except sr.RequestError as e:\n",
    "    print(\"Could not request results from Google Cloud Speech service; {0}\".format(e))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# IBM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition as sr\n",
    "\n",
    "# obtain audio from the microphone\n",
    "r = sr.Recognizer()\n",
    "with sr.Microphone() as source:\n",
    "    print(\"Say something!\")\n",
    "    audio = r.listen(source)\n",
    "\n",
    "\n",
    "# recognize speech using IBM Speech to Text\n",
    "IBM_USERNAME = \"INSERT IBM SPEECH TO TEXT USERNAME HERE\"  # IBM Speech to Text usernames are strings of the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX\n",
    "IBM_PASSWORD = \"INSERT IBM SPEECH TO TEXT PASSWORD HERE\"  # IBM Speech to Text passwords are mixed-case alphanumeric strings\n",
    "try:\n",
    "    print(\"IBM Speech to Text thinks you said \" + r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD))\n",
    "except sr.UnknownValueError:\n",
    "    print(\"IBM Speech to Text could not understand audio\")\n",
    "except sr.RequestError as e:\n",
    "    print(\"Could not request results from IBM Speech to Text service; {0}\".format(e))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "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.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
