package org.studiorailgun.conversation.semantic;

import java.util.HashMap;
import java.util.Map;

import org.tensorflow.Result;
import org.tensorflow.SavedModelBundle;
import org.tensorflow.Tensor;
import org.tensorflow.ndarray.FloatNdArray;
import org.tensorflow.types.TFloat32;
import org.tensorflow.types.TInt64;

/**
 * Parses the subject of a sentence
 */
public class SentenceSubjectParser {

    /**
     * The model
     */
    SavedModelBundle model;
    
    /**
     * Initializes the model
     */
    public void init(){
        model = SavedModelBundle.load("./data/semantic/model");

        //describe the model
        // System.out.println(model.functions());
        // System.out.println(model.functions().get(0).signature());
        // System.out.println(model.functions().get(1).signature());
        // System.out.println(model.functions().get(2).signature());
        // System.out.println(model.functions().get(0).signature().getInputs());
        // System.out.println(model.functions().get(0).signature().getInputs().values().iterator().next().name);
        // System.out.println(model.functions().get(0).signature().getInputs().values().iterator().next().shape);
    }

    /**
     * Evaluates a sentence
     * @param sentence The sentence to evaluate
     */
    public void evaluate(String sentence){
        //run predict
        TInt64 tensor = TInt64.scalarOf(10);
        org.tensorflow.ndarray.Shape shape = tensor.shape().append(2001).append(128);
        tensor = TInt64.tensorOf(shape);

        Map<String,Tensor> inputMap = new HashMap<String,Tensor>();
        inputMap.put("keras_tensor",tensor);

        Result result = model.function("serve").call(inputMap);
        System.out.println(result.get(0));
        TFloat32 resultTensor = (TFloat32)result.get(0);
        FloatNdArray floatArr = resultTensor.get(0);
        System.out.println(floatArr.getFloat(0));
    }
}
