All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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> headerInfo, List columns) {
		if(this.user == null) {
			SemossPixelException exception = new SemossPixelException(new NounMetadata("Requires login to google", PixelDataType.CONST_STRING, PixelOperationType.ERROR));
			exception.setContinueThreadOfExecution(false);
			throw exception;
		}
		AccessToken googleAccess = this.user.getAccessToken(AuthProvider.GOOGLE);
		if(googleAccess == null) {
			SemossPixelException exception = new SemossPixelException(new NounMetadata("Requires login to google", PixelDataType.CONST_STRING, PixelOperationType.ERROR));
			exception.setContinueThreadOfExecution(false);
			throw exception;
		}
		
		this.headerInfo = headerInfo;
		this.totalCols = headerInfo.size();
		
		String headerToConvert = columns.get(0);
		for(int j = 0; j < totalCols; j++) {
			Map headerMap = headerInfo.get(j);
			String alias = headerMap.get("alias").toString();
			if(alias.equals(headerToConvert)) {
				// we found the index
				this.colIndex = j;
			}
		}
		
		// this modifies the header info map by reference
		Map sentenceHeader = getBaseHeader("sentence", "STRING");
		this.headerInfo.add(sentenceHeader);
		Map magnitudeHeader = getBaseHeader("magnitude", "NUMBER");
		this.headerInfo.add(magnitudeHeader);
		Map scoreHeader = getBaseHeader("score", "NUMBER");
		this.headerInfo.add(scoreHeader);
	}
	
	/**
	 * Grab a base header object
	 * @param name
	 * @param type
	 * @return
	 */
	private Map getBaseHeader(String name, String type) {
		Map header = new HashMap();
		header.put("alias", name);
		header.put("header", name);
		header.put("derived", true);
		header.put("type", type.toUpperCase());
		return header;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy