package ch.hearc.p3.recsys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import ch.hearc.p3.recsys.bookanalysis.TypeData;
import ch.hearc.p3.recsys.bookanalysis.analysis.Abstract;
import ch.hearc.p3.recsys.bookanalysis.analysis.Analyzer;
import ch.hearc.p3.recsys.bookanalysis.analysis.Author;
import ch.hearc.p3.recsys.bookanalysis.analysis.Genre;
import ch.hearc.p3.recsys.bookanalysis.analysis.Subject;
import ch.hearc.p3.recsys.bookanalysis.analysis.Title;
import ch.hearc.p3.recsys.io.xml.ImportBooksXML;
import ch.hearc.p3.recsys.settings.SettingsBookAnalysis;
import ch.hearc.p3.recsys.settings.SettingsFilePaths;
import ch.hearc.p3.recsys.utils.Pair;

public class Main
{

	public static void main(String[] args) throws Exception
	{
		long start = System.currentTimeMillis();
		display(executeAnalyzer(createAnalyzer(ImportBooksXML.importBooksXML(SettingsFilePaths.BOOKS_FILE))));
		System.out.println("Total time : " + (System.currentTimeMillis() - start) / 1000.0 + " seconds");
	}

	private static void display(List<List<Pair<Integer, List<Pair<String, Double>>>>> features)
	{
		for (List<Pair<Integer, List<Pair<String, Double>>>> list : features)
		{
			System.out.println("One of the category Subject, Author, Genre, Title or Abstract");
			for (Pair<Integer, List<Pair<String, Double>>> pair : list)
			{
				System.out.println("Book : " + pair.getKey());
				for (Pair<String, Double> feature : pair.getValue())
					System.out.println(feature);
				System.out.println();
			}
			System.out.println();
		}
	}

	private static Analyzer[] createAnalyzer(List<Pair<Integer, Map<TypeData, List<String>>>> books)
	{
		Analyzer[] analyzer = new Analyzer[SettingsBookAnalysis.ATTRIBUTE_WEIGHT_FACTOR.size()];
		analyzer[0] = new Abstract(books);
		analyzer[1] = new Author(books);
		analyzer[2] = new Genre(books);
		analyzer[3] = new Title(books);
		analyzer[4] = new Subject(books);
		return analyzer;
	}

	private static List<List<Pair<Integer, List<Pair<String, Double>>>>> executeAnalyzer(Analyzer[] analyzer)
	{
		List<List<Pair<Integer, List<Pair<String, Double>>>>> features = new ArrayList<List<Pair<Integer, List<Pair<String, Double>>>>>();
		for (Analyzer an : analyzer)
			features.add(an.computeFeatures());
		return features;
	}
}

@@ -22,16 +22,102 @@
 ecsys;%0A%0A
+import java.io.BufferedWriter;%0Aimport java.io.FileWriter;%0Aimport java.io.IOException;%0A
 import j
@@ -182,16 +182,63 @@
 l.Map;%0A%0A
+import jdk.internal.jfr.events.FileWriteEvent;%0A
 import c
@@ -1241,20 +1241,174 @@
 eatures)
+ throws IOException%0A%09%7B%0A%09%09FileWriter fw = null;%0A%09%09BufferedWriter bw = null;%0A%0A%09%09try%0A%09%09%7B%0A%09%09%09fw = new FileWriter(%22output.txt%22);%0A%09%09%09bw = new BufferedWriter(fw);%0A
 %0A%09
-%7B%0A
 %09%09for (L
@@ -1477,31 +1477,23 @@
 )%0A%09%09
+%09
 %7B%0A%09%09%09
-System.out.println
+%09bw.write
 (%22On
@@ -1551,20 +1551,23 @@
 Abstract
+%5Cn
 %22);%0A
+%09
 %09%09%09for (
@@ -1628,32 +1628,24 @@
 %0A%09%09%09
+%09
 %7B%0A%09%09%09%09
-System.out.println
+%09bw.write
 (%22Bo
@@ -1666,19 +1666,27 @@
 getKey()
+ + %22%5Cn%22
 );%0A
+%09
 %09%09%09%09for 
@@ -1743,89 +1743,216 @@
 %09%09%09%09
-System.out.println(feature);%0A%09%09%09%09System.out.println();%0A%09%09%09%7D%0A%09%09%09System.out.println
+%09bw.write(feature + %22%5Cn%22);%0A%09%09%09%09%09bw.write(%22%5Cn%22);%0A%09%09%09%09%7D%0A%09%09%09%09bw.write(%22%5Cn%22);%0A%09%09%09%7D%0A%09%09%7D catch (Exception e)%0A%09%09%7B%0A%09%09%09System.err.println(%22Error during the writing process%22);%0A%09%09%7D finally%0A%09%09%7B%0A%09%09%09bw.close();%0A%09%09%09fw.close
 ();%0A

Write the result into a file