Sentiment Analysis API resources

On this page we collect code examples of the implementations of the Sentiment Analysis API documentation, together with the best practices in the deployment and the use of the code.

This page currently contains the (links to) following implementations:

  • Python
  • Ruby
  • PHP

We encourage, support and reward the community efforts to extend and streamline the implementations for different platforms and programming languages, as we believe that no one knows their tools better than the seasoned developers themselves. If you want to know more or wish to submit an implementation of your own, please contact us!

 
Other-python-icon.png

Python (2.6, 2.7,3.x) implementation

This Python implementation allows you to communicate with the Sentiment Analysis API using your an API key. To use this implementation in your own project, please make sure you extract it in your project's directory, or somewhere on the Python's path.  

PYTHON 2.x Version code:

Download the Python class for Sentiment Analysis API here 

PYTHON 3.x Version code, kindly provided by Willem Lenaerts:

Download the Python class for Sentiment Analysis API here

The ZIP file above contains two files: 

  1. aia_SentimentAnalysisAPI.py: This contains the API wrapper class which can be instantiatied to talk directly to the API. To be instantiated, the class requires an API key. 
  2. test_sentiment.py: a simple test script, creating an instance of the aia_SentimentAnalysisAPI class, adding some data for processing, and processing it using the API.

The API connection class is an extension of a ordinary Python list, inheriting all the Python list behaviors, and adding a process() command allowing you to call the API. A use example of the class is present in the test script, with a simple example provided here:

# Import the API connection library
import aia_SentimentAnalysisAPI

# Create a API connection instance using your API key and, optionally, the classifier you wish to use
# The optional data parameter allows to assign the data in the appropriate format to the API if you have it stored
# in another Python list object, for instance, if you have done preprocessing by Language Detection API
api = aia_SentimentAnalysisAPI.aia_SentimentAnalysisAPI("<your_api_key_here>", sentiment_classifier="default", data=[])

# Add some objects for classification to the API connection instance in the same way you would add them to a ordinary Python list
# Please make sure you add "language_iso" markers to every entry. If you have masses of text and need to detect the languages
# automatically, please pre-process the data with the Language Detection API.
# For more information on language codes, please consult the Sentiment Analysis API documentation
api.append({"text": "This is a test.", "language_iso": "eng"})
api.append({"text": "Dit is een test.", "language_iso": "nld"})
api.append({"text": "Dies ist ein Test.", "language_iso": "deu"})

# Execute the API call for all your text, optionally specifying whether you want to have verbose output of the processing
# The objects in the api list will be automatically changed by this call, adding to them the annotations from the API
# The text summarizing the processing will be returned by the .process() call
summary = api.process(verbose=False)

# Print the processing summary to stdout
>> print summary
"OK. Call processed."

# Print the first object from the processed data. Remember, the api instance is just another Python list
# allowing you all the normal list operations on it.
>> print api[0]
{"text": "This is a test.", "language_iso": "eng", "sentiment_class": "negative", "confidence_sentiment": 0.509487627042885}
 

Ruby Implementation

A Ruby implementation has been created by Andrea Mostosi, and made available on GitHub and on the Ruby Gems overview. To obtain the implementation (documentation included), please use the following links:

Code on GitHub: 

https://github.com/zenkay/ai-applied-ruby

Ruby Gem: 

http://rubygems.org/gems/applied

 
phplogo-highres.png

PHP Implementation

A PHP implementation of all Ai Applied's APIs has been created by Mark Mooij. This implementation also supports the Sentiment Analysis API. The full implementation is downloadable from this link:

Download the PHP implementation of, amongst others, Sentiment Analysis API

The PHP implementetion contains a api_tester.php file, containing the actual code which calls the APIs, as well as a index.php file which contains API call examples. Both files are well documented on different aspects and configuration parameters for calling the API. At this point in time, we consider this implementation to be rudimentary, and any additions or re-implementations are most welcome!

 

For more information, please consult the Sentiment Analysis API documentation.

Please feel free to use the comments section for your questions, discussion and tips!