Watson Tutorial #1: Speech to Text + AlchemyLanguage Sentiment Analysis in Python


Introduction

Josh Zheng
Jan 25, 2016 · 7 min read

Edit: Watson AlchemyLanguage has since been replaced by Natural Language Understanding. The capabilities and usage remain very similar.

Step 0: What You’ll Need

Step 1: Create Bluemix Account

Step 2: Create the Watson Speech to Text Service

Image for post
Image for post
Image for post
Image for post

Step 3: Create Speech to Text Credentials

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Step 4: Register for AlchemyLanguage API Key

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Step 5: Python Setup

Image for post
Image for post

Step 6: Code

Image for post
Image for post
Image for post
Image for post
import os
import json
from os.path import join, dirname
from dotenv import load_dotenv
from watson_developer_cloud import SpeechToTextV1 as SpeechToText
from watson_developer_cloud import AlchemyLanguageV1 as AlchemyLanguage
from speech_sentiment_python.recorder import Recorder
recorder = Recorder(“speech.wav”)
recorder.record_to_file()
def transcribe_audio(path_to_audio_file):
username = os.environ.get(“BLUEMIX_USERNAME”)
password = os.environ.get(“BLUEMIX_PASSWORD”)
speech_to_text = SpeechToText(username=username,
password=password)
with open(join(dirname(__file__), path_to_audio_file), ‘rb’) as
audio_file:
return speech_to_text.recognize(audio_file,
content_type=’audio/wav’)
def get_text_sentiment(text):
alchemy_api_key = os.environ.get(“ALCHEMY_API_KEY”)

alchemy_language = AlchemyLanguage(api_key=alchemy_api_key)
result = alchemy_language.sentiment(text=text)
if result[‘docSentiment’][‘type’] == ‘neutral’:
return ‘netural’, 0
return result[‘docSentiment’][‘type’],
result[‘docSentiment’[‘score’]

Step 7: What’s Next

IBM Watson

AI Platform for the Enterprise

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store