<commit_before>package mapreduce;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;

public class RegressionReducer<KEY> extends Reducer<KEY, CompositeWritable, KEY, CompositeWritable> {
	private CompositeWritable resultSet = null;
	private List<CompositeWritable> cache = new ArrayList<CompositeWritable>();
	
	public void reduce(KEY key, Iterable<CompositeWritable> values, Context context) throws IOException, InterruptedException {
		int n = 0;
		int somaX = 0;
		double somaY = 0.0;
		double mediaX = 0.0;
		double mediaY = 0.0;
		double b = 0.0;
		double a = 0.0;
		double bNumerador = 0.0;
		double bDenominador = 0.0;
		double xMin = Double.MAX_VALUE;
		double xMax = 0.0;
		
		for (Iterator<CompositeWritable> iterator = values.iterator(); iterator.hasNext();) {
			CompositeWritable aux = iterator.next();
			
			DoubleWritable valorYWritable = aux.getEstatisticaY();
			double valorY = valorYWritable.get();
			
			System.out.println("\n\tOY"+valorY+"\n");
			somaY += valorY;
			
			Text valorXWritable = aux.getAgrupadorX();
			int valorX = Integer.parseInt(valorXWritable.toString());
			
			System.out.println("\n\tOX"+valorX+"\n");
			
			somaX += valorX;
			n++;
			if(valorX < xMin){
				xMin = valorX;
			}
			if(valorX > xMax){
				xMax = valorX;
			}
			// Cria cache pois nao e possivel iterar novamente
			cache.add(aux);
		}
		mediaY = somaY/n;
		mediaX = somaX/n;
		System.out.println("MediaX: "+mediaX+". MediaY: "+mediaY);
		
		for (Iterator<CompositeWritable> iterator = cache.iterator(); iterator.hasNext();) {
			CompositeWritable aux = (CompositeWritable) iterator.next();
			
			DoubleWritable valorYWritable = aux.getEstatisticaY();
			double valorY = valorYWritable.get();
			
			
			// ATENCAO
			Text valorXWritable = aux.getAgrupadorX();
			double valorX = Double.parseDouble(valorXWritable.toString());
			
			bNumerador += (valorX*(valorY-mediaY));
			System.out.println("bNumerador: "+bNumerador);
			bDenominador += (valorX*(valorX-mediaX));
			System.out.println("bDenominador: "+bDenominador);
		}<commit_msg>Utiliza Collections.sort para realizar regressao<commit_after>package mapreduce;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;

public class RegressionReducer<KEY> extends Reducer<KEY, CompositeWritable, KEY, CompositeWritable> {
	private CompositeWritable resultSet = null;
	private List<CompositeWritable> cache = new ArrayList<CompositeWritable>();
	
	public void reduce(KEY key, Iterable<CompositeWritable> values, Context context) throws IOException, InterruptedException {
		int n = 0;
		int somaX = 0;
		double somaY = 0.0;
		double mediaX = 0.0;
		double mediaY = 0.0;
		double b = 0.0;
		double a = 0.0;
		double bNumerador = 0.0;
		double bDenominador = 0.0;
		double xMin = Double.MAX_VALUE;
		double xMax = 0.0;
		
		for (Iterator<CompositeWritable> iterator = values.iterator(); iterator.hasNext();) {
			CompositeWritable temp = iterator.next();
			cache.add(temp);
		}
		// Utiliza o metodo compareTo implementado em CompositeWritable
		Collections.sort(cache);
		
		for (int i = 0; i < cache.size(); i++) {
			CompositeWritable cw = cache.get(i);
			DoubleWritable valorYWritable = cw.getEstatisticaY();
			double valorY = valorYWritable.get();
			somaY += valorY;
			
			int valorX = i;
			somaX += valorX;
			
			System.out.println("\tValor Y: "+valorY+"\n");
			System.out.println("\tValor X: "+valorX+"\n");
			System.out.println("\tValor X real: "+cw.getAgrupadorX().toString()+"\n");
			System.out.println("\tSomaX: "+somaX+"\n");
			System.out.println("\tSomaY: "+somaY+"\n");
			
			n++;
			if(valorX < xMin){
				xMin = valorX;
			}
			if(valorX > xMax){
				xMax = valorX;
			}
		}
		mediaY = somaY/n;
		mediaX = somaX/n;
		System.out.println("MediaX: "+mediaX+". MediaY: "+mediaY);
		
		for (int i = 0; i < cache.size(); i++) {
			CompositeWritable cw = cache.get(i);
			
			DoubleWritable valorYWritable = cw.getEstatisticaY();
			double valorY = valorYWritable.get();
			
			int valorX = i;
			
			bNumerador += (valorX*(valorY-mediaY));
			bDenominador += (valorX*(valorX-mediaX));
			
			System.out.println("\tValor Y: "+valorY+"\n");			
			System.out.println("\tValor X: "+valorX+"\n");
			System.out.println("\tValor X real: "+cw.getAgrupadorX().toString()+"\n");
			System.out.println("bNumerador: "+bNumerador);
			System.out.println("bDenominador: "+bDenominador);
			
		}
{'fraction_non_alphanumeric': 0.1417004048582996, 'fraction_numerical': 0.007692307692307693, 'mean_word_length': 3.960843373493976, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4806862', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1743, 'n_words': 358}