
prerna.reactor.task.lambda.flatmap.GoogleSentimentAnalyzerLambda Maven / Gradle / Ivy
The newest version!
package prerna.reactor.task.lambda.flatmap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import prerna.auth.AccessToken;
import prerna.auth.AuthProvider;
import prerna.engine.api.IHeadersDataRow;
import prerna.io.connector.google.GoogleSentimentAnalyzer;
import prerna.om.SentimentAnalysis;
import prerna.sablecc2.om.PixelDataType;
import prerna.sablecc2.om.PixelOperationType;
import prerna.sablecc2.om.execptions.SemossPixelException;
import prerna.sablecc2.om.nounmeta.NounMetadata;
import prerna.util.Constants;
public class GoogleSentimentAnalyzerLambda extends AbstractFlatMapLambda {
private static final Logger classLogger = LogManager.getLogger(GoogleSentimentAnalyzerLambda.class);
// col index we care about to get lat/long from
private int colIndex;
// total number of columns
private int totalCols;
@Override
public List process(IHeadersDataRow row) {
Object value = row.getValues()[colIndex];
if(value == null || value.toString().isEmpty()) {
return new Vector();
}
// grab the column sindex we want to use as the address
Map params = new HashMap<>();
Map docParam = new HashMap<>();
docParam.put("type", "PLAIN_TEXT");
docParam.put("language", "EN");
docParam.put("content", value.toString().replace("_", " "));
params.put("document", docParam);
// construct new values to append onto the row
// add new headers
String[] newHeaders = new String[]{"sentence", "magnitude", "score"};
List retList = new Vector();
try {
// loop through the results
GoogleSentimentAnalyzer goog = new GoogleSentimentAnalyzer();
Object resultObj = goog.execute(this.user, params);
if(resultObj instanceof List) {
List results = (List) resultObj;
for(int i = 0; i < results.size(); i++) {
SentimentAnalysis sentiment = results.get(i);
processSentiment(sentiment, newHeaders, row, retList);
}
} else {
SentimentAnalysis sentiment = (SentimentAnalysis) resultObj;
processSentiment(sentiment, newHeaders, row, retList);
}
} catch(Exception e) {
classLogger.error(Constants.STACKTRACE, e);
}
return retList;
}
/**
* Process a sentiment
* @param sentiment
* @param newHeaders
* @param curRow
* @param retList
*/
private void processSentiment(SentimentAnalysis sentiment, String[] newHeaders, IHeadersDataRow curRow, List retList) {
Object[] newValues = new Object[3];
newValues[0] = sentiment.getSentence();
if(newValues[0] != null) {
newValues[0] = newValues[0].toString()
.replace("\n", " *LINE BREAK* ")
.replace("\r", " *LINE BREAK* ")
.replace("\t", " ")
.replace("\"", "");
}
newValues[1] = sentiment.getMagnitude();
newValues[2] = sentiment.getScore();
// copy the row so we dont mess up references
IHeadersDataRow rowCopy = curRow.copy();
rowCopy.addFields(newHeaders, newValues);
retList.add(rowCopy);
}
@Override
public void init(List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy