<commit_before>package com.example.agonyaunt;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import android.content.Context;
import android.os.Build;

/** This class manages user ratings
 * @author <NAME>
 */

public class Rater {

	Context context;
	// For writing
	InputStream is;
	FileOutputStream fs;
	// Question manager
	QuestionManager quesMan = new QuestionManager();
	int qID_bits = quesMan.getQIDbits();
	int num_Qs = quesMan.getNUM_QUESTIONS();
	int sub_per_Q = quesMan.getSUB_PER_Q();
	Question[] mainQs, subQs;
	// Vars for getting the best next Q
	int best_rating = 0;
	int best_id;
	// To write out
	ArrayList<byte[]> for_net_list = new ArrayList<byte[]>();
	int[][] store;

	public Rater(Context context) {
		this.context = context;
		loadFile();
	}

	/** Gets the ratings file */
	public void loadFile() {
		// Open ratings
		File f = new File(context.getFilesDir(), "ratings_for_app");
		Scanner scan;

		try {
			f.createNewFile();
			scan = new Scanner(f);

			// Iterate through file
			int i = 0;
			int j = 0;
			int sub_index;
			int rating;
			// Get the questions -> question in the first level
			mainQs = quesMan.getTherapQuestions();

			while (scan.hasNext()) {
				// Save ratings
				sub_index = Integer.parseInt(scan.next());
				rating = Integer.parseInt(scan.next());

				update(i, sub_index, rating, true);
				j++;
				if (j > 0 && j % 2 == 0)
					i++;
			}
			scan.close();
			calculate();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/** Print question and ratings for debugging */
	public void calculate() {
		mainQs = quesMan.getTherapQuestions();
		for (int i = 0; i < mainQs.length; i++) {
			System.out.println(mainQs[i].getContent());
			subQs = mainQs[i].getSubQuestions();
			for (int j = 0; j < subQs.length; j++) {
				System.out.println(subQs[j].getID()[0] + "   "
						+ subQs[j].getRating());
			}
		}

	}

	/** Update the rating of a subquestion
	 * @param parent	Index of the parent
	 * @param sub		Index of the sub question
	 * @param new_rate	New rating
	 * @param loading	Whether we're just loading the questions
	 */
	public void update(int parent, int sub, int new_rate, boolean loading) {
		// If we aren't just loading from the file
		
			int old_rate = mainQs[parent].getSubQuestions()[sub].getRating();
			// If the question has not been asked for the first time 
			// (i.e. rating is not -1)
			// And we aren't just loading the questions
			if (!loading && old_rate >= 0) {
				// Get the average for the new rate
				new_rate = (new_rate + old_rate) / 2;
			}
			// Update
			quesMan.update(parent, sub, new_rate);
			mainQs[parent].getSubQuestions()[sub].setRating(new_rate);

	}

	/** Get best question paring
	 * @throws IOExeption
	 */
	public void getBest() throws IOException {<commit_msg>Rater value store in shared Preference so can be save to database<commit_after>package com.example.agonyaunt;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.util.Log;

/** This class manages user ratings
 * @author <NAME>
 * @author <NAME>
 */

public class Rater {

	Context context;
	// For writing
	InputStream is;
	FileOutputStream fs;
	// Question manager
	QuestionManager quesMan = new QuestionManager();
	int qID_bits = quesMan.getQIDbits();
	int num_Qs = quesMan.getNUM_QUESTIONS();
	int sub_per_Q = quesMan.getSUB_PER_Q();
	Question[] mainQs, subQs;
	// Vars for getting the best next Q
	int best_rating = 0;
	int best_id;
	// To write out
	ArrayList<byte[]> for_net_list = new ArrayList<byte[]>();
	int[][] store;

	public Rater(Context context) {
		this.context = context;
		loadFile();
	}

	/** Gets the ratings file */
	public void loadFile() {
		// Open ratings
		File f = new File(context.getFilesDir(), "ratings_for_app");
		Scanner scan;

		try {
			f.createNewFile();
			scan = new Scanner(f);

			// Iterate through file
			int i = 0;
			int j = 0;
			int sub_index;
			int rating;
			// Get the questions -> question in the first level
			mainQs = quesMan.getTherapQuestions();

			while (scan.hasNext()) {
				// Save ratings
				sub_index = Integer.parseInt(scan.next());
				rating = Integer.parseInt(scan.next());

				update(i, sub_index, rating, true);
				j++;
				if (j > 0 && j % 2 == 0)
					i++;
			}
			scan.close();
			calculate();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/** Print question and ratings for debugging */
	public void calculate() {
		mainQs = quesMan.getTherapQuestions();
		for (int i = 0; i < mainQs.length; i++) {
			System.out.println(mainQs[i].getContent());
			subQs = mainQs[i].getSubQuestions();
			for (int j = 0; j < subQs.length; j++) {
				System.out.println(subQs[j].getID()[0] + "   "
						+ subQs[j].getRating());
			}
		}

	}

	/** Update the rating of a subquestion
	 * @param parent	Index of the parent
	 * @param sub		Index of the sub question
	 * @param new_rate	New rating
	 * @param loading	Whether we're just loading the questions
	 */
	public void update(int parent, int sub, int new_rate, boolean loading) {
		// If we aren't just loading from the file
        Question[] subQs = quesMan.getTherapeuticQ(parent).getSubQuestions();
			int old_rate = subQs[sub].getRating();
        Log.w("BEFORE RATE IS SET:",old_rate+"");
//            int old_rate = mainQs.
			// If the question has not been asked for the first time 
			// (i.e. rating is not -1)
			// And we aren't just loading the questions
			if (!loading && old_rate >= 0) {
				// Get the average for the new rate
				new_rate = (new_rate + old_rate) / 2;
			}
			// Update
//			quesMan.update(parent, sub, new_rate);
//			mainQs[parent].getSubQuestions()[sub].setRating(new_rate);
            subQs[sub].setRating(new_rate);

        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPref.edit();
        int sub0Rate = subQs[0].getRating();
        int sub1Rate = subQs[1].getRating();
        editor.putInt("sub0Rate", sub0Rate);
        editor.putInt("sub1Rate", sub1Rate);
        editor.commit();

        Log.w("IF RATE IS SET:", subQs[sub].getRating()+"");

	}

	/** Get best question paring
	 * @throws IOExeption
	 */
	public void getBest() throws IOException {
{'fraction_non_alphanumeric': 0.12999567162025683, 'fraction_numerical': 0.004616938392728322, 'mean_word_length': 3.310945273631841, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 16, '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': '5038391', 'n_tokens_mistral': 2565, 'n_tokens_neox': 2255, 'n_words': 648}