package com.mooctest.demo;



import com.hankcs.hanlp.mining.word2vec.Vector;
import net.semanticmetadata.lire.aggregators.AbstractAggregator;
import net.semanticmetadata.lire.aggregators.BOVW;
import net.semanticmetadata.lire.imageanalysis.features.GlobalFeature;
import net.semanticmetadata.lire.imageanalysis.features.LocalFeature;
import net.semanticmetadata.lire.imageanalysis.features.LocalFeatureExtractor;
import net.semanticmetadata.lire.imageanalysis.features.global.CEDD;
import net.semanticmetadata.lire.imageanalysis.features.global.FCTH;
import net.semanticmetadata.lire.imageanalysis.features.global.JCD;
import net.semanticmetadata.lire.imageanalysis.features.global.PHOG;
import net.semanticmetadata.lire.imageanalysis.features.global.spatialpyramid.SPACC;
import net.semanticmetadata.lire.imageanalysis.features.global.spatialpyramid.SPCEDD;
import net.semanticmetadata.lire.imageanalysis.features.global.spatialpyramid.SPFCTH;
import net.semanticmetadata.lire.imageanalysis.features.global.spatialpyramid.SPJCD;
import net.semanticmetadata.lire.imageanalysis.features.local.sift.SiftExtractor;
import net.semanticmetadata.lire.imageanalysis.features.local.sift.SiftFeature;
import net.semanticmetadata.lire.imageanalysis.features.local.surf.SurfExtractor;
import net.semanticmetadata.lire.imageanalysis.features.local.surf.SurfFeature;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class SimilaritySearchWithLIRE {
    public static void main(String[] args) throws IOException, IllegalAccessException, InstantiationException {
        GlobalFeature f1 = new SPCEDD();
        GlobalFeature f2 = new SPCEDD();
        BufferedImage img = ImageIO.read(new FileInputStream("/Users/major/Downloads/test8.png"));
        BufferedImage img2 = ImageIO.read(new FileInputStream("/Users/major/Downloads/test9.png"));
        f1.extract(img);
        double[] v1 = f1.getFeatureVector();
        f2.extract(img2);
        double[] v2 = f2.getFeatureVector();


        System.out.println("tanimoto sim: " + (1 - f1.getDistance(f2) / 100d));
        System.out.println("cosine sim: " + cosine(v1, v2));
    }

    public static double calculateEuclideanDistance(double[] vector1, double[] vector2) {

        double innerSum = 0.0;
        for (int i = 0; i < vector1.length; i++) {
            innerSum += Math.pow(vector1[i] - vector2[i], 2.0);
        }

        return Math.sqrt(innerSum);

    }

    public static float cosine(double[] v1, double[] v2)
    {
        Vector vec1 = new Vector(convertDoubleToFloat(v1));
        Vector vec2 = new Vector(convertDoubleToFloat(v2));
        return vec1.cosine(vec2);
    }

    public static float[] convertDoubleToFloat(double[] input)
    {
        if (input == null)
        {
            return null; // Or throw an exception - your choice
        }
        float[] output = new float[input.length];
        for (int i = 0; i < input.length; i++)
        {
            output[i] = (float) input[i];
        }
        return output;
    }


}
