package org.diagbot.nlp.test;

import org.diagbot.nlp.participle.ParticipleUtil;
import org.diagbot.nlp.participle.word.Lexeme;
import org.diagbot.pub.jdbc.MysqlJdbc;
import org.diagbot.pub.utils.security.EncrypDES;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName org.diagbot.nlp.test.SplitDicTest
 * @Description TODO
 * @Author fyeman
 * @Date 2019/4/25/025 14:36
 * @Version 1.0
 **/
public class SplitDicTest {
    public static void main(String[] args) {
        SplitDicTest test = new SplitDicTest();
//        test.split();

        test.createDic();
//        test.copy();
    }

    public void createDic() {
        MysqlJdbc nlpJdbc = new MysqlJdbc("root", "diagbot@20180822", "jdbc:mysql://192.168.2.235:3306/diagbot-rule?useUnicode=true&characterEncoding=UTF-8");
        List<Map<String, String>> data = nlpJdbc.query("medicine_rule_split", new String[]{"standword", "split_word"}, " where split_word is not null");
        Map<String, String> words = new HashMap<>();

        String word = "";
        String[] split_words = null;
//        for (Map<String, String> map : data) {
//            word = map.get("standword");
//            words.put(word, word);
//        }
        for (Map<String, String> map : data) {
            word = map.get("split_word");
            if (word.indexOf(",") > -1) {
                split_words = word.split(",");
                for (int i  = 0; i < split_words.length; i++){
                    if (words.get(split_words[i]) == null) {
                        words.put(split_words[i], split_words[i]);
                    }
                }
            } else {
                if (words.get(word) == null) {
                    words.put(word, word);
                }
            }
        }
        List<Map<String, Object>> lists = new ArrayList<>();
        for (Map.Entry<String, String> m : words.entrySet()) {
            Map<String, Object> row = new HashMap<>();
            row.put("name", m.getKey());
            row.put("category_id", "-2");
            row.put("category","本次整理");
            lists.add(row);
        }

//        nlpJdbc.insert(lists, "kl_standard_split", new String[]{"name", "category_id", "category"});

        System.out.println("肌紧张:"+words.get("肌紧张"));
        try {
            String path = this.getClass().getClassLoader().getResource("").getPath();
            path = path.substring(0, path.indexOf("target"));
            FileWriter fw = new FileWriter(path + "src/main/resources/tc.dict");

            EncrypDES encrypDES = new EncrypDES();
            for (Map.Entry<String, String> entry : words.entrySet()) {
                fw.write(encrypDES.encrytor(entry.getKey() + "|9|9|9"));
                fw.write("\n");
            }
            fw.close();

        } catch (Exception ioe) {
            ioe.printStackTrace();
        }
    }

    public void copy() {
        MysqlJdbc nlpJdbc = new MysqlJdbc("root", "diagbot@20180822", "jdbc:mysql://192.168.2.235:3306/diagbot-rule?useUnicode=true&characterEncoding=UTF-8");
        List<Map<String, String>> data = nlpJdbc.query("medicine_rule_origin", new String[]{"id", "disease", "type", "number", "standword", "extract_word"}, " where number like '1.%' or number like '2.%'");

        String extract_word = "";
        String[] ews  = null;
        List<Map<String, Object>> lists = new ArrayList<>();
        for (Map<String, String> map : data) {
            extract_word = map.get("extract_word");
            if ("基础体温呈单相".equals(map.get("standword"))) {
                System.out.print("map.get(\"standword\")");
            }
            if (extract_word != null && extract_word.indexOf("、") > -1) {
                ews = extract_word.split("、");
                for (int i  = 0; i < ews.length; i++){
                    Map<String, Object> line = new HashMap<>();
                    line.put("disease", map.get("disease"));
                    line.put("type", map.get("type"));
                    line.put("number", map.get("number"));
                    line.put("standword", map.get("standword"));
                    line.put("extract_word", ews[i]);
                    lists.add(line);
                }
            } else {
                Map<String, Object> line = new HashMap<>();
                line.put("disease", map.get("disease"));
                line.put("type", map.get("type"));
                line.put("number", map.get("number"));
                line.put("standword", map.get("standword"));
                line.put("extract_word", extract_word);
                lists.add(line);
            }
        }

        nlpJdbc.insert(lists, "medicine_rule_origin_split", new String[]{"disease", "type", "number", "standword", "extract_word"});
    }

    public void split() {
        MysqlJdbc nlpJdbc = new MysqlJdbc("root", "diagbot@20180822", "jdbc:mysql://192.168.2.235:3306/diagbot-rule?useUnicode=true&characterEncoding=UTF-8");
        List<Map<String, String>> data = nlpJdbc.query("medicine_rule_part1", new String[]{"id", "standWord"}, " where number like '1.%' or number like '2.%'");
        try {
            List<Lexeme> lexemes = null;
            String s = "";
            String sql = "";
            List<String> sqls = new ArrayList<>();
            for (Map<String, String> map : data) {
                if (map.get("standWord") != null) {
                    lexemes = ParticipleUtil.participle(map.get("standWord"), false);
                    for (int i = 0; i < lexemes.size(); i++) {
                        if (i > 0) {
                            s = s + "," + lexemes.get(i).getText();
                        } else {
                            s = lexemes.get(i).getText();
                        }
                    }
                    sql = "update medicine_rule_part1 set split_word = '" + s + "' where id = " + map.get("id");
                    sqls.add(sql);
                }
            }
            nlpJdbc.update(sqls);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
