package org.algorithm.core.neural.dataset;

import org.algorithm.util.TextFileReader;
import org.diagbot.pub.utils.PropertiesUtil;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 神经网络数据集，关键词非平行
 * 
 * @Author: bijl
 * @Date: 2018年7月26日-上午10:19:43
 * @Description:
 */
public class NNDataSetImplNonParallel extends NNDataSet {


    public NNDataSetImplNonParallel(String modelAndVersion) {
        super(modelAndVersion);
    }


    @Override
    public void readReSplitWordDict() {

    }

    @Override
    public void readFilterDiagnosisDict() {

    }

    @Override
    public float[] toFeatureVector(Map<String, Map<String, String>> inputs) {
        // inputs {症状名:{partbody:部位名, property:属性名, duration:时间类别, sex:性别值, age:年龄值}
        
        int numCols = 4;
        float[][] featureVector = new float[this.NUM_FEATURE][numCols];
        float[] results = new float[this.NUM_FEATURE * numCols];
        
        // 初始化featureVector
        for (int i=0; i<this.NUM_FEATURE; i++)
            for (int j=0; j<numCols; j++)
                featureVector[i][j] = 0.0f;

        Iterator<Entry<String, Map<String, String>>> entries = inputs.entrySet().iterator();

        String featureName = "";
        Integer position = -1;
        Map<String, String> featureValues = null;
        Entry<String, Map<String, String>> entry = null;
        
        float[] values = new float[numCols];  //  收录一行的值
        
        boolean isFirst = true;
        
        float sex = -2.0f;
        float age = -1.0f;
        
        /**
         * 数据方案设计
         */
        while (entries.hasNext()) {

            entry = entries.next();
            featureName = entry.getKey();
            featureValues = entry.getValue();
            position = this.FEATURE_DICT.get(featureName);
//            negative = this.NEGATIVE_DICT.get(featureValues.get("negative"));
            
            // 保留性别和年龄
            if (isFirst) {
                if (featureValues.get("sex") == "M")
                    sex = 1.0f;
                else if (featureValues.get("sex") == "F")
                    sex = -1.0f;
                else 
                    sex = 0.0f;
                
                age = Float.parseFloat(featureValues.get("age"));
                
                isFirst = false;
            }
            
            // 取值
            values[0] = 1.0f;
            
            if (featureValues.get("partbody") == null || "".equals(featureValues.get("partbody"))
                    || this.PARTBODY_DICT.get(featureValues.get("partbody")) == null)
                values[1] = this.PARTBODY_DICT.get("EMPTY").floatValue();
            else  // 值即是index
                values[1] = this.PARTBODY_DICT.get(featureValues.get("partbody")).floatValue();
            
            if (featureValues.get("property") == null || "".equals(featureValues.get("property"))
                    || this.PROPERTY_DICT.get(featureValues.get("property")) == null)
                values[2] = this.PROPERTY_DICT.get("EMPTY").floatValue();
            else
                values[2] = this.PROPERTY_DICT.get(featureValues.get("property")).floatValue();  
            
            values[3] = this.DURATION_DICT.get(featureValues.get("duration"));
            
            if (position != null) {
                for (int i=0; i<numCols;i++)
                    featureVector[position][i] = values[i];
            }
        }
        
        // 添加年龄和性别
        position = this.FEATURE_DICT.get("性别");
        featureVector[position][0] = sex;
        featureVector[position][1] = -1;
        featureVector[position][2] = -1;
        featureVector[position][3] = -1;
        
        position = this.FEATURE_DICT.get("年龄");
        featureVector[position][0] = age;
        featureVector[position][1] = -1;
        featureVector[position][2] = -1;
        featureVector[position][3] = -1;
        
     // 多维数组转一维数组（tensorflow需要），参看numpy或tensorflow reshape方法
        int index = 0;
        for (int i=0; i<this.NUM_FEATURE; i++)
            for (int j=0; j<numCols; j++) {
                results[index] = featureVector[i][j];
                index += 1;
            }
                
        return results;
    }

    @Override
    public int[] toCharIds(Map<String, Map<String, String>> inputs) {
        return new int[0];
    }

    @Override
    public int[] toPosIds(Map<String, Map<String, String>> inputs) {
        return new int[0];
    }


    /**
     * 读取字典
     */

    @Override
    public void readDict(String modelAndVersion) {
        
        PropertiesUtil prop = new PropertiesUtil("/algorithm.properties");
        String model_version = prop.getProperty(modelAndVersion);

        String filePath = prop.getProperty("basicPath");  // 基本目录
        filePath = filePath.substring(0, filePath.indexOf("model_version_replacement"));
        
        filePath = filePath + "dictionaries.bin";  // 字典文件位置
        
        List<String> lines = TextFileReader.readLines(filePath);

        boolean firstLine = true;
        
        String[] temp = null;
        for (String line : lines) {
            if (firstLine) {  // 去除第一行
                firstLine = false;
                continue;
            }
            
            temp = line.split("\\|");
            
            if(temp[3].equals(model_version)){
                int type_id = Integer.parseInt(temp[2]);
                int _index = Integer.parseInt(temp[1]);
                String _name = temp[0];

                if (type_id == 1)
                    this.FEATURE_DICT.put(_name, _index);
                else if (type_id == 2)
                    this.LABEL_DICT.put(_name, _index);
                else if (type_id == 8)
                    this.NEGATIVE_DICT.put(_name, _index);
                else if (type_id == 11)
                    this.PARTBODY_DICT.put(_name, _index);
                else if (type_id == 12)
                    this.PROPERTY_DICT.put(_name, _index);
                else if (type_id == 13)
                    this.DURATION_DICT.put(_name, _index);
            }

        }

        System.out.println("feature size:" + this.FEATURE_DICT.size());

    }

    @Override
    public void readChar2IdDict(String modelAndVersion) {

    }

}
