AlchemyAPI provides advanced text analysis capabilities through a REST API. This allows you to transform your unstructured content from sources like blog posts, news articles, product reviews, forums and social media posts into structured data. With AlchemyAPI, you can easily perform difficult tasks such as extracting keywords, calculating sentiment, or identifying companies.
To use our text analysis functions, AlchemyAPI must be integrated into your application. This tutorial will walk you through the steps to get up and running with AlchemyAPI using the Python programming language. At the conclusion of this tutorial, you will be ready to integrate AlchemyAPI into your application to solve your text analysis needs.
This tutorial will cover the following steps:
To follow along with this tutorial, you will need the following:
Operating System: This tutorial will be created using Ubuntu v13.10 64-bit, but you should be able to complete the same steps using the Mac OSX terminal or the Windows cmd window.
AlchemyAPI requires an API Key to access our text analysis functions. This key will be included into each API request, but don't worry, the SDK will handle this part for you. If you already have an API key, great! Move on to the next step. If not, please click the button to the right to register and receive your API via e-mail.
Note: AlchemyAPI is a paid service that charges per API transaction. It's free to try, and you'll get 1,000 transactions per day on the starter plan. Paid plans start at 5,000 daily transactions and go up to 200M+. Check out the pricing page for more information.
To make using AlchemyAPI easier, we've created a software development kit, or SDK for short. The SDK handles sending the API requests and converting the response data into an easy to use Python object. The source code is hosted on the popular social coding site, GitHub. To use the SDK, we need to clone the repo to your computer, which to the uninitiated basically means copying and pasting the code from GitHub to a folder on your computer.
The AlchemyAPI Python SDK Repo at http://github.com/alchemyapi/alchemyapi_python
To clone the SDK to your computer, open up a terminal window and type the following commands:
mkdir -p ~/src/test
git clone https://github.com/AlchemyAPI/alchemyapi_python.git
Note: this is assuming that you'll use ~/src/test as your software development directory. If you want to use a different directory, just navigate to that directory instead before cloning the SDK.
If everything worked, you should see the following output in your terminal window:
Cloning the AlchemyAPI Python SDK repo to your machine
If you don't have Git: If the Git version control system is not available on your machine, you can download a .zip file of the Python SDK from GitHub instead. Just go to: http://github.com/alchemyapi/alchemyapi_python and click the "Download ZIP" button on the right sidebar. Extract the files in the .zip to your software development directory, and then continue on with the tutorial.
Now that you have the Python SDK on your computer, all you need to do is configure it to use your API key. In the alchemyapi_python directory, run:
python alchemyapi.py YOUR_API_KEY
Where YOUR_API_KEY is the 40 character API key you received in your e-mail when you registered. If everything goes okay, you should see the following output:
Configuring the AlchemyAPI Python SDK to use your API Key
You're almost done! The Python SDK comes with an example that calls each of AlchemyAPI's text analysis functions with example data to show how to call each function, and how to parse the output. To run the example, simply type:
You should get lots of output in the terminal window as each function is called and the output is parsed, similar to this:
Example output from running the AlchemyAPI Python SDK example
Congratulations, you've now used all of AlchemyAPI's text analysis functions! However, all you've done is analyze some demo content, and it would be much more interesting to analyze some of your own content. To do that, you can use the example.py file as a guide. Just copy and paste the alchemyapi.py file into your project, and in the file that will handle the text analysis include the following lines:
from alchemyapi import AlchemyAPI
alchemyapi = AlchemyAPI()
Now you can use the alchemyapi object to access any of the text analysis functions, just like the example.py file does. For example, to calculate the sentiment for a simple sentence, try:
myText = "I'm excited to get started with AlchemyAPI!"
response = alchemyapi.sentiment("text", myText)
print "Sentiment: ", response["docSentiment"]["type"]
How you actually integrate AlchemyAPI into your project is outside of the scope of this tutorial, but you should now have the starting point of a proof-of-concept project. If you have any questions or comments, or if you are having trouble getting started with AlchemyAPI using Python, please contact support.