package model;

import dataObjects.Document;
import javafx.collections.ObservableList;
import model.similarity.Cosine;
import model.similarity.DotProduct;
import model.similarity.Similarity;
import model.weight.IDF;
import model.weight.NormalizedIDF;
import model.weight.Weight;
import utilities.TermExtractor;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;

/**
 * Created by Thagus on 04/09/16.
 */
public class ModelOperations {
    private Weight weight;
    private Similarity similarity;

    private HashMap<String, Weight> weightHashMap;
    private HashMap<String, Similarity> similarityHashMap;

    protected ModelOperations(Connection connection) throws SQLException{
        similarityHashMap = new HashMap<>();
        weightHashMap = new HashMap<>();

        //Create similarity objects
        similarityHashMap.put("Dot product", new DotProduct(connection));
        similarityHashMap.put("Cosine", new Cosine(connection));

        //Create weight objects
        weightHashMap.put("IDF", new IDF(connection));
        weightHashMap.put("Normalized IDF", new NormalizedIDF(connection));


        //Initialize with IDF and DotProduct
        setSimilarityMethod("Dot product");
        setWeightMethod("IDF");
    }

    /**
     * A method to evaluate a query calculating its similarity against the documents,
     * in order to get the documents sorted by relevance
     *
     * @param query The input query from the user
     * @return An ArrayList of Documents sorted by their similarity to the query
     */
    public ObservableList<Document> evaluateQuery(String query){
        HashMap<String, Integer> wordCount = TermExtractor.extractTerms(query);           //Counter for word occurrence in the query
        ObservableList<Document> searchResult;

        searchResult = similarity.calculateSimilarity(wordCount);

        //Sort the ArrayList
        Collections.sort(searchResult, Collections.reverseOrder());

        //Return
        return searchResult;
    }

    /**
     * A method to calculate the weights of the inserted terms
     */
    public void calculateWeights() {
        weight.calculateWeights();
    }

    /**
     *  Getters
     */

    public String getSimilarityName(){
        return similarity.getSimilarityMethodName();
    }

    public String getWeightName(){
        return weight.getWeightMethodName();
    }

    public Set<String> getSimilarityMethods(){
        return similarityHashMap.keySet();
    }

    public Set<String> getWeightMethods(){
        return weightHashMap.keySet();
    }

    /**
     * Set the weight method
     * @param weightMethod the weight method name to be used
     */
    public void setWeightMethod(String weightMethod){
        if(this.weight==null || this.weight.getWeightMethodName()!=weightMethod) {
            this.weight = weightHashMap.get(weightMethod);
            System.out.println("Calculating weights...");
            calculateWeights();
            System.out.println("Weights calculated!");
        }
        else{
            System.out.println("\nTrying to calculate an already calculated weight method!\nSkipping...\n");
        }
    }

    /**
     * Set the similarity method
     * @param similarityMethod The similarity method name to be used
     */
    public void setSimilarityMethod(String similarityMethod){
        this.similarity = similarityHashMap.get(similarityMethod);
    }
}

@@ -84,40 +84,8 @@
 st;%0A
-import model.similarity.Cosine;%0A
 impo
@@ -172,16 +172,18 @@
 .weight.
+TF
 IDF;%0Aimp
@@ -209,16 +209,18 @@
 rmalized
+TF
 IDF;%0Aimp
@@ -932,32 +932,34 @@
 tion));%0A        
+//
 similarityHashMa
@@ -1059,16 +1059,19 @@
 ap.put(%22
+TF-
 IDF%22, ne
@@ -1072,16 +1072,18 @@
 F%22, new 
+TF
 IDF(conn
@@ -1130,16 +1130,19 @@
 malized 
+TF-
 IDF%22, ne
@@ -1153,16 +1153,18 @@
 rmalized
+TF
 IDF(conn
@@ -1201,16 +1201,19 @@
 ze with 
+TF-
 IDF and 
@@ -1292,16 +1292,19 @@
 Method(%22
+TF-
 IDF%22);%0A 
@@ -2220,32 +2220,33 @@
 ights();%0A    %7D%0A%0A
+%0A
     /**%0A     *  
@@ -2266,194 +2266,8 @@
 */%0A%0A
-    public String getSimilarityName()%7B%0A        return similarity.getSimilarityMethodName();%0A    %7D%0A%0A    public String getWeightName()%7B%0A        return weight.getWeightMethodName();%0A    %7D%0A%0A
     
@@ -2778,20 +2778,20 @@
 ut.print
-ln
 (%22
+%5Cn
 Calculat
@@ -2868,18 +2868,16 @@
 ut.print
-ln
 (%22Weight
@@ -2889,16 +2889,18 @@
 culated!
+%5Cn
 %22);%0A    

Rename TFIDF