//Sentiment analysis algorithm for AsterixDB //Sentiment analysis class (with inverted index) public class SAInvertedIndex { //inverted index private HashMap> iWordMap; private HashMap iSentimentMap; //internal class for holding positive/negative sentiment values class SentimentValues { private double iPos; private double iNeg; public SentimentValues(double pos,double neg) { iPos=pos; iNeg=neg; } public double getPos(){return iPos;} public double getNeg(){return iNeg;} }//SentimentValues public double performSa(HashSet words) { double sumOfWords=0.0; //Find sentiment score for each word from the memory map Iterator it=words.iterator(); double value=0.0; while(it.hasNext()){ value=findWord(it.next()); sumOfWords=sumOfWords+value; } return sumOfWords; } double findWord(String word) { double doubleSum=0.0; int wordFound=0; SentimentValues sentValues=new SentimentValues(0,0); //Get indexes for word from wordMap HashSet intSet=iWordMap.get(word); if(intSet==null){ return 0.0; }//if Iterator it=intSet.iterator(); //Get sentiment values for word based on indexes while(it.hasNext()){ Integer integer=it.next(); sentValues=iSentimentMap.get(integer); double value=sentValues.getPos()-sentValues.getNeg(); doubleSum+=value; wordFound++; } if(wordFound==0) return 0.0; else{ double result=doubleSum/wordFound; return result; } }//findWord }//SaInvertedIndex //Java UDF class to be called, when receiving AQL query for sentiment analysis public class SaTweetFunction implements IExternalScalarFunction { public void evaluate(IFunctionHelper functionHelper) throws Exception { list.clear(); JRecord inputRecord = (JRecord) functionHelper.getArgument(0); JString text = (JString) inputRecord.getValueByName("message-text"); JRecord result = (JRecord) functionHelper.getResultObject(); JDouble newField = (JDouble) functionHelper.getObject(JTypeTag.DOUBLE); result.setField("tweetid", id); HashSet words=new HashSet(); //split tweet into tokens String[] tokens = text.getValue().split(" "); for (String tk : tokens) { words.add(tk); }//for //call Sentiment analysis (with inverted index) SAInvertedIndex saInv=SaIndexFunction.getSaInvertedIndex(); double resDouble=saInv.performSa(words); //output result newField.setValue(resDouble); result.setField("sa-result", newField); functionHelper.setResult(result); }//evaluate }//SaTweetFunction //Example: Sentiment analysis call from Test client to AsterixDB's REST API. Eventually, SaTweetFunction-class (UDF) will be called. for $user in dataset TweetMessages where $user.tweetid>=0 and $user.tweetid<=200 return testlib#saTweet($user);