Integration Cookbook AlchemyAPI
Share!TwitterFacebookLinkedInGoogle+Email

Hi everyone, this is my first technical post on SemantAPI and I hope it won’t be the last. In the coming weeks I’ll be detailing my first-hand experience integrating the sentiment analysis APIs used with SemantAPI. I will try to be as objective as possible and highlight all aspects of the integration from my personal experience.
It wasn’t easy to decide which services should be used in the SemantAPI toolkit. In the end, I think we chose the 6 best sentiment APIs on the market, all of which claim the highest accuracy of sentiment analysis out of the box. The APIs featured in SemantAPI include:

  • AlchemyAPI.
  • Semantria.
  • ChatterBox.
  • Viralheat.
  • Bitext.

AlchemyAPI

Alchemy API is one of the most diverse NLP solutions offering a wide range of features through their RestAPI.

Alchemy offers SDKs for nine programming languages, including a raw .NET SDK (which is what I used), available on their website (http://www.alchemyapi.com/developers/sdks/).  According to their documentation, Alchemy supports both popular data formats, XML and JSON. However, when I downloaded their SDK I noticed that the SDK methods returned only JSON, even if I set XML as the output format. Therefore, the following code returned JSON with Alchemy’s .Net SDK.

AlchemyAPI.AlchemyAPI session = new AlchemyAPI.AlchemyAPI();
session.SetAPIKey(context.Key);
AlchemyAPI_TargetedSentimentParams parameters = new AlchemyAPI_TargetedSentimentParams();
parameters.setOutputMode(AlchemyAPI_BaseParams.OutputMode.XML);
string strResult = session.TextGetTextSentiment(document.Value.Source, parameters);

I reported the error and fixed this mistake in their SDK so I was able to get XML output for my evaluation. However, this wasn’t the only problem I ran into. The second, more interesting issue I encountered was that all their SDK methods returned only raw XML strings instead of well-liked objects, which are native for Object Oriented languages. So, I added a few small types in order to de-serialize Alchemy’s output into objects. I was interested in only working with the sentiment output, so it took only 25 lines of code along with empty line delimiters. Given the simplicity of this addition, I’m not sure why it wasn’t added on the SDK level from scratch.

[XmlRootAttribute("docSentiment", Namespace = "")]
public sealed class AlchemyDocSentiment
{
[XmlElementAttribute("type")]
public string Type { get; set; }

[XmlElementAttribute("score")]
public double Score { get; set; }

[XmlElementAttribute("mixed")]
public int Mixed { get; set; }
}
[XmlRootAttribute("results", Namespace = "")]
public sealed class AlchemySentimentResult
{
[XmlElementAttribute("status")]
public string Status { get; set; }
 
[XmlElementAttribute("language")]
public string Language { get; set; }

[XmlElementAttribute("docSentiment", typeof(AlchemyDocSentiment))]
public AlchemyDocSentiment SentimentDetails { get; set; }
}

Finally, I was able to run my test and got the sentiment results.

Overall opinion

Because of the availability of their SDKs, integration with Alchemy API was fast and didn’t require any specific skills except the knowledge of the selected language/platform. At the same time, the SDK contained a lot of unfinished work, so polishing was necessary. The raw SDK, at least the .Net version wasn’t useful for immediate integration.

 

Stay tuned for next week when I’ll be covering Semantria.

George Kozlov is a software engineering guru. He specializes in software research, architecture and maintenance. He co-founded Semantria, and is currently their CTO and go-to guy when things need improving.

Share!TwitterFacebookLinkedInGoogle+Email