<commit_before>package search;

import documents.Document;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by Adam on 2015-11-26.
 */
public class FactorCalculator {

    public static Double getQueryFactor(ArrayList<String> queryKeywords) {
        Double underRoot = 0.0;
        for (String keyword : queryKeywords) {
            int docWithKeyword = SearchEngine.getInstance().getDocumentsWith(keyword);
            if (docWithKeyword > 0) {
                underRoot += Math.pow(Math.log(SearchEngine.getInstance().getDocuments().size() /
                        SearchEngine.getInstance().getDocumentsWith(keyword)), 2);
            }

        }
        return Math.sqrt(underRoot);
    }

    public static HashMap<String, Double> getQueryFactorMatrix(ArrayList<String> queryKeywords) {
        HashMap<String, Double> returnMatrix = new HashMap<>();
        for (String keyword : queryKeywords) {
            int docWithKeyword = SearchEngine.getInstance().getDocumentsWith(keyword);
            if (docWithKeyword > 0) {
                returnMatrix.put(keyword, Math.log(SearchEngine.getInstance().getDocuments().size() /
                        SearchEngine.getInstance().getDocumentsWith(keyword)));
            }
        }
        return returnMatrix;
    }

    public static Double getDocumentFactor(Document document) {
        HashMap<String, Integer> countedQuery = Helper.prepareKeywordsMatrix(document.getKeywords());
        Double underRoot = 0.0;
        Iterator it = countedQuery.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();
            underRoot += Math.pow((Integer) pair.getValue(), 2);
            it.remove();
        }
        return Math.sqrt(underRoot);
    }

    public static Double getSimilarity(ArrayList<String> querryList, Document document) {
        HashMap<String, Double> queryMatrix = getQueryFactorMatrix(querryList);
        HashMap<String, Double> documentMatrix = document.getTF();
        Double licznik = 0.0;
        Iterator it = queryMatrix.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();
            //System.out.println("docMatrix: " + documentMatrix.get(pair.getKey()) + " / mianownik: " + mianownik);
            if (documentMatrix.get(pair.getKey()) != null &&
                    !documentMatrix.get(pair.getKey()).toString().equals("-Infinity") &&<commit_msg>fixed bug<commit_after>package search;

import documents.Document;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by Adam on 2015-11-26.
 */
public class FactorCalculator {

    public static Double getQueryFactor(ArrayList<String> queryKeywords) {
        Double underRoot = 0.0;
        for (String keyword : queryKeywords) {
            int docWithKeyword = SearchEngine.getInstance().getDocumentsWith(keyword);
            if (docWithKeyword > 0) {
                underRoot += Math.pow(Math.log((double) SearchEngine.getInstance().getDocuments().size() /
                        SearchEngine.getInstance().getDocumentsWith(keyword)), 2);
            }

        }
        return Math.sqrt(underRoot);
    }

    public static HashMap<String, Double> getQueryFactorMatrix(ArrayList<String> queryKeywords) {
        HashMap<String, Double> returnMatrix = new HashMap<>();
        for (String keyword : queryKeywords) {
            int docWithKeyword = SearchEngine.getInstance().getDocumentsWith(keyword);
            if (docWithKeyword > 0) {
                returnMatrix.put(keyword, Math.log((double) SearchEngine.getInstance().getDocuments().size() /
                        SearchEngine.getInstance().getDocumentsWith(keyword)));
            }
        }
        return returnMatrix;
    }

    public static Double getDocumentFactor(Document document) {
        HashMap<String, Integer> countedQuery = Helper.prepareKeywordsMatrix(document.getKeywords());
        Double underRoot = 0.0;
        Iterator it = countedQuery.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();
            underRoot += Math.pow((Integer) pair.getValue(), 2);
            it.remove();
        }
        return Math.sqrt(underRoot);
    }

    public static Double getSimilarity(ArrayList<String> querryList, Document document) {
        HashMap<String, Double> queryMatrix = getQueryFactorMatrix(querryList);
        HashMap<String, Double> documentMatrix = document.getTF();
        Double licznik = 0.0;
        Iterator it = queryMatrix.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();
            //System.out.println("docMatrix: " + documentMatrix.get(pair.getKey()) + " / mianownik: " + mianownik);
            if (documentMatrix.get(pair.getKey()) != null &&
                    !documentMatrix.get(pair.getKey()).toString().equals("-Infinity") &&
{'fraction_non_alphanumeric': 0.1227164416203336, 'fraction_numerical': 0.007148530579825258, 'mean_word_length': 2.639450867052023, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 25, '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': '1357930', 'n_tokens_mistral': 1352, 'n_tokens_neox': 1311, 'n_words': 269}