Thursday 10 April 2014

Liferay 6.2 + Sentiment analysis with Alchemy API

Hi Guys,

In this era of IT industries transforming words into knowledge is much more useful and alchemy API is just the right thing to do these things and many more.

This post is just of an overview or you can say just a meet up what exactly Alchemy API is and how does it work.

Now you can easily perform sentiment analysis, keyword extraction, entity extraction, image tagging and much more on the massive volumes of web pages, documents, tweets and photos produced every second with Alchemy API.

In this post we are going to do sentiment analysis of the blog entry written in liferay sites and it will give xml output for each blog entry on console. You can also use search container or any document generator API for listing out the result.

I am providing you very basic and raw example.

Lets get started...

1) First of all you have to get register key for your application in which you are using this particular API.
You can get free API key here.

2) Create one simple MVC portlet as shown in below image.




3) Now download Alchemy SDK (specific to java language) from this link.

4) Now Extract that sdk and go to the path where build.xml resides and run "ant jar" command from the cmd and it will create AlchemyAPI.jar file in dist folder.

5) Copy that jar file into our portlet lib folder and we are ready to go.

6) Open view.jsp file and copy below content in it.


 <%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %>  
 <%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>  
 <portlet:defineObjects/>  
 <aui:a href='<%= renderRequest.getParameter("url")%>'><%= renderRequest.getParameter("url")%></aui:a>  
 <portlet:actionURL name="doAnalyze" var="doAnalyze"></portlet:actionURL>  
 <aui:button value="Analyze" onClick="<%=doAnalyze %>" name="Analyze"></aui:button>  
 This is the <b>Sentiment Analysis</b> portlet.  

7) Open controller.java class which is our mvcPortlet class and copy below content in it.


 package com.cignex.demo;  
 import java.io.IOException;  
 import java.io.StringWriter;  
 import java.util.Iterator;  
 import java.util.List;  
 import javax.portlet.ActionRequest;  
 import javax.portlet.ActionResponse;  
 import javax.portlet.PortletException;  
 import javax.xml.parsers.ParserConfigurationException;  
 import javax.xml.transform.Transformer;  
 import javax.xml.transform.TransformerException;  
 import javax.xml.transform.TransformerFactory;  
 import javax.xml.transform.dom.DOMSource;  
 import javax.xml.transform.stream.StreamResult;  
 import javax.xml.xpath.XPathExpressionException;  
 import org.w3c.dom.Document;  
 import org.xml.sax.SAXException;  
 import com.alchemyapi.api.AlchemyAPI;  
 import com.liferay.portal.kernel.exception.SystemException;  
 import com.liferay.portlet.blogs.model.BlogsEntry;  
 import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;  
 import com.liferay.util.bridges.mvc.MVCPortlet;  
 public class Controller extends MVCPortlet {  
      public void doAnalyze(ActionRequest actionRequest,  
                ActionResponse actionResponse) throws IOException, PortletException {  
           try {  
                List<BlogsEntry> blogEntryList = BlogsEntryLocalServiceUtil.getBlogsEntries(0, BlogsEntryLocalServiceUtil.getBlogsEntriesCount());  
                System.out.println("number of blogs:"+blogEntryList.size());  
                for (Iterator iterator = blogEntryList.iterator(); iterator.hasNext();) {  
                     BlogsEntry blogsEntry = (BlogsEntry) iterator.next();  
                     AlchemyAPI alchemyObj = AlchemyAPI.GetInstanceFromString("yourtapikey");  
                     try {  
                          Document doc = alchemyObj.TextGetTextSentiment(blogsEntry.getContent());  
                          System.out.println(getStringFromDocument(doc));  
                     } catch (XPathExpressionException e) {  
                          // TODO Auto-generated catch block  
                          e.printStackTrace();  
                     } catch (SAXException e) {  
                          // TODO Auto-generated catch block  
                          e.printStackTrace();  
                     } catch (ParserConfigurationException e) {  
                          // TODO Auto-generated catch block  
                          e.printStackTrace();  
                     }  
                }  
           } catch (SystemException e) {  
                e.printStackTrace();  
           }  
      }  
       private static String getStringFromDocument(Document doc) {  
          try {  
            DOMSource domSource = new DOMSource(doc);  
            StringWriter writer = new StringWriter();  
            StreamResult result = new StreamResult(writer);  
            TransformerFactory tf = TransformerFactory.newInstance();  
            Transformer transformer = tf.newTransformer();  
            transformer.transform(domSource, result);  
            return writer.toString();  
          } catch (TransformerException ex) {  
            ex.printStackTrace();  
            return null;  
          }  
        }  
 }  

Please note that you have to replace your registered api key in above code other wise you will get exception.

8) Deploy the portlet and put it on the page and click analyse button you will get sentiment analysis as a result.




9) Output result:



Done :) now you can also play with AlchemyApi and can get meaning full data do lot more then this.

GOOD DAY


No comments:

Post a Comment